Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,71 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from transformers import AutoTokenizer, AutoModelForSequenceClassification
|
3 |
+
from sentence_transformers import SentenceTransformer, util
|
4 |
+
import torch
|
5 |
+
import torch.nn.functional as F
|
6 |
+
|
7 |
+
# Load SBERT model
|
8 |
+
sbert_model = SentenceTransformer("sentence-transformers/all-MiniLM-L6-v2")
|
9 |
+
|
10 |
+
# Load NLI model
|
11 |
+
nli_model_name = "tasksource/ModernBERT-base-nli"
|
12 |
+
nli_tokenizer = AutoTokenizer.from_pretrained(nli_model_name)
|
13 |
+
nli_model = AutoModelForSequenceClassification.from_pretrained(nli_model_name)
|
14 |
+
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
15 |
+
nli_model.to(device)
|
16 |
+
|
17 |
+
# SBERT function
|
18 |
+
def compute_similarity(text1, text2):
|
19 |
+
embeddings = sbert_model.encode([text1, text2], convert_to_tensor=True)
|
20 |
+
similarity = float(util.pytorch_cos_sim(embeddings[0], embeddings[1])[0])
|
21 |
+
|
22 |
+
interpretation = ""
|
23 |
+
if similarity > 0.9:
|
24 |
+
interpretation = "π’ Very High Similarity"
|
25 |
+
elif similarity > 0.75:
|
26 |
+
interpretation = "π‘ Moderate Similarity"
|
27 |
+
elif similarity > 0.5:
|
28 |
+
interpretation = "π Low Similarity"
|
29 |
+
else:
|
30 |
+
interpretation = "π΄ Very Low Similarity"
|
31 |
+
|
32 |
+
return round(similarity, 4), interpretation
|
33 |
+
|
34 |
+
# NLI function
|
35 |
+
def check_entail(premise, hypothesis):
|
36 |
+
inputs = nli_tokenizer(premise, hypothesis, return_tensors="pt", truncation=True, padding=True, max_length=512).to(device)
|
37 |
+
with torch.no_grad():
|
38 |
+
logits = nli_model(**inputs).logits
|
39 |
+
probs = torch.softmax(logits, dim=-1)[0]
|
40 |
+
label = ["entailment", "neutral", "contradiction"][torch.argmax(probs).item()]
|
41 |
+
return label, { "entailment": float(probs[0]), "neutral": float(probs[1]), "contradiction": float(probs[2]) }
|
42 |
+
|
43 |
+
def run_bi_direction(a, b):
|
44 |
+
res1 = check_entail(a, b)
|
45 |
+
res2 = check_entail(b, a)
|
46 |
+
return res1[0], res1[1], res2[0], res2[1]
|
47 |
+
|
48 |
+
# Build the interface
|
49 |
+
with gr.Blocks() as demo:
|
50 |
+
gr.Markdown("# βοΈ Essay Comparison Tool")
|
51 |
+
|
52 |
+
with gr.Tab("Semantic Similarity (SBERT)"):
|
53 |
+
a1 = gr.Textbox(label="Essay A", lines=8)
|
54 |
+
b1 = gr.Textbox(label="Essay B", lines=8)
|
55 |
+
sim_button = gr.Button("Compare Similarity")
|
56 |
+
sim_score = gr.Textbox(label="Cosine Similarity (0β1)")
|
57 |
+
sim_interpret = gr.Textbox(label="Interpretation")
|
58 |
+
sim_button.click(fn=compute_similarity, inputs=[a1, b1], outputs=[sim_score, sim_interpret])
|
59 |
+
|
60 |
+
with gr.Tab("Bidirectional Entailment (NLI)"):
|
61 |
+
a2 = gr.Textbox(label="Essay A (Original)", lines=8)
|
62 |
+
b2 = gr.Textbox(label="Essay B (Modified)", lines=8)
|
63 |
+
nli_button = gr.Button("Run Entailment Check")
|
64 |
+
ab_label = gr.Textbox(label="A β B Label")
|
65 |
+
ab_scores = gr.JSON(label="A β B Scores")
|
66 |
+
ba_label = gr.Textbox(label="B β A Label")
|
67 |
+
ba_scores = gr.JSON(label="B β A Scores")
|
68 |
+
nli_button.click(fn=run_bi_direction, inputs=[a2, b2], outputs=[ab_label, ab_scores, ba_label, ba_scores])
|
69 |
+
|
70 |
+
if __name__ == "__main__":
|
71 |
+
demo.launch()
|