import gradio as gr from huggingface_hub import InferenceClient import os # 🔑 Load Hugging Face API Token HF_API_TOKEN = os.getenv("HF_API_TOKEN") # Initialize Hugging Face model client with authentication client = InferenceClient("HuggingFaceH4/zephyr-7b-beta", token=HF_API_TOKEN) # System message setting the chatbot's behavior SYSTEM_MESSAGE = ( "You are a helpful Assistant. You let the user know if the information they share with you is true or false. " "You return the actual facts behind the information given, after stating the state of Truth or False." ) # Function to handle chatbot responses def respond(message, history): if history is None: history = [] messages = [{"role": "system", "content": SYSTEM_MESSAGE}] for user, bot in history: messages.append({"role": "user", "content": user}) messages.append({"role": "assistant", "content": bot}) messages.append({"role": "user", "content": message}) # ✅ Ensure streaming is disabled to prevent looping response = client.chat_completion(messages, max_tokens=200, temperature=0.7, stream=False) # ✅ Extract response only once bot_reply = response.choices[0].message.content.strip() # ✅ Append correctly formatted tuple to history history.append((message, bot_reply)) # ✅ Return chatbot history and state return history, history # Create Gradio chatbot UI demo = gr.Interface( fn=respond, inputs=["text", "state"], # ✅ Pass state properly outputs=["chatbot", "state"], # ✅ Ensure state is included in outputs title="Misinformation Detection Chatbot", description="Ask anything, and the chatbot will verify whether it's true or false.", live=True, ) if __name__ == "__main__": demo.launch()