aubynsamuel05 commited on
Commit
b372940
·
verified ·
1 Parent(s): e974ba7

Added gradio to app.py

Browse files
Files changed (1) hide show
  1. app.py +19 -40
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, sentences: List[str], similarity_threshold: float = 0.4
15
  ) -> float:
16
  """
17
- Calculates a weighted score representing how well a list of sentences supports a claim.
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
- average_all_scores = sum(all_scores) / len(all_scores)
58
- # penalty = 0.80 # 20% reduction
59
- final_score = average_all_scores # * penalty
60
 
61
- return final_score
62
 
63
 
64
- if __name__ == "__main__":
65
- claim_to_verify = "The new government policy is expected to boost the economy."
66
- evidence_sentences = [
67
- "The recent legislation is projected to stimulate significant economic growth.", # High similarity
68
- "Market analysts are optimistic about the financial future following the announcement.", # High similarity
69
- "However, some critics argue that the policy might lead to unforeseen inflation.", # Low similarity
70
- "The stock market reacted positively, showing a slight increase.", # Medium similarity
71
- "This is considered a poor decision for the nation's financial stability by some experts.", # Opposing sentiment
72
- "The primary goal of the initiative is to create jobs and encourage consumer spending.", # High similarity
73
- "Unemployment rates are expected to decline in the coming months.", # High similarity
74
- "There has been some public disapproval regarding the policy's rollout.", # Low similarity
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
- final_score = calculate_semantic_similarity(claim_to_verify, evidence_sentences)
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()