Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -425,43 +425,41 @@ def calculate_dot_product_and_representations_independent(query_model_choice, do
|
|
425 |
doc_vector_fn, doc_tokenizer, doc_is_binary, doc_model_name_display = get_model_assets(doc_model_choice)
|
426 |
|
427 |
if query_vector_fn is None or doc_vector_fn is None:
|
428 |
-
return "Please select valid models for both query and document encoding.", ""
|
429 |
|
430 |
query_vector = query_vector_fn(query_text)
|
431 |
doc_vector = doc_vector_fn(doc_text)
|
432 |
|
433 |
if query_vector is None or doc_vector is None:
|
434 |
-
return "Failed to generate one or both vectors. Please check model loading and input text.", ""
|
435 |
|
436 |
# Calculate dot product
|
437 |
-
# Ensure both vectors are on CPU before dot product to avoid device mismatch issues
|
438 |
-
# and to ensure .item() works reliably for conversion to float.
|
439 |
dot_product = float(torch.dot(query_vector.cpu(), doc_vector.cpu()).item())
|
440 |
|
441 |
# Format representations - these functions now return two strings (main_output, info_output)
|
442 |
query_main_rep_str, query_info_str = format_sparse_vector_output(query_vector, query_tokenizer, query_is_binary)
|
443 |
doc_main_rep_str, doc_info_str = format_sparse_vector_output(doc_vector, doc_tokenizer, doc_is_binary)
|
444 |
|
445 |
-
|
446 |
# Combine output into a single string for the Markdown component
|
|
|
447 |
full_output = f"### Dot Product Score: {dot_product:.6f}\n\n"
|
448 |
full_output += "---\n\n"
|
449 |
|
450 |
# Query Representation
|
451 |
-
full_output += f"Query Representation ({query_model_name_display}):\n
|
452 |
-
full_output += query_main_rep_str
|
453 |
-
full_output += "\n
|
|
|
454 |
|
455 |
# Document Representation
|
456 |
-
full_output += f"Document Representation ({doc_model_name_display}):\n
|
457 |
-
|
458 |
-
full_output +=
|
459 |
|
460 |
return full_output
|
461 |
|
462 |
|
463 |
# Global variable to store the share URL once the app is launched
|
464 |
-
# This will be populated by the demo.launch() call
|
465 |
global_share_url = None
|
466 |
|
467 |
def get_current_share_url():
|
@@ -503,8 +501,8 @@ with gr.Blocks(title="SPLADE Demos", css=css) as demo:
|
|
503 |
|
504 |
share_output = gr.Textbox(
|
505 |
label="Share URL",
|
506 |
-
interactive=True,
|
507 |
-
visible=False,
|
508 |
placeholder="Click 'Get Share Link' to generate URL..."
|
509 |
)
|
510 |
|
@@ -517,8 +515,6 @@ with gr.Blocks(title="SPLADE Demos", css=css) as demo:
|
|
517 |
main_representation_output = gr.Markdown()
|
518 |
|
519 |
# Connect share button.
|
520 |
-
# When share_button is clicked, get_current_share_url is called to populate share_output
|
521 |
-
# and then share_output is made visible.
|
522 |
share_button.click(
|
523 |
fn=get_current_share_url,
|
524 |
outputs=share_output
|
@@ -578,13 +574,14 @@ with gr.Blocks(title="SPLADE Demos", css=css) as demo:
|
|
578 |
placeholder="e.g., Padua's cuisine is as famous as its legendary University."
|
579 |
)
|
580 |
|
581 |
-
# --- MODIFIED: Output component as a Textbox with scrollbar ---
|
582 |
output_dot_product_text = gr.Textbox(
|
583 |
label="Dot Product Result and Representations",
|
584 |
-
lines=20,
|
585 |
-
autoscroll=True,
|
586 |
-
interactive=False,
|
587 |
-
max_lines=None
|
|
|
588 |
)
|
589 |
|
590 |
# Update the gr.Interface call to use the new Textbox output
|
@@ -596,7 +593,7 @@ with gr.Blocks(title="SPLADE Demos", css=css) as demo:
|
|
596 |
query_text_input,
|
597 |
doc_text_input
|
598 |
],
|
599 |
-
outputs=output_dot_product_text,
|
600 |
allow_flagging="never"
|
601 |
)
|
602 |
|
@@ -617,37 +614,8 @@ with gr.Blocks(title="SPLADE Demos", css=css) as demo:
|
|
617 |
|
618 |
# This block ensures the share URL is captured when the app launches
|
619 |
if __name__ == "__main__":
|
620 |
-
# Launch and capture the share URL
|
621 |
-
# Setting live=False for better control over when functions run
|
622 |
-
# and to ensure the share_url is available early.
|
623 |
launched_demo = demo.launch(share=True)
|
624 |
-
# The share_url is typically available on the launched_demo object
|
625 |
-
# This might vary slightly based on Gradio version.
|
626 |
-
# For newer Gradio versions (>=3.x), the share_url is usually printed to console
|
627 |
-
# and can sometimes be accessed via launched_demo.share_url directly if `return_url=True`
|
628 |
-
# (though that was removed in recent versions as it's default now)
|
629 |
-
|
630 |
-
# In older Gradio, you might need to manually copy the URL from console.
|
631 |
-
# In newer Gradio, the share URL is often automatically put into a share button/link
|
632 |
-
# in the UI (usually top right or footer) when share=True.
|
633 |
-
# For our custom button, we're assuming we'll get the URL once it's launched.
|
634 |
-
# Gradio automatically injects the share URL into the client-side JavaScript.
|
635 |
-
# A simple way to get it from within the app is using Javascript or by storing it globally.
|
636 |
-
|
637 |
-
# To bridge the gap, we'll tell the user to copy from the console for now,
|
638 |
-
# or the built-in Gradio share icon (usually on the top right) will work automatically.
|
639 |
-
|
640 |
print("\n--- Gradio App Launched ---")
|
641 |
print("If a public share link is generated, it will be displayed in your console.")
|
642 |
print("You can also use the '🔗 Get Share Link' button on the 'Sparse Representation' tab.")
|
643 |
-
print("---------------------------\n")
|
644 |
-
|
645 |
-
# In a real deployed scenario, this global_share_url might be populated by
|
646 |
-
# a Gradio callback for `launch`, but for typical local execution,
|
647 |
-
# the URL is primarily given via console/built-in UI.
|
648 |
-
# The `get_current_share_url` function will return 'Share URL not available yet.'
|
649 |
-
# until a share URL is actually generated by Gradio's backend when running `share=True`.
|
650 |
-
# To truly populate `global_share_url` reliably for the custom button,
|
651 |
-
# you often need to manually copy it or rely on a more advanced Gradio feature (like a custom JS callback or an API endpoint)
|
652 |
-
# which is beyond simple `gr.Blocks` component connections.
|
653 |
-
# The most direct approach for users is the default Gradio share icon/text.
|
|
|
425 |
doc_vector_fn, doc_tokenizer, doc_is_binary, doc_model_name_display = get_model_assets(doc_model_choice)
|
426 |
|
427 |
if query_vector_fn is None or doc_vector_fn is None:
|
428 |
+
return "Please select valid models for both query and document encoding.", ""
|
429 |
|
430 |
query_vector = query_vector_fn(query_text)
|
431 |
doc_vector = doc_vector_fn(doc_text)
|
432 |
|
433 |
if query_vector is None or doc_vector is None:
|
434 |
+
return "Failed to generate one or both vectors. Please check model loading and input text.", ""
|
435 |
|
436 |
# Calculate dot product
|
|
|
|
|
437 |
dot_product = float(torch.dot(query_vector.cpu(), doc_vector.cpu()).item())
|
438 |
|
439 |
# Format representations - these functions now return two strings (main_output, info_output)
|
440 |
query_main_rep_str, query_info_str = format_sparse_vector_output(query_vector, query_tokenizer, query_is_binary)
|
441 |
doc_main_rep_str, doc_info_str = format_sparse_vector_output(doc_vector, doc_tokenizer, doc_is_binary)
|
442 |
|
|
|
443 |
# Combine output into a single string for the Markdown component
|
444 |
+
# Using Markdown's blockquote for representations can help them stand out as code/data
|
445 |
full_output = f"### Dot Product Score: {dot_product:.6f}\n\n"
|
446 |
full_output += "---\n\n"
|
447 |
|
448 |
# Query Representation
|
449 |
+
full_output += f"#### Query Representation ({query_model_name_display}):\n" # Smaller heading for sub-section
|
450 |
+
full_output += f"> {query_main_rep_str}\n" # Using blockquote for the sparse list
|
451 |
+
full_output += f"> {query_info_str}\n" # Using blockquote for info as well
|
452 |
+
full_output += "\n---\n\n" # Separator
|
453 |
|
454 |
# Document Representation
|
455 |
+
full_output += f"#### Document Representation ({doc_model_name_display}):\n" # Smaller heading for sub-section
|
456 |
+
full_output += f"> {doc_main_rep_str}\n" # Using blockquote
|
457 |
+
full_output += f"> {doc_info_str}" # Using blockquote
|
458 |
|
459 |
return full_output
|
460 |
|
461 |
|
462 |
# Global variable to store the share URL once the app is launched
|
|
|
463 |
global_share_url = None
|
464 |
|
465 |
def get_current_share_url():
|
|
|
501 |
|
502 |
share_output = gr.Textbox(
|
503 |
label="Share URL",
|
504 |
+
interactive=True,
|
505 |
+
visible=False,
|
506 |
placeholder="Click 'Get Share Link' to generate URL..."
|
507 |
)
|
508 |
|
|
|
515 |
main_representation_output = gr.Markdown()
|
516 |
|
517 |
# Connect share button.
|
|
|
|
|
518 |
share_button.click(
|
519 |
fn=get_current_share_url,
|
520 |
outputs=share_output
|
|
|
574 |
placeholder="e.g., Padua's cuisine is as famous as its legendary University."
|
575 |
)
|
576 |
|
577 |
+
# --- MODIFIED: Output component as a Textbox with scrollbar and Markdown rendering ---
|
578 |
output_dot_product_text = gr.Textbox(
|
579 |
label="Dot Product Result and Representations",
|
580 |
+
lines=20,
|
581 |
+
autoscroll=True,
|
582 |
+
interactive=False,
|
583 |
+
max_lines=None,
|
584 |
+
render_as_markdown=True # <--- THE KEY ADDITION HERE!
|
585 |
)
|
586 |
|
587 |
# Update the gr.Interface call to use the new Textbox output
|
|
|
593 |
query_text_input,
|
594 |
doc_text_input
|
595 |
],
|
596 |
+
outputs=output_dot_product_text,
|
597 |
allow_flagging="never"
|
598 |
)
|
599 |
|
|
|
614 |
|
615 |
# This block ensures the share URL is captured when the app launches
|
616 |
if __name__ == "__main__":
|
|
|
|
|
|
|
617 |
launched_demo = demo.launch(share=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
618 |
print("\n--- Gradio App Launched ---")
|
619 |
print("If a public share link is generated, it will be displayed in your console.")
|
620 |
print("You can also use the '🔗 Get Share Link' button on the 'Sparse Representation' tab.")
|
621 |
+
print("---------------------------\n")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|