Spaces:
Sleeping
Sleeping
File size: 3,110 Bytes
58e746a a402109 58e746a a402109 58e746a a402109 58e746a a402109 58e746a a402109 ed04ebb 58e746a a402109 23b191f 58e746a a402109 58e746a a402109 ed04ebb 58e746a a402109 58e746a a402109 ed04ebb 58e746a 709eb0a 58e746a a402109 8cf8406 58e746a a402109 58e746a a5e2e1e a402109 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 | import gradio as gr
import tweepy
from groq import Groq
import os
api_key = os.getenv("Groqapi")
# Define the Groq-based function to predict fake news
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,
)
# Iterate over the streaming response to get the result
prediction = ""
for chunk in completion:
prediction += chunk.choices[0].delta.content or ""
return prediction.strip() # Return the result (Fake or Real)
# Define a function to update on Twitter
def update_on_Twitter(tweet_text, prediction):
# Replace with your own Twitter API credentials
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")
# Authenticate to Twitter
auth = tweepy.OAuthHandler(CONSUMER_KEY, CONSUMER_SECRET)
auth.set_access_token(ACCESS_TOKEN, ACCESS_TOKEN_SECRET)
# Create an API object
api = tweepy.API(auth)
# Create a Client object for posting tweets
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}'
# Use Gradio Blocks to create a more flexible interface
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")
# Button to get prediction using Groq LLM
gr.Button("Detect").click(predict_fake_news, inputs=text_input, outputs=text_output)
# Button to generate a Gradio link and post to Twitter
gr.Button("Detect on Twitter").click(update_on_Twitter, inputs=[text_input, text_output], outputs=link_output)
# Launch the interface
demo.launch()
|