pravjet commited on
Commit
95de976
·
verified ·
1 Parent(s): 3868de3

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +31 -5
app.py CHANGED
@@ -4,6 +4,7 @@ import torch
4
  import requests
5
  from bs4 import BeautifulSoup
6
  import matplotlib.pyplot as plt
 
7
  import os
8
 
9
  # Load model and tokenizer
@@ -11,11 +12,13 @@ model_name = "mrm8488/bert-tiny-finetuned-fake-news-detection"
11
  tokenizer = AutoTokenizer.from_pretrained(model_name)
12
  model = AutoModelForSequenceClassification.from_pretrained(model_name)
13
 
14
- # Initialize verdict counters
15
  verdict_counts = {"Authentic": 0, "Possibly Misinformation": 0}
16
 
17
- # Read API key from environment variable
18
  FACT_CHECK_API_KEY = os.getenv("FACT_CHECK_API_KEY")
 
 
19
 
20
  def extract_text_from_url(url):
21
  try:
@@ -64,11 +67,28 @@ def fact_check_google_api(query, api_key):
64
  except Exception as e:
65
  return f"Error calling Fact Check API: {e}"
66
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
67
  def detect_misinformation(input_text, input_type):
68
  if input_type == "URL":
69
  input_text = extract_text_from_url(input_text)
70
  if input_text.startswith("Error"):
71
- return input_text, "Error", 0.0, update_chart(), "URL extraction failed."
72
 
73
  inputs = tokenizer(input_text, return_tensors="pt", truncation=True, padding=True)
74
  with torch.no_grad():
@@ -78,7 +98,8 @@ def detect_misinformation(input_text, input_type):
78
  verdict = "Possibly Misinformation" if score > 0.5 else "Authentic"
79
  verdict_counts[verdict] += 1
80
  fact_check_result = fact_check_google_api(input_text, FACT_CHECK_API_KEY)
81
- return input_text[:1000], verdict, round(score * 100, 2), update_chart(), fact_check_result
 
82
 
83
  with gr.Blocks() as demo:
84
  gr.Markdown("## 🧠 Misinformation Detection Dashboard")
@@ -93,6 +114,7 @@ with gr.Blocks() as demo:
93
  score = gr.Label(label="Authenticity Score (%)")
94
  chart = gr.Plot(label="Analytics Dashboard")
95
  fact_check = gr.Textbox(label="Fact Check Results", lines=6)
 
96
 
97
  btn = gr.Button("Analyze")
98
 
@@ -104,6 +126,10 @@ with gr.Blocks() as demo:
104
  mode = "Text"
105
  return detect_misinformation(text, mode)
106
 
107
- btn.click(fn=handle_input, inputs=[input_text, input_type], outputs=[output_text, verdict, score, chart, fact_check])
 
 
 
 
108
 
109
  demo.launch()
 
4
  import requests
5
  from bs4 import BeautifulSoup
6
  import matplotlib.pyplot as plt
7
+ import openai
8
  import os
9
 
10
  # Load model and tokenizer
 
12
  tokenizer = AutoTokenizer.from_pretrained(model_name)
13
  model = AutoModelForSequenceClassification.from_pretrained(model_name)
14
 
15
+ # Verdict counters
16
  verdict_counts = {"Authentic": 0, "Possibly Misinformation": 0}
17
 
18
+ # API keys
19
  FACT_CHECK_API_KEY = os.getenv("FACT_CHECK_API_KEY")
20
+ OPENAI_API_KEY = os.getenv("OPENAI_API_KEY")
21
+ openai.api_key = OPENAI_API_KEY
22
 
23
  def extract_text_from_url(url):
24
  try:
 
67
  except Exception as e:
68
  return f"Error calling Fact Check API: {e}"
69
 
70
+ def gpt_fact_check(prompt):
71
+ if not OPENAI_API_KEY:
72
+ return "OpenAI API key not found. Please set OPENAI_API_KEY in environment."
73
+ try:
74
+ response = openai.ChatCompletion.create(
75
+ model="gpt-3.5-turbo", # or "gpt-4" if you have access
76
+ messages=[
77
+ {"role": "system", "content": "You are a helpful assistant for fact-checking news articles. Analyze the following content for misinformation, summarize the main claim, and explain your reasoning."},
78
+ {"role": "user", "content": prompt}
79
+ ],
80
+ max_tokens=300,
81
+ temperature=0.2,
82
+ )
83
+ return response['choices'][0]['message']['content'].strip()
84
+ except Exception as e:
85
+ return f"OpenAI API error: {e}"
86
+
87
  def detect_misinformation(input_text, input_type):
88
  if input_type == "URL":
89
  input_text = extract_text_from_url(input_text)
90
  if input_text.startswith("Error"):
91
+ return input_text, "Error", 0.0, update_chart(), "URL extraction failed.", ""
92
 
93
  inputs = tokenizer(input_text, return_tensors="pt", truncation=True, padding=True)
94
  with torch.no_grad():
 
98
  verdict = "Possibly Misinformation" if score > 0.5 else "Authentic"
99
  verdict_counts[verdict] += 1
100
  fact_check_result = fact_check_google_api(input_text, FACT_CHECK_API_KEY)
101
+ gpt_result = gpt_fact_check(input_text)
102
+ return input_text[:1000], verdict, round(score * 100, 2), update_chart(), fact_check_result, gpt_result
103
 
104
  with gr.Blocks() as demo:
105
  gr.Markdown("## 🧠 Misinformation Detection Dashboard")
 
114
  score = gr.Label(label="Authenticity Score (%)")
115
  chart = gr.Plot(label="Analytics Dashboard")
116
  fact_check = gr.Textbox(label="Fact Check Results", lines=6)
117
+ gpt_fact = gr.Textbox(label="OpenAI GPT Analysis", lines=6)
118
 
119
  btn = gr.Button("Analyze")
120
 
 
126
  mode = "Text"
127
  return detect_misinformation(text, mode)
128
 
129
+ btn.click(
130
+ fn=handle_input,
131
+ inputs=[input_text, input_type],
132
+ outputs=[output_text, verdict, score, chart, fact_check, gpt_fact]
133
+ )
134
 
135
  demo.launch()