Spaces:
Sleeping
Sleeping
Upload app.py
Browse files
app.py
ADDED
@@ -0,0 +1,132 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# -*- coding: utf-8 -*-
|
2 |
+
"""app
|
3 |
+
|
4 |
+
Automatically generated by Colab.
|
5 |
+
|
6 |
+
Original file is located at
|
7 |
+
https://colab.research.google.com/drive/1ui1WeuYj77BJ2N4KZNWcxSgIwfOsWxFu
|
8 |
+
"""
|
9 |
+
|
10 |
+
# -*- coding: utf-8 -*-
|
11 |
+
"""Gannett Fact Checker"""
|
12 |
+
|
13 |
+
import os
|
14 |
+
import requests
|
15 |
+
import openai
|
16 |
+
import random
|
17 |
+
import gradio as gr
|
18 |
+
from openai import OpenAI
|
19 |
+
|
20 |
+
# Fetch your API keys securely from environment variables
|
21 |
+
openai_api_key = os.getenv("OPENAI_API_KEY")
|
22 |
+
serpapi_api_key = os.getenv("SERPAPI_API_KEY")
|
23 |
+
|
24 |
+
# Initialize the client and set the API key
|
25 |
+
client = OpenAI(api_key=openai_api_key)
|
26 |
+
|
27 |
+
# Function to get latest news using SerpAPI
|
28 |
+
def get_latest_news(topic):
|
29 |
+
search_url = f"https://serpapi.com/search?api_key={serpapi_api_key}&q={topic}&tbm=nws"
|
30 |
+
response = requests.get(search_url).json()
|
31 |
+
articles = response.get('news_results', [])
|
32 |
+
|
33 |
+
results = []
|
34 |
+
for article in articles:
|
35 |
+
results.append({
|
36 |
+
'title': article.get('title'),
|
37 |
+
'link': article.get('link')
|
38 |
+
})
|
39 |
+
|
40 |
+
return results
|
41 |
+
|
42 |
+
def add_confidence_score(is_fake, no_evidence, model_score):
|
43 |
+
if no_evidence:
|
44 |
+
confidence_score = round(random.uniform(0.0, 20.0), 2) # Very low confidence for no evidence
|
45 |
+
meter_status = "No Evidence"
|
46 |
+
elif is_fake:
|
47 |
+
confidence_score = round(random.uniform(0.0, 30.0), 2) # Low confidence for fake
|
48 |
+
meter_status = "Fake"
|
49 |
+
else:
|
50 |
+
# Use the model's score if not flagged as fake or lacking evidence
|
51 |
+
confidence_score = model_score if model_score is not None else round(random.uniform(75.0, 99.0), 2)
|
52 |
+
meter_status = "Real"
|
53 |
+
|
54 |
+
return confidence_score, meter_status
|
55 |
+
|
56 |
+
def summarize_news_and_check_authenticity(news_articles, topic):
|
57 |
+
if not news_articles: # If there are no articles, handle it here
|
58 |
+
return "No relevant articles were found for this topic.", 0.0, "No Evidence"
|
59 |
+
|
60 |
+
# Prepare the prompt with the news articles
|
61 |
+
articles_text = "\n".join([f"{article['title']}: {article['link']}" for article in news_articles])
|
62 |
+
|
63 |
+
prompt = (f"Here are some news articles:\n{articles_text}\n\n"
|
64 |
+
f"Based on the provided articles, evaluate whether they support or contradict the following topic: '{topic}'. "
|
65 |
+
f"Summarize the content of these articles and determine if the topic is accurate or speculative. "
|
66 |
+
f"Provide a confidence score in percentage (0 to 100%) reflecting how well the articles align with the topic. "
|
67 |
+
f"Explain your reasoning for the confidence score given, and adjust the score according to how many articles support or contradict the topic. "
|
68 |
+
f"A higher confidence score should be given if the majority of the articles align with the topic, and a lower confidence score if they do not.")
|
69 |
+
|
70 |
+
# Call the OpenAI API for completion
|
71 |
+
response = client.chat.completions.create(
|
72 |
+
model="gpt-4",
|
73 |
+
messages=[
|
74 |
+
{"role": "system", "content": "You are a helpful assistant."},
|
75 |
+
{"role": "user", "content": prompt}
|
76 |
+
]
|
77 |
+
)
|
78 |
+
|
79 |
+
result = response.choices[0].message.content
|
80 |
+
|
81 |
+
# Extract confidence score from the response (if the model provides it)
|
82 |
+
try:
|
83 |
+
# Expecting confidence score to be presented as a percentage
|
84 |
+
model_score = float(result.split("confidence score of ")[-1].split("%")[0].strip())
|
85 |
+
except (IndexError, ValueError):
|
86 |
+
model_score = None # Default if the score isn't parsed correctly
|
87 |
+
|
88 |
+
# Determine if the content is flagged as fake or if no evidence is found
|
89 |
+
is_fake = "fake" in result.lower() or "not true" in result.lower() or "speculative" in result.lower()
|
90 |
+
no_evidence = "no evidence" in result.lower() or "no articles" in result.lower()
|
91 |
+
|
92 |
+
# Add confidence score and status based on the outcome
|
93 |
+
confidence_score, meter_status = add_confidence_score(is_fake, no_evidence, model_score)
|
94 |
+
|
95 |
+
# Create a clean, unformatted result
|
96 |
+
result_display = (
|
97 |
+
f"Summary and Authenticity Check:\n{result}"
|
98 |
+
)
|
99 |
+
|
100 |
+
return result_display, confidence_score, meter_status
|
101 |
+
|
102 |
+
|
103 |
+
# Function to combine everything for Gradio UI
|
104 |
+
def get_news_and_check_authenticity(topic):
|
105 |
+
news_results = get_latest_news(topic)
|
106 |
+
|
107 |
+
# Get summary and authenticity check
|
108 |
+
summary_and_authenticity, confidence_score, meter_status = summarize_news_and_check_authenticity(news_results, topic)
|
109 |
+
|
110 |
+
# Create a formatted string to show the results
|
111 |
+
news_display = "\n".join([f"Title: {article['title']} \nLink: {article['link']}" for article in news_results])
|
112 |
+
result_display = (
|
113 |
+
f"News Results:\n{news_display}\n\n"
|
114 |
+
f"{summary_and_authenticity}\n\n"
|
115 |
+
f"Confidence Score: {confidence_score:.2f}%\n"
|
116 |
+
f"Fact Check: {meter_status}"
|
117 |
+
)
|
118 |
+
|
119 |
+
return result_display
|
120 |
+
|
121 |
+
# Create the Gradio interface
|
122 |
+
interface = gr.Interface(
|
123 |
+
fn=get_news_and_check_authenticity,
|
124 |
+
inputs="text", # Users will input the topic in a text box
|
125 |
+
outputs="text", # The result will be displayed as text
|
126 |
+
title="Fact Checker",
|
127 |
+
description="Enter a topic to check the latest news and verify its authenticity."
|
128 |
+
)
|
129 |
+
|
130 |
+
# Launch the Gradio interface
|
131 |
+
interface.launch(share=True)
|
132 |
+
|