Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -453,30 +453,55 @@ def calculate_dot_product_and_representations_independent(query_model_choice, do
|
|
453 |
|
454 |
# Document Representation
|
455 |
full_output += f"Document Representation ({doc_model_name_display}):\n\n"
|
456 |
-
|
457 |
-
full_output += full_rep_str
|
458 |
|
459 |
return full_output
|
460 |
|
461 |
|
462 |
# Global variable to store the share URL once the app is launched
|
463 |
-
# This will be populated by the demo.launch() call
|
464 |
global_share_url = None
|
465 |
|
466 |
-
|
467 |
-
|
468 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
469 |
|
470 |
# --- Gradio Interface Setup with Tabs ---
|
471 |
with gr.Blocks(title="SPLADE Demos", css=css) as demo:
|
472 |
-
gr.Markdown("# π Sparse Encoder Playground")
|
473 |
-
gr.Markdown("Explore different SPLADE models and their sparse representation types, and calculate similarity between query and document representations.")
|
474 |
|
475 |
with gr.Tabs():
|
476 |
with gr.TabItem("Sparse Representation"):
|
477 |
gr.Markdown("### Produce a Sparse Representation of an Input Text")
|
478 |
with gr.Row():
|
479 |
-
with gr.Column(scale=1):
|
480 |
model_radio = gr.Radio(
|
481 |
[
|
482 |
"MLM encoder (SPLADE-cocondenser-distil)",
|
@@ -495,35 +520,55 @@ with gr.Blocks(title="SPLADE Demos", css=css) as demo:
|
|
495 |
# Custom Share Button and URL display
|
496 |
with gr.Row(elem_classes="share-button-container"):
|
497 |
share_button = gr.Button(
|
498 |
-
"π Get Share Link",
|
499 |
elem_classes="custom-share-button",
|
500 |
size="sm"
|
501 |
)
|
502 |
|
503 |
-
|
504 |
-
label="Share URL",
|
505 |
-
interactive=True,
|
506 |
-
visible=False,
|
507 |
-
placeholder="Click 'Get Share Link' to generate URL..."
|
508 |
)
|
509 |
|
|
|
|
|
|
|
|
|
510 |
info_output_display = gr.Markdown(
|
511 |
value="",
|
512 |
label="Vector Information",
|
513 |
elem_id="info_output_display"
|
514 |
)
|
515 |
-
with gr.Column(scale=2):
|
516 |
main_representation_output = gr.Markdown()
|
517 |
|
518 |
-
# Connect share button
|
519 |
-
#
|
520 |
-
#
|
|
|
521 |
share_button.click(
|
522 |
-
fn=
|
523 |
-
outputs=
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
524 |
).then(
|
525 |
-
fn=lambda: gr.update(visible=True),
|
526 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
527 |
)
|
528 |
|
529 |
|
@@ -586,36 +631,22 @@ with gr.Blocks(title="SPLADE Demos", css=css) as demo:
|
|
586 |
# This block ensures the share URL is captured when the app launches
|
587 |
if __name__ == "__main__":
|
588 |
# Launch and capture the share URL
|
589 |
-
#
|
590 |
-
#
|
591 |
-
|
592 |
-
#
|
593 |
-
#
|
594 |
-
|
595 |
-
|
596 |
-
#
|
597 |
-
|
598 |
-
|
599 |
-
|
600 |
-
|
601 |
-
|
602 |
-
|
603 |
-
|
604 |
-
|
605 |
-
|
606 |
-
|
607 |
-
|
608 |
-
print("\n--- Gradio App Launched ---")
|
609 |
-
print("If a public share link is generated, it will be displayed in your console.")
|
610 |
-
print("You can also use the 'π Get Share Link' button on the 'Sparse Representation' tab.")
|
611 |
-
print("---------------------------\n")
|
612 |
-
|
613 |
-
# In a real deployed scenario, this global_share_url might be populated by
|
614 |
-
# a Gradio callback for `launch`, but for typical local execution,
|
615 |
-
# the URL is primarily given via console/built-in UI.
|
616 |
-
# The `get_current_share_url` function will return 'Share URL not available yet.'
|
617 |
-
# until a share URL is actually generated by Gradio's backend when running `share=True`.
|
618 |
-
# To truly populate `global_share_url` reliably for the custom button,
|
619 |
-
# you often need to manually copy it or rely on a more advanced Gradio feature (like a custom JS callback or an API endpoint)
|
620 |
-
# which is beyond simple `gr.Blocks` component connections.
|
621 |
-
# The most direct approach for users is the default Gradio share icon/text.
|
|
|
453 |
|
454 |
# Document Representation
|
455 |
full_output += f"Document Representation ({doc_model_name_display}):\n\n"
|
456 |
+
full_output += doc_main_rep_str + "\n\n" + doc_info_str # Added an extra newline for better spacing
|
|
|
457 |
|
458 |
return full_output
|
459 |
|
460 |
|
461 |
# Global variable to store the share URL once the app is launched
|
|
|
462 |
global_share_url = None
|
463 |
|
464 |
+
# JavaScript to copy text to clipboard
|
465 |
+
# This will be injected into the Gradio UI
|
466 |
+
copy_to_clipboard_js = """
|
467 |
+
async (url) => {
|
468 |
+
if (url) {
|
469 |
+
try {
|
470 |
+
await navigator.clipboard.writeText(url);
|
471 |
+
console.log('Share URL copied to clipboard:', url);
|
472 |
+
return 'Copied to clipboard!'; // Message to display to user
|
473 |
+
} catch (err) {
|
474 |
+
console.error('Failed to copy share URL:', err);
|
475 |
+
return 'Failed to copy!';
|
476 |
+
}
|
477 |
+
}
|
478 |
+
return 'URL not available.';
|
479 |
+
}
|
480 |
+
"""
|
481 |
+
|
482 |
+
def get_share_url_for_button():
|
483 |
+
"""
|
484 |
+
Function to provide the share URL to the Gradio Textbox.
|
485 |
+
This will be called when the custom button is clicked.
|
486 |
+
"""
|
487 |
+
if global_share_url:
|
488 |
+
return global_share_url
|
489 |
+
else:
|
490 |
+
# In case the URL isn't set yet, provide a placeholder.
|
491 |
+
# This function runs server-side, so it won't have the client-side URL
|
492 |
+
# until the app has truly launched.
|
493 |
+
return "Generating share link..."
|
494 |
|
495 |
# --- Gradio Interface Setup with Tabs ---
|
496 |
with gr.Blocks(title="SPLADE Demos", css=css) as demo:
|
497 |
+
gr.Markdown("# π Sparse Encoder Playground")
|
498 |
+
gr.Markdown("Explore different SPLADE models and their sparse representation types, and calculate similarity between query and document representations.")
|
499 |
|
500 |
with gr.Tabs():
|
501 |
with gr.TabItem("Sparse Representation"):
|
502 |
gr.Markdown("### Produce a Sparse Representation of an Input Text")
|
503 |
with gr.Row():
|
504 |
+
with gr.Column(scale=1):
|
505 |
model_radio = gr.Radio(
|
506 |
[
|
507 |
"MLM encoder (SPLADE-cocondenser-distil)",
|
|
|
520 |
# Custom Share Button and URL display
|
521 |
with gr.Row(elem_classes="share-button-container"):
|
522 |
share_button = gr.Button(
|
523 |
+
"π Get & Copy Share Link",
|
524 |
elem_classes="custom-share-button",
|
525 |
size="sm"
|
526 |
)
|
527 |
|
528 |
+
share_output_textbox = gr.Textbox( # Renamed for clarity
|
529 |
+
label="Share URL (Click button to copy)",
|
530 |
+
interactive=True,
|
531 |
+
visible=False,
|
532 |
+
placeholder="Click 'Get & Copy Share Link' to generate and copy URL..."
|
533 |
)
|
534 |
|
535 |
+
# A small text display to confirm copy
|
536 |
+
copy_confirmation_text = gr.Markdown(value="", visible=False)
|
537 |
+
|
538 |
+
|
539 |
info_output_display = gr.Markdown(
|
540 |
value="",
|
541 |
label="Vector Information",
|
542 |
elem_id="info_output_display"
|
543 |
)
|
544 |
+
with gr.Column(scale=2):
|
545 |
main_representation_output = gr.Markdown()
|
546 |
|
547 |
+
# Connect share button:
|
548 |
+
# 1. Populate the textbox with the share URL (from backend)
|
549 |
+
# 2. Make the textbox visible
|
550 |
+
# 3. Use JS to copy its content to clipboard
|
551 |
share_button.click(
|
552 |
+
fn=get_share_url_for_button, # This populates the textbox
|
553 |
+
outputs=share_output_textbox
|
554 |
+
).then(
|
555 |
+
fn=lambda: gr.update(visible=True), # This makes the textbox visible
|
556 |
+
outputs=share_output_textbox
|
557 |
+
).then(
|
558 |
+
fn=None, # This is a dummy function to allow JS to run
|
559 |
+
_js=f'(url) => {{ {copy_to_clipboard_js} (url); }}', # Pass the URL from the textbox to JS
|
560 |
+
inputs=[share_output_textbox], # The output of previous step (share_output_textbox) becomes input to this step
|
561 |
+
outputs=[copy_confirmation_text] # Output status of copy to confirmation text
|
562 |
).then(
|
563 |
+
fn=lambda msg: gr.update(value=msg, visible=True), # Show confirmation message
|
564 |
+
inputs=[copy_confirmation_text],
|
565 |
+
outputs=[copy_confirmation_text]
|
566 |
+
).then(
|
567 |
+
fn=lambda: gr.update(value="", visible=False), # Hide confirmation message after a short delay
|
568 |
+
outputs=[copy_confirmation_text],
|
569 |
+
queue=False, # Don't block other events
|
570 |
+
api_name="hide_confirmation_message", # Optional: give it an API name
|
571 |
+
every=2 # Hide after 2 seconds
|
572 |
)
|
573 |
|
574 |
|
|
|
631 |
# This block ensures the share URL is captured when the app launches
|
632 |
if __name__ == "__main__":
|
633 |
# Launch and capture the share URL
|
634 |
+
# Gradio will print the share URL to the console automatically when share=True
|
635 |
+
# For more reliable programmatic access *within* the app (especially for older Gradio versions
|
636 |
+
# or more complex scenarios), you might need a custom endpoint or to rely on the
|
637 |
+
# client-side `Gradio.share_url` variable.
|
638 |
+
# For simple local demo, printing to console is often sufficient.
|
639 |
+
launched_info = demo.launch(share=True)
|
640 |
+
|
641 |
+
# In newer Gradio versions (e.g., 3.x and 4.x), the share_url is typically
|
642 |
+
# available via `launched_info.share_url` directly.
|
643 |
+
if hasattr(launched_info, 'share_url') and launched_info.share_url:
|
644 |
+
global_share_url = launched_info.share_url
|
645 |
+
print(f"\n--- Public Share URL (for your custom button): {global_share_url} ---\n")
|
646 |
+
else:
|
647 |
+
print("\n--- Public Share URL not available directly from launch info. "
|
648 |
+
"Please check your console for the Gradio-generated share link. ---\n")
|
649 |
+
|
650 |
+
print("\n--- Gradio App Running ---")
|
651 |
+
print("Open the link in your browser to interact with the app.")
|
652 |
+
print("---------------------------\n")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|