afriddev commited on
Commit
4ffa42b
·
verified ·
1 Parent(s): 33e4eda

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +22 -14
app.py CHANGED
@@ -1,34 +1,42 @@
1
  import gradio as gr
2
  from sentence_transformers import CrossEncoder
 
3
 
4
- # Load a Semantic Textual Similarity CrossEncoder
5
- model = CrossEncoder("cross-encoder/stsb-roberta-large")
 
6
 
7
  def predict_similarity(s1, s2):
8
- score = model.predict([(s1, s2)])[0] # already continuous
9
- return round(float(score), 4)
 
 
 
10
 
11
  with gr.Blocks(theme=gr.themes.Soft()) as demo:
12
- gr.Markdown("## 🔎 Semantic Textual Similarity (CrossEncoder)")
13
- gr.Markdown("Score ranges 0 (unrelated) → 1 (identical meaning).")
 
 
 
14
 
15
  with gr.Row():
16
- s1 = gr.Textbox(label="Sentence 1", placeholder="Type first sentence...")
17
- s2 = gr.Textbox(label="Sentence 2", placeholder="Type second sentence...")
18
 
19
  btn = gr.Button("Compute Similarity 🚀")
20
- out = gr.Number(label="Similarity Score (0–1)")
21
 
22
  btn.click(fn=predict_similarity, inputs=[s1, s2], outputs=out)
23
 
24
  gr.Examples(
25
  examples=[
26
- ["I am a boy", "I am a girl"],
27
- ["I am happy today", "I am feeling a bit curious and joyful today"],
28
- ["I am going", "I am moving from you"],
29
- ["The sun is hot", "Ice cream is cold"],
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)