Spaces:
Sleeping
Sleeping
Added gradio to app.py
Browse files
app.py
CHANGED
|
@@ -1,29 +1,23 @@
|
|
| 1 |
import os
|
| 2 |
-
|
| 3 |
os.environ["TF_CPP_MIN_LOG_LEVEL"] = "3"
|
|
|
|
| 4 |
from typing import List
|
| 5 |
import torch
|
| 6 |
from sentence_transformers import SentenceTransformer, util
|
| 7 |
from textblob import TextBlob
|
|
|
|
| 8 |
|
| 9 |
model = SentenceTransformer("paraphrase-MiniLM-L12-v2")
|
| 10 |
model.eval()
|
| 11 |
|
| 12 |
|
| 13 |
def calculate_semantic_similarity(
|
| 14 |
-
claim: str,
|
| 15 |
) -> float:
|
| 16 |
"""
|
| 17 |
-
|
| 18 |
-
Args:
|
| 19 |
-
claim (str): The claim to be verified.
|
| 20 |
-
sentences (List[str]): A list of sentences to check against the claim.
|
| 21 |
-
similarity_threshold (float, optional): The minimum similarity score for a
|
| 22 |
-
sentence to be considered "supporting". Defaults to 0.5.
|
| 23 |
-
|
| 24 |
-
Returns:
|
| 25 |
-
float: A weighted score between 0.0 and 1.0.
|
| 26 |
"""
|
|
|
|
| 27 |
if not sentences:
|
| 28 |
return 0.0
|
| 29 |
|
|
@@ -44,7 +38,6 @@ def calculate_semantic_similarity(
|
|
| 44 |
elif claim_sentiment * sentence_sentiment < 0:
|
| 45 |
similarity *= 0.9
|
| 46 |
|
| 47 |
-
print(f"Sentence: {sentence}\nSimilarity: {similarity:.2f}\n")
|
| 48 |
similarity = max(0.0, min(1.0, similarity))
|
| 49 |
all_scores.append(similarity)
|
| 50 |
|
|
@@ -54,35 +47,21 @@ def calculate_semantic_similarity(
|
|
| 54 |
if proportion_supporting >= 0.30:
|
| 55 |
final_score = sum(supporting_scores) / len(supporting_scores)
|
| 56 |
else:
|
| 57 |
-
|
| 58 |
-
# penalty = 0.80 # 20% reduction
|
| 59 |
-
final_score = average_all_scores # * penalty
|
| 60 |
|
| 61 |
-
return final_score
|
| 62 |
|
| 63 |
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
"
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
"This will surely lead to a stronger and more resilient economy.", # High similarity
|
| 76 |
-
"Financial experts have voiced concerns about the potential long-term consequences.", # Opposing sentiment
|
| 77 |
-
]
|
| 78 |
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
print(f"The final weighted support score for the claim is: {final_score:.2f}")
|
| 82 |
-
|
| 83 |
-
if final_score > 0.65:
|
| 84 |
-
print("Interpretation: The claim is strongly supported by the evidence. ✅")
|
| 85 |
-
elif final_score > 0.4:
|
| 86 |
-
print("Interpretation: The claim has moderate support from the evidence. 🤔")
|
| 87 |
-
else:
|
| 88 |
-
print("Interpretation: The claim has weak support from the evidence. ❌")
|
|
|
|
| 1 |
import os
|
|
|
|
| 2 |
os.environ["TF_CPP_MIN_LOG_LEVEL"] = "3"
|
| 3 |
+
|
| 4 |
from typing import List
|
| 5 |
import torch
|
| 6 |
from sentence_transformers import SentenceTransformer, util
|
| 7 |
from textblob import TextBlob
|
| 8 |
+
import gradio as gr
|
| 9 |
|
| 10 |
model = SentenceTransformer("paraphrase-MiniLM-L12-v2")
|
| 11 |
model.eval()
|
| 12 |
|
| 13 |
|
| 14 |
def calculate_semantic_similarity(
|
| 15 |
+
claim: str, sentences_input: str, similarity_threshold: float = 0.4
|
| 16 |
) -> float:
|
| 17 |
"""
|
| 18 |
+
Accepts a claim and newline-separated sentences. Returns a weighted similarity score.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
"""
|
| 20 |
+
sentences = [s.strip() for s in sentences_input.split("\n") if s.strip()]
|
| 21 |
if not sentences:
|
| 22 |
return 0.0
|
| 23 |
|
|
|
|
| 38 |
elif claim_sentiment * sentence_sentiment < 0:
|
| 39 |
similarity *= 0.9
|
| 40 |
|
|
|
|
| 41 |
similarity = max(0.0, min(1.0, similarity))
|
| 42 |
all_scores.append(similarity)
|
| 43 |
|
|
|
|
| 47 |
if proportion_supporting >= 0.30:
|
| 48 |
final_score = sum(supporting_scores) / len(supporting_scores)
|
| 49 |
else:
|
| 50 |
+
final_score = sum(all_scores) / len(all_scores)
|
|
|
|
|
|
|
| 51 |
|
| 52 |
+
return round(final_score, 4)
|
| 53 |
|
| 54 |
|
| 55 |
+
iface = gr.Interface(
|
| 56 |
+
fn=calculate_semantic_similarity,
|
| 57 |
+
inputs=[
|
| 58 |
+
gr.Textbox(label="Claim"),
|
| 59 |
+
gr.Textbox(lines=10, label="Evidence Sentences (one per line)"),
|
| 60 |
+
gr.Slider(minimum=0.0, maximum=1.0, step=0.05, value=0.4, label="Similarity Threshold")
|
| 61 |
+
],
|
| 62 |
+
outputs=gr.Number(label="Final Weighted Support Score"),
|
| 63 |
+
title="Claim Support Checker",
|
| 64 |
+
description="Input a claim and evidence sentences to calculate how strongly the evidence supports the claim."
|
| 65 |
+
)
|
|
|
|
|
|
|
|
|
|
| 66 |
|
| 67 |
+
iface.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|