Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -16,12 +16,22 @@ JINA_ENDPOINT = "https://api.jina.ai/v1/rerank"
|
|
| 16 |
# -------------------------------
|
| 17 |
hf_model = CrossEncoder(HF_MODEL)
|
| 18 |
|
| 19 |
-
def
|
| 20 |
-
#
|
| 21 |
-
|
| 22 |
-
|
|
|
|
| 23 |
|
| 24 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
headers = {
|
| 26 |
"Authorization": f"Bearer {JINA_API_KEY}",
|
| 27 |
"Content-Type": "application/json",
|
|
@@ -29,27 +39,44 @@ def compare_models(query, doc):
|
|
| 29 |
payload = {
|
| 30 |
"model": JINA_MODEL,
|
| 31 |
"query": query,
|
| 32 |
-
"documents":
|
| 33 |
}
|
| 34 |
try:
|
| 35 |
r = requests.post(JINA_ENDPOINT, headers=headers, json=payload, timeout=20)
|
| 36 |
r.raise_for_status()
|
| 37 |
-
|
|
|
|
|
|
|
| 38 |
except Exception as e:
|
| 39 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 40 |
|
| 41 |
-
return
|
| 42 |
|
| 43 |
# -------------------------------
|
| 44 |
-
# Simple
|
| 45 |
# -------------------------------
|
| 46 |
with gr.Blocks() as demo:
|
| 47 |
-
gr.Markdown("### π Query
|
| 48 |
-
query = gr.Textbox(label="Query", lines=
|
| 49 |
-
|
| 50 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 51 |
|
| 52 |
-
btn = gr.Button("
|
| 53 |
-
btn.click(
|
| 54 |
|
| 55 |
demo.launch()
|
|
|
|
| 16 |
# -------------------------------
|
| 17 |
hf_model = CrossEncoder(HF_MODEL)
|
| 18 |
|
| 19 |
+
def rerank(query, docs_text):
|
| 20 |
+
# Split input documents (one per line)
|
| 21 |
+
docs = [d.strip() for d in docs_text.split("\n") if d.strip()]
|
| 22 |
+
if not docs:
|
| 23 |
+
return "β οΈ No documents provided."
|
| 24 |
|
| 25 |
+
# -------------------------------
|
| 26 |
+
# Hugging Face CrossEncoder Scores
|
| 27 |
+
# -------------------------------
|
| 28 |
+
hf_scores = hf_model.predict([(query, d) for d in docs])
|
| 29 |
+
hf_scores = [torch.sigmoid(torch.tensor(s)).item() for s in hf_scores]
|
| 30 |
+
hf_ranking = sorted(zip(docs, hf_scores), key=lambda x: x[1], reverse=True)
|
| 31 |
+
|
| 32 |
+
# -------------------------------
|
| 33 |
+
# Jina Reranker API Scores
|
| 34 |
+
# -------------------------------
|
| 35 |
headers = {
|
| 36 |
"Authorization": f"Bearer {JINA_API_KEY}",
|
| 37 |
"Content-Type": "application/json",
|
|
|
|
| 39 |
payload = {
|
| 40 |
"model": JINA_MODEL,
|
| 41 |
"query": query,
|
| 42 |
+
"documents": docs,
|
| 43 |
}
|
| 44 |
try:
|
| 45 |
r = requests.post(JINA_ENDPOINT, headers=headers, json=payload, timeout=20)
|
| 46 |
r.raise_for_status()
|
| 47 |
+
results = r.json()["results"]
|
| 48 |
+
jina_scores = [res["relevance_score"] for res in results]
|
| 49 |
+
jina_ranking = sorted(zip(docs, jina_scores), key=lambda x: x[1], reverse=True)
|
| 50 |
except Exception as e:
|
| 51 |
+
jina_ranking = [("Error", str(e))]
|
| 52 |
+
|
| 53 |
+
# -------------------------------
|
| 54 |
+
# Format output
|
| 55 |
+
# -------------------------------
|
| 56 |
+
out = "### Hugging Face Ranking\n"
|
| 57 |
+
for doc, score in hf_ranking:
|
| 58 |
+
out += f"- ({score:.4f}) {doc}\n"
|
| 59 |
+
|
| 60 |
+
out += "\n### Jina Reranker Ranking\n"
|
| 61 |
+
for doc, score in jina_ranking:
|
| 62 |
+
out += f"- ({score}) {doc}\n"
|
| 63 |
|
| 64 |
+
return out
|
| 65 |
|
| 66 |
# -------------------------------
|
| 67 |
+
# Simple UI
|
| 68 |
# -------------------------------
|
| 69 |
with gr.Blocks() as demo:
|
| 70 |
+
gr.Markdown("### π Query + Multiple Docs Reranking (HF vs Jina)")
|
| 71 |
+
query = gr.Textbox(label="Query", lines=2, placeholder="Enter your query here...")
|
| 72 |
+
docs = gr.Textbox(
|
| 73 |
+
label="Candidate Documents (one per line)",
|
| 74 |
+
lines=10,
|
| 75 |
+
placeholder="Paste multiple document chunks here, each on a new line..."
|
| 76 |
+
)
|
| 77 |
+
out = gr.Textbox(label="Ranked Results", lines=15)
|
| 78 |
|
| 79 |
+
btn = gr.Button("Rerank π")
|
| 80 |
+
btn.click(rerank, inputs=[query, docs], outputs=out)
|
| 81 |
|
| 82 |
demo.launch()
|