|
|
import gradio as gr |
|
|
import tweepy |
|
|
from groq import Groq |
|
|
import os |
|
|
|
|
|
api_key = os.getenv("Groqapi") |
|
|
|
|
|
|
|
|
def predict_fake_news(text): |
|
|
client = Groq(api_key= api_key) |
|
|
completion = client.chat.completions.create( |
|
|
model="llama-3.1-70b-versatile", |
|
|
messages=[ |
|
|
{ |
|
|
"role": "system", |
|
|
"content": "You are an experienced and up-to-date fact-checker who is very proficient in identifying falsehood and has a repertoire of knowledge of what real news is and what fake news is. You have over 30 years of experience in the field. I want you to analyse any news tweet entered and reply with ONLY one word 'Fake' if it is fake and 'Real' if it is real." |
|
|
}, |
|
|
{ |
|
|
"role": "user", |
|
|
"content": text |
|
|
} |
|
|
], |
|
|
temperature=1, |
|
|
max_tokens=8000, |
|
|
top_p=1, |
|
|
stream=True, |
|
|
stop=None, |
|
|
) |
|
|
|
|
|
|
|
|
prediction = "" |
|
|
for chunk in completion: |
|
|
prediction += chunk.choices[0].delta.content or "" |
|
|
|
|
|
return prediction.strip() |
|
|
|
|
|
|
|
|
def update_on_Twitter(tweet_text, prediction): |
|
|
|
|
|
CONSUMER_KEY = os.getenv("TwitterConsumer") |
|
|
CONSUMER_SECRET = os.getenv("ConsumerSecret") |
|
|
ACCESS_TOKEN = os.getenv("AccessToken") |
|
|
ACCESS_TOKEN_SECRET = os.getenv("AccTokenSecret") |
|
|
BEARER_TOKEN = os.getenv("BearerToken") |
|
|
|
|
|
|
|
|
auth = tweepy.OAuthHandler(CONSUMER_KEY, CONSUMER_SECRET) |
|
|
auth.set_access_token(ACCESS_TOKEN, ACCESS_TOKEN_SECRET) |
|
|
|
|
|
|
|
|
api = tweepy.API(auth) |
|
|
|
|
|
|
|
|
client = tweepy.Client( |
|
|
BEARER_TOKEN, |
|
|
CONSUMER_KEY, |
|
|
CONSUMER_SECRET, |
|
|
ACCESS_TOKEN, |
|
|
ACCESS_TOKEN_SECRET, |
|
|
wait_on_rate_limit=True |
|
|
) |
|
|
|
|
|
post_text = f"The news: {tweet_text} is {prediction}." |
|
|
|
|
|
try: |
|
|
api.verify_credentials() |
|
|
print("Authentication OK") |
|
|
client.create_tweet(text=post_text) |
|
|
return f'<a href="https://twitter.com/CANNBot" target="_blank">Detect Fake News on Twitter Bot Account</a>' |
|
|
except Exception as e: |
|
|
print(e) |
|
|
return f'Error: {e}' |
|
|
|
|
|
|
|
|
with gr.Blocks() as demo: |
|
|
gr.Markdown("# Fake News Detection that updates on X") |
|
|
text_input = gr.Textbox(placeholder="Enter a news Tweet here...", label="News Tweet") |
|
|
text_output = gr.Textbox(label="Prediction") |
|
|
link_output = gr.HTML(label="Twitter Bot Account") |
|
|
|
|
|
|
|
|
gr.Button("Detect").click(predict_fake_news, inputs=text_input, outputs=text_output) |
|
|
|
|
|
|
|
|
gr.Button("Detect on Twitter").click(update_on_Twitter, inputs=[text_input, text_output], outputs=link_output) |
|
|
|
|
|
|
|
|
demo.launch() |
|
|
|