Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,43 +1,55 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
from sentence_transformers import CrossEncoder
|
| 3 |
import torch
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
)
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 26 |
|
| 27 |
btn = gr.Button("Compute Similarity ๐")
|
| 28 |
-
|
| 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 |
-
# ๐ Launch without enable_queue (new Gradio)
|
| 43 |
demo.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
from sentence_transformers import CrossEncoder
|
| 3 |
import torch
|
| 4 |
+
import requests
|
| 5 |
+
|
| 6 |
+
# -------------------------------
|
| 7 |
+
# CONFIG
|
| 8 |
+
# -------------------------------
|
| 9 |
+
HF_MODEL = "cross-encoder/ms-marco-MiniLM-L-12-v2"
|
| 10 |
+
JINA_MODEL = "jina-reranker-m0"
|
| 11 |
+
JINA_API_KEY = "jina_4075150fa702471c85ddea0a9ad4b306ouE7ymhrCpvxTxX3mScUv5LLDPKQ"
|
| 12 |
+
JINA_ENDPOINT = "https://api.jina.ai/v1/rerank"
|
| 13 |
+
|
| 14 |
+
# -------------------------------
|
| 15 |
+
# Load Hugging Face CrossEncoder
|
| 16 |
+
# -------------------------------
|
| 17 |
+
hf_model = CrossEncoder(HF_MODEL)
|
| 18 |
+
|
| 19 |
+
def compare_models(query, doc):
|
| 20 |
+
# Hugging Face score
|
| 21 |
+
raw_score = hf_model.predict([(query, doc)])[0]
|
| 22 |
+
hf_score = torch.sigmoid(torch.tensor(raw_score)).item()
|
| 23 |
+
|
| 24 |
+
# Jina reranker score
|
| 25 |
+
headers = {
|
| 26 |
+
"Authorization": f"Bearer {JINA_API_KEY}",
|
| 27 |
+
"Content-Type": "application/json",
|
| 28 |
+
}
|
| 29 |
+
payload = {
|
| 30 |
+
"model": JINA_MODEL,
|
| 31 |
+
"query": query,
|
| 32 |
+
"documents": [doc],
|
| 33 |
+
}
|
| 34 |
+
try:
|
| 35 |
+
r = requests.post(JINA_ENDPOINT, headers=headers, json=payload, timeout=20)
|
| 36 |
+
r.raise_for_status()
|
| 37 |
+
jina_score = r.json()["results"][0]["relevance_score"]
|
| 38 |
+
except Exception as e:
|
| 39 |
+
jina_score = f"Error: {str(e)}"
|
| 40 |
+
|
| 41 |
+
return f"Hugging Face ({HF_MODEL}): {round(hf_score,4)}\nJina ({JINA_MODEL}): {jina_score}"
|
| 42 |
+
|
| 43 |
+
# -------------------------------
|
| 44 |
+
# Simple Lite UI
|
| 45 |
+
# -------------------------------
|
| 46 |
+
with gr.Blocks() as demo:
|
| 47 |
+
gr.Markdown("### ๐ Query vs Document Similarity (HF vs Jina)")
|
| 48 |
+
query = gr.Textbox(label="Query", lines=3, placeholder="Paste your query here...")
|
| 49 |
+
doc = gr.Textbox(label="Document Chunk", lines=6, placeholder="Paste your document chunk here...")
|
| 50 |
+
out = gr.Textbox(label="Scores", lines=3)
|
| 51 |
|
| 52 |
btn = gr.Button("Compute Similarity ๐")
|
| 53 |
+
btn.click(compare_models, inputs=[query, doc], outputs=out)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 54 |
|
|
|
|
| 55 |
demo.launch()
|