afriddev commited on
Commit
063bf3b
Β·
verified Β·
1 Parent(s): 28d504e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +21 -11
app.py CHANGED
@@ -1,19 +1,29 @@
1
  import gradio as gr
2
  from sentence_transformers import CrossEncoder
3
 
4
- # Load CrossEncoder model
5
- model = CrossEncoder("cross-encoder/nli-deberta-v3-base")
6
 
7
  def predict_similarity(sentence1, sentence2):
8
- score = model.predict([(sentence1, sentence2)]) # returns numpy array
9
- return float(score.squeeze()[0]) # βœ… safe extraction of single float
 
 
 
10
 
11
- demo = gr.Interface(
12
- fn=predict_similarity,
13
- inputs=["text", "text"],
14
- outputs="number",
15
- title="CrossEncoder (nli-deberta-v3-base)",
16
- description="Enter two sentences to compute semantic similarity."
17
- )
 
 
 
 
 
 
 
18
 
19
  demo.launch()
 
1
  import gradio as gr
2
  from sentence_transformers import CrossEncoder
3
 
4
+ # Load CrossEncoder with 3 labels
5
+ model = CrossEncoder("cross-encoder/nli-deberta-v3-base", num_labels=3)
6
 
7
  def predict_similarity(sentence1, sentence2):
8
+ # Get probabilities for contradiction, neutral, entailment
9
+ probs = model.predict([(sentence1, sentence2)], apply_softmax=True)[0]
10
+ # Similarity = P(neutral) + P(entailment)
11
+ similarity = float(probs[1] + probs[2])
12
+ return round(similarity, 4) # cleaner output
13
 
14
+ # UI layout
15
+ with gr.Blocks(theme=gr.themes.Soft()) as demo:
16
+ gr.Markdown("## πŸ€– CrossEncoder NLI β†’ Semantic Similarity")
17
+ gr.Markdown("Enter two sentences to compute similarity. "
18
+ "Score = P(neutral)+P(entailment). Range [0–1].")
19
+
20
+ with gr.Row():
21
+ s1 = gr.Textbox(label="Sentence 1", placeholder="Type first sentence...")
22
+ s2 = gr.Textbox(label="Sentence 2", placeholder="Type second sentence...")
23
+
24
+ btn = gr.Button("Compute Similarity πŸš€")
25
+ out = gr.Number(label="Similarity Score (0-1)")
26
+
27
+ btn.click(fn=predict_similarity, inputs=[s1, s2], outputs=out)
28
 
29
  demo.launch()