afriddev commited on
Commit
ec4ed0d
Β·
verified Β·
1 Parent(s): 7ec684a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +43 -16
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 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",
@@ -29,27 +39,44 @@ def compare_models(query, doc):
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()
 
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()