isana25 commited on
Commit
980d93e
·
verified ·
1 Parent(s): 30e5974

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +8 -10
app.py CHANGED
@@ -19,14 +19,13 @@ model.eval()
19
  clip_model = CLIPModel.from_pretrained("openai/clip-vit-base-patch32")
20
  clip_processor = CLIPProcessor.from_pretrained("openai/clip-vit-base-patch32")
21
 
22
- # ==== Initialize LIME Explainer ====
23
  explainer = LimeTextExplainer(class_names=["Real", "Fake"])
24
 
25
- # ==== Groq Client Setup ====
26
- groq_api_key = os.getenv("Groq_Api_key", "your_groq_api_key_here") # Use environment variable or replace manually
27
- groq_client = Groq(api_key=groq_api_key)
28
 
29
- # ==== Helper: Classify News with BERT ====
30
  def classify_news(text):
31
  inputs = tokenizer(text, return_tensors="pt", truncation=True, padding=True)
32
  with torch.no_grad():
@@ -34,17 +33,17 @@ def classify_news(text):
34
  probs = torch.nn.functional.softmax(outputs.logits, dim=-1)
35
  return probs[0].tolist()
36
 
37
- # ==== Helper: Explain with LIME ====
38
  def explain_prediction(text):
39
  def predictor(texts):
40
  return [classify_news(t) for t in texts]
41
  explanation = explainer.explain_instance(text, predictor, num_features=6)
42
  return explanation.as_list()
43
 
44
- # ==== Helper: Groq Second Opinion ====
45
  def get_llm_opinion(text):
46
  try:
47
- response = groq_client.chat.completions.create(
48
  model="mixtral-8x7b-32768",
49
  messages=[
50
  {"role": "system", "content": "You are a helpful assistant that detects fake news based on user input."},
@@ -55,7 +54,7 @@ def get_llm_opinion(text):
55
  except Exception as e:
56
  return f"LLM Error: {str(e)}"
57
 
58
- # ==== Helper: Image Verification via CLIP ====
59
  def verify_image_with_text(image, text):
60
  if image is None:
61
  return "No image uploaded."
@@ -77,7 +76,6 @@ def analyze_news(text, image=None):
77
  llm_verdict = get_llm_opinion(text)
78
  img_verification = verify_image_with_text(image, text)
79
 
80
- # JSON Report
81
  report = {
82
  "model_prediction": model_label,
83
  "bert_probs": {"Real": round(prediction[0], 3), "Fake": round(prediction[1], 3)},
 
19
  clip_model = CLIPModel.from_pretrained("openai/clip-vit-base-patch32")
20
  clip_processor = CLIPProcessor.from_pretrained("openai/clip-vit-base-patch32")
21
 
22
+ # ==== LIME Explainer ====
23
  explainer = LimeTextExplainer(class_names=["Real", "Fake"])
24
 
25
+ # ==== Groq Client ====
26
+ client = Groq(api_key=os.environ.get("GROQ_API_KEY"))
 
27
 
28
+ # ==== News Classification ====
29
  def classify_news(text):
30
  inputs = tokenizer(text, return_tensors="pt", truncation=True, padding=True)
31
  with torch.no_grad():
 
33
  probs = torch.nn.functional.softmax(outputs.logits, dim=-1)
34
  return probs[0].tolist()
35
 
36
+ # ==== LIME Explanation ====
37
  def explain_prediction(text):
38
  def predictor(texts):
39
  return [classify_news(t) for t in texts]
40
  explanation = explainer.explain_instance(text, predictor, num_features=6)
41
  return explanation.as_list()
42
 
43
+ # ==== LLM Second Opinion ====
44
  def get_llm_opinion(text):
45
  try:
46
+ response = client.chat.completions.create(
47
  model="mixtral-8x7b-32768",
48
  messages=[
49
  {"role": "system", "content": "You are a helpful assistant that detects fake news based on user input."},
 
54
  except Exception as e:
55
  return f"LLM Error: {str(e)}"
56
 
57
+ # ==== CLIP Image-Text Similarity ====
58
  def verify_image_with_text(image, text):
59
  if image is None:
60
  return "No image uploaded."
 
76
  llm_verdict = get_llm_opinion(text)
77
  img_verification = verify_image_with_text(image, text)
78
 
 
79
  report = {
80
  "model_prediction": model_label,
81
  "bert_probs": {"Real": round(prediction[0], 3), "Fake": round(prediction[1], 3)},