Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,34 +1,42 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
from sentence_transformers import CrossEncoder
|
|
|
|
| 3 |
|
| 4 |
-
# Load
|
| 5 |
-
|
|
|
|
| 6 |
|
| 7 |
def predict_similarity(s1, s2):
|
| 8 |
-
score
|
| 9 |
-
|
|
|
|
|
|
|
|
|
|
| 10 |
|
| 11 |
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
| 12 |
-
gr.Markdown("## 🔎
|
| 13 |
-
gr.Markdown(
|
|
|
|
|
|
|
|
|
|
| 14 |
|
| 15 |
with gr.Row():
|
| 16 |
-
s1 = gr.Textbox(label="
|
| 17 |
-
s2 = gr.Textbox(label="
|
| 18 |
|
| 19 |
btn = gr.Button("Compute Similarity 🚀")
|
| 20 |
-
out = gr.Number(label="
|
| 21 |
|
| 22 |
btn.click(fn=predict_similarity, inputs=[s1, s2], outputs=out)
|
| 23 |
|
| 24 |
gr.Examples(
|
| 25 |
examples=[
|
| 26 |
-
["
|
| 27 |
-
["I am happy today", "I am feeling
|
| 28 |
-
["
|
| 29 |
-
["
|
| 30 |
],
|
| 31 |
inputs=[s1, s2],
|
| 32 |
)
|
| 33 |
|
| 34 |
-
demo.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
from sentence_transformers import CrossEncoder
|
| 3 |
+
import torch
|
| 4 |
|
| 5 |
+
# Load MS MARCO CrossEncoder (query-document relevance)
|
| 6 |
+
MODEL_NAME = "cross-encoder/ms-marco-MiniLM-L-12-v2"
|
| 7 |
+
model = CrossEncoder(MODEL_NAME)
|
| 8 |
|
| 9 |
def predict_similarity(s1, s2):
|
| 10 |
+
# Get raw score (logit)
|
| 11 |
+
score = model.predict([(s1, s2)])[0]
|
| 12 |
+
# Apply sigmoid to map to 0–1
|
| 13 |
+
similarity = torch.sigmoid(torch.tensor(score)).item()
|
| 14 |
+
return round(similarity, 4)
|
| 15 |
|
| 16 |
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
| 17 |
+
gr.Markdown("## 🔎 Query–Document Relevance (CrossEncoder)")
|
| 18 |
+
gr.Markdown(
|
| 19 |
+
f"Model: **{MODEL_NAME}**\n\n"
|
| 20 |
+
"Scores are mapped with sigmoid to the range **0 (irrelevant) → 1 (highly relevant)**."
|
| 21 |
+
)
|
| 22 |
|
| 23 |
with gr.Row():
|
| 24 |
+
s1 = gr.Textbox(label="Query", placeholder="Enter your search query...")
|
| 25 |
+
s2 = gr.Textbox(label="Document Chunk", placeholder="Enter a document chunk...")
|
| 26 |
|
| 27 |
btn = gr.Button("Compute Similarity 🚀")
|
| 28 |
+
out = gr.Number(label="Relevance Score (0–1)")
|
| 29 |
|
| 30 |
btn.click(fn=predict_similarity, inputs=[s1, s2], outputs=out)
|
| 31 |
|
| 32 |
gr.Examples(
|
| 33 |
examples=[
|
| 34 |
+
["What is the capital of France?", "Paris is the capital city of France."],
|
| 35 |
+
["I am happy today", "I am feeling joyful and excited right now."],
|
| 36 |
+
["Python programming", "Bananas are yellow fruits."],
|
| 37 |
+
["Machine learning applications", "ML is widely used in healthcare and finance."],
|
| 38 |
],
|
| 39 |
inputs=[s1, s2],
|
| 40 |
)
|
| 41 |
|
| 42 |
+
demo.launch(enable_queue=True)
|