Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -6,7 +6,7 @@ from tqdm.auto import tqdm
|
|
| 6 |
import os
|
| 7 |
|
| 8 |
|
| 9 |
-
# CSS to style the share button
|
| 10 |
css = """
|
| 11 |
.share-button-container {
|
| 12 |
display: flex;
|
|
@@ -34,6 +34,28 @@ css = """
|
|
| 34 |
transform: translateY(-2px);
|
| 35 |
box-shadow: 0 4px 8px rgba(0,0,0,0.2);
|
| 36 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 37 |
"""
|
| 38 |
|
| 39 |
|
|
@@ -431,27 +453,19 @@ def calculate_dot_product_and_representations_independent(query_model_choice, do
|
|
| 431 |
|
| 432 |
# Document Representation
|
| 433 |
full_output += f"Document Representation ({doc_model_name_display}):\n\n"
|
| 434 |
-
|
|
|
|
| 435 |
|
| 436 |
return full_output
|
| 437 |
|
| 438 |
|
| 439 |
-
#
|
| 440 |
-
|
|
|
|
| 441 |
|
| 442 |
-
def
|
| 443 |
-
|
| 444 |
-
if
|
| 445 |
-
return f"🔗 Share this app: {share_url}"
|
| 446 |
-
else:
|
| 447 |
-
return "🔗 Share URL will be available after launching with share=True"
|
| 448 |
-
|
| 449 |
-
def copy_share_url():
|
| 450 |
-
global share_url
|
| 451 |
-
if share_url:
|
| 452 |
-
return share_url
|
| 453 |
-
else:
|
| 454 |
-
return "Share URL not available yet"
|
| 455 |
|
| 456 |
# --- Gradio Interface Setup with Tabs ---
|
| 457 |
with gr.Blocks(title="SPLADE Demos", css=css) as demo:
|
|
@@ -477,22 +491,22 @@ with gr.Blocks(title="SPLADE Demos", css=css) as demo:
|
|
| 477 |
label="Enter your query or document text here:",
|
| 478 |
placeholder="e.g., Why is Padua the nicest city in Italy?"
|
| 479 |
)
|
| 480 |
-
|
| 481 |
-
#
|
| 482 |
with gr.Row(elem_classes="share-button-container"):
|
| 483 |
share_button = gr.Button(
|
| 484 |
-
"🔗 Get Share Link",
|
| 485 |
elem_classes="custom-share-button",
|
| 486 |
size="sm"
|
| 487 |
)
|
| 488 |
-
|
| 489 |
-
|
| 490 |
-
label="Share
|
| 491 |
-
interactive=True,
|
| 492 |
-
visible=False,
|
| 493 |
-
placeholder="Share
|
| 494 |
)
|
| 495 |
-
|
| 496 |
info_output_display = gr.Markdown(
|
| 497 |
value="",
|
| 498 |
label="Vector Information",
|
|
@@ -501,17 +515,19 @@ with gr.Blocks(title="SPLADE Demos", css=css) as demo:
|
|
| 501 |
with gr.Column(scale=2): # Right column for the main representation output
|
| 502 |
main_representation_output = gr.Markdown()
|
| 503 |
|
| 504 |
-
|
| 505 |
-
#
|
|
|
|
| 506 |
share_button.click(
|
| 507 |
-
fn=
|
| 508 |
-
outputs=
|
| 509 |
).then(
|
| 510 |
fn=lambda: gr.update(visible=True),
|
| 511 |
-
outputs=
|
| 512 |
)
|
| 513 |
|
| 514 |
-
|
|
|
|
| 515 |
model_radio.change(
|
| 516 |
fn=predict_representation_explorer,
|
| 517 |
inputs=[model_radio, input_text],
|
|
@@ -522,26 +538,17 @@ with gr.Blocks(title="SPLADE Demos", css=css) as demo:
|
|
| 522 |
inputs=[model_radio, input_text],
|
| 523 |
outputs=[main_representation_output, info_output_display]
|
| 524 |
)
|
| 525 |
-
|
| 526 |
-
# Connect share button
|
| 527 |
-
share_button.click(
|
| 528 |
-
fn=generate_share_link,
|
| 529 |
-
outputs=share_output
|
| 530 |
-
).then(
|
| 531 |
-
fn=lambda: gr.update(visible=True),
|
| 532 |
-
outputs=share_output
|
| 533 |
-
)
|
| 534 |
|
| 535 |
# Initial call to populate on load (optional, but good for demo)
|
| 536 |
demo.load(
|
| 537 |
-
fn=lambda: predict_representation_explorer(
|
| 538 |
outputs=[main_representation_output, info_output_display]
|
| 539 |
)
|
| 540 |
|
| 541 |
-
|
|
|
|
| 542 |
gr.Markdown("### Calculate Dot Product Similarity Between Encoded Query and Document")
|
| 543 |
|
| 544 |
-
# Define the common model choices for cleaner code
|
| 545 |
model_choices = [
|
| 546 |
"MLM encoder (SPLADE-cocondenser-distil)",
|
| 547 |
"MLP encoder (SPLADE-v3-lexical)",
|
|
@@ -549,17 +556,17 @@ with gr.Blocks(title="SPLADE Demos", css=css) as demo:
|
|
| 549 |
]
|
| 550 |
|
| 551 |
gr.Interface(
|
| 552 |
-
fn=calculate_dot_product_and_representations_independent,
|
| 553 |
inputs=[
|
| 554 |
gr.Radio(
|
| 555 |
model_choices,
|
| 556 |
label="Choose Query Encoding Model",
|
| 557 |
-
value="MLM encoder (SPLADE-cocondenser-distil)"
|
| 558 |
),
|
| 559 |
gr.Radio(
|
| 560 |
model_choices,
|
| 561 |
label="Choose Document Encoding Model",
|
| 562 |
-
value="MLM encoder (SPLADE-cocondenser-distil)"
|
| 563 |
),
|
| 564 |
gr.Textbox(
|
| 565 |
lines=3,
|
|
@@ -573,15 +580,42 @@ with gr.Blocks(title="SPLADE Demos", css=css) as demo:
|
|
| 573 |
)
|
| 574 |
],
|
| 575 |
outputs=gr.Markdown(),
|
| 576 |
-
allow_flagging="never"
|
| 577 |
)
|
| 578 |
|
| 579 |
-
#
|
| 580 |
if __name__ == "__main__":
|
| 581 |
# Launch and capture the share URL
|
| 582 |
-
|
| 583 |
-
|
| 584 |
-
|
| 585 |
-
|
| 586 |
-
|
| 587 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
import os
|
| 7 |
|
| 8 |
|
| 9 |
+
# CSS to style the custom share button (for the "Sparse Representation" tab)
|
| 10 |
css = """
|
| 11 |
.share-button-container {
|
| 12 |
display: flex;
|
|
|
|
| 34 |
transform: translateY(-2px);
|
| 35 |
box-shadow: 0 4px 8px rgba(0,0,0,0.2);
|
| 36 |
}
|
| 37 |
+
/* IMPORTANT: This CSS targets Gradio's *default* share button that appears
|
| 38 |
+
when demo.launch(share=True) is used.
|
| 39 |
+
You might want to comment this out if you prefer Gradio's default positioning
|
| 40 |
+
for the main share button (usually in the header/footer) and rely only on your custom one.
|
| 41 |
+
*/
|
| 42 |
+
.share-button {
|
| 43 |
+
position: fixed !important;
|
| 44 |
+
top: 20px !important;
|
| 45 |
+
right: 20px !important;
|
| 46 |
+
z-index: 1000 !important;
|
| 47 |
+
background: #4CAF50 !important;
|
| 48 |
+
color: white !important;
|
| 49 |
+
border-radius: 8px !important;
|
| 50 |
+
padding: 8px 16px !important;
|
| 51 |
+
font-weight: bold !important;
|
| 52 |
+
box-shadow: 0 2px 10px rgba(0,0,0,0.2) !important;
|
| 53 |
+
}
|
| 54 |
+
|
| 55 |
+
.share-button:hover {
|
| 56 |
+
background: #45a049 !important;
|
| 57 |
+
transform: translateY(-1px) !important;
|
| 58 |
+
}
|
| 59 |
"""
|
| 60 |
|
| 61 |
|
|
|
|
| 453 |
|
| 454 |
# Document Representation
|
| 455 |
full_output += f"Document Representation ({doc_model_name_display}):\n\n"
|
| 456 |
+
full_rep_str = doc_main_rep_str + "\n\n" + doc_info_str # Adjusted for consistency
|
| 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 |
+
def get_current_share_url():
|
| 467 |
+
"""Returns the globally stored share URL."""
|
| 468 |
+
return global_share_url if global_share_url else "Share URL not available yet."
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 469 |
|
| 470 |
# --- Gradio Interface Setup with Tabs ---
|
| 471 |
with gr.Blocks(title="SPLADE Demos", css=css) as demo:
|
|
|
|
| 491 |
label="Enter your query or document text here:",
|
| 492 |
placeholder="e.g., Why is Padua the nicest city in Italy?"
|
| 493 |
)
|
| 494 |
+
|
| 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 |
+
share_output = gr.Textbox(
|
| 504 |
+
label="Share URL",
|
| 505 |
+
interactive=True, # Make it interactive so user can copy
|
| 506 |
+
visible=False, # Start as hidden
|
| 507 |
+
placeholder="Click 'Get Share Link' to generate URL..."
|
| 508 |
)
|
| 509 |
+
|
| 510 |
info_output_display = gr.Markdown(
|
| 511 |
value="",
|
| 512 |
label="Vector Information",
|
|
|
|
| 515 |
with gr.Column(scale=2): # Right column for the main representation output
|
| 516 |
main_representation_output = gr.Markdown()
|
| 517 |
|
| 518 |
+
# Connect share button.
|
| 519 |
+
# When share_button is clicked, get_current_share_url is called to populate share_output
|
| 520 |
+
# and then share_output is made visible.
|
| 521 |
share_button.click(
|
| 522 |
+
fn=get_current_share_url,
|
| 523 |
+
outputs=share_output
|
| 524 |
).then(
|
| 525 |
fn=lambda: gr.update(visible=True),
|
| 526 |
+
outputs=share_output
|
| 527 |
)
|
| 528 |
|
| 529 |
+
|
| 530 |
+
# Connect the core prediction logic
|
| 531 |
model_radio.change(
|
| 532 |
fn=predict_representation_explorer,
|
| 533 |
inputs=[model_radio, input_text],
|
|
|
|
| 538 |
inputs=[model_radio, input_text],
|
| 539 |
outputs=[main_representation_output, info_output_display]
|
| 540 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 541 |
|
| 542 |
# Initial call to populate on load (optional, but good for demo)
|
| 543 |
demo.load(
|
| 544 |
+
fn=lambda: predict_representation_explorer(model_radio.value, input_text.value),
|
| 545 |
outputs=[main_representation_output, info_output_display]
|
| 546 |
)
|
| 547 |
|
| 548 |
+
|
| 549 |
+
with gr.TabItem("Compare Encoders"):
|
| 550 |
gr.Markdown("### Calculate Dot Product Similarity Between Encoded Query and Document")
|
| 551 |
|
|
|
|
| 552 |
model_choices = [
|
| 553 |
"MLM encoder (SPLADE-cocondenser-distil)",
|
| 554 |
"MLP encoder (SPLADE-v3-lexical)",
|
|
|
|
| 556 |
]
|
| 557 |
|
| 558 |
gr.Interface(
|
| 559 |
+
fn=calculate_dot_product_and_representations_independent,
|
| 560 |
inputs=[
|
| 561 |
gr.Radio(
|
| 562 |
model_choices,
|
| 563 |
label="Choose Query Encoding Model",
|
| 564 |
+
value="MLM encoder (SPLADE-cocondenser-distil)"
|
| 565 |
),
|
| 566 |
gr.Radio(
|
| 567 |
model_choices,
|
| 568 |
label="Choose Document Encoding Model",
|
| 569 |
+
value="MLM encoder (SPLADE-cocondenser-distil)"
|
| 570 |
),
|
| 571 |
gr.Textbox(
|
| 572 |
lines=3,
|
|
|
|
| 580 |
)
|
| 581 |
],
|
| 582 |
outputs=gr.Markdown(),
|
| 583 |
+
allow_flagging="never"
|
| 584 |
)
|
| 585 |
|
| 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 |
+
# Setting live=False for better control over when functions run
|
| 590 |
+
# and to ensure the share_url is available early.
|
| 591 |
+
launched_demo = demo.launch(share=True)
|
| 592 |
+
# The share_url is typically available on the launched_demo object
|
| 593 |
+
# This might vary slightly based on Gradio version.
|
| 594 |
+
# For newer Gradio versions (>=3.x), the share_url is usually printed to console
|
| 595 |
+
# and can sometimes be accessed via launched_demo.share_url directly if `return_url=True`
|
| 596 |
+
# (though that was removed in recent versions as it's default now)
|
| 597 |
+
|
| 598 |
+
# In older Gradio, you might need to manually copy the URL from console.
|
| 599 |
+
# In newer Gradio, the share URL is often automatically put into a share button/link
|
| 600 |
+
# in the UI (usually top right or footer) when share=True.
|
| 601 |
+
# For our custom button, we're assuming we'll get the URL once it's launched.
|
| 602 |
+
# Gradio automatically injects the share URL into the client-side JavaScript.
|
| 603 |
+
# A simple way to get it from within the app is using Javascript or by storing it globally.
|
| 604 |
+
|
| 605 |
+
# To bridge the gap, we'll tell the user to copy from the console for now,
|
| 606 |
+
# or the built-in Gradio share icon (usually on the top right) will work automatically.
|
| 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.
|