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'Detect Fake News on Twitter Bot Account' 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()