Spaces:
Sleeping
Sleeping
| import gradio as gr | |
| import requests | |
| import os | |
| HF_API_TOKEN = os.getenv("HF_API_TOKEN") | |
| API_URL = "https://api-inference.huggingface.co/models/HuggingFaceH4/zephyr-7b-beta" | |
| headers = {"Authorization": f"Bearer {HF_API_TOKEN}"} | |
| def query(payload): | |
| try: | |
| response = requests.post(API_URL, headers=headers, json=payload) | |
| response.raise_for_status() | |
| return response.json() | |
| except Exception as e: | |
| return [{"generated_text": f"Error: {str(e)}"}] | |
| def classify_news(chat_history, user_input): | |
| if not user_input or not user_input.strip(): | |
| return chat_history, "" | |
| prompt = ( | |
| f"You are a fake news classifier. Respond with 'Answer: Real' or 'Answer: Fake'.\n" | |
| f"Statement: {user_input}" | |
| ) | |
| output = query({ | |
| "inputs": prompt, | |
| "parameters": { | |
| "max_new_tokens": 10, | |
| "temperature": 0.2, | |
| "do_sample": False | |
| } | |
| }) | |
| try: | |
| model_output = output[0]["generated_text"] | |
| # Robust parsing -- finds "Answer: Real" or "Answer: Fake" | |
| if "Answer:" in model_output: | |
| result = model_output.split("Answer:")[-1].strip().split()[0] | |
| else: | |
| result = model_output.strip().split()[0] | |
| if result.lower().startswith("real"): | |
| result = "π’ Real" | |
| elif result.lower().startswith("fake"): | |
| result = "π΄ Fake" | |
| else: | |
| result = "β οΈ Unclear" | |
| except Exception: | |
| result = "β Error: Could not get a response." | |
| chat_history = chat_history or [] | |
| chat_history.append({"role": "user", "content": user_input}) | |
| chat_history.append({"role": "assistant", "content": result}) | |
| return chat_history, "" | |
| def export_chat(chat_history): | |
| export_text = "" | |
| for message in chat_history: | |
| role = "User" if message["role"] == "user" else "Model" | |
| export_text += f"{role}: {message['content']}\n" | |
| with open("chat_log.txt", "w") as f: | |
| f.write(export_text.strip()) | |
| return "chat_log.txt" | |
| with gr.Blocks(theme=gr.themes.Soft(), css="""...""") as demo: | |
| gr.Markdown(""" | |
| # π° Fake News Detector Chat | |
| _Classify news as **Real** or **Fake** using a GPT-style model._ | |
| """) | |
| chatbot = gr.Chatbot(type="messages", label="π§ Chatbot", elem_classes=["chatbot"]) | |
| with gr.Row(): | |
| user_input = gr.Textbox(placeholder="Type or paste a news statement here...", scale=6) | |
| clear_btn = gr.Button("π§Ή Clear", elem_id="clear-button") | |
| submit_btn = gr.Button("π Classify", elem_id="submit-button", scale=2) | |
| with gr.Row(): | |
| audio = gr.Audio(type="filepath", label="π€ Speak News", interactive=True) | |
| export_btn = gr.Button("β¬οΈ Export Chat") | |
| toggle_dark_mode = gr.Checkbox(label="π Toggle Dark Mode", value=True) | |
| submit_btn.click(classify_news, inputs=[chatbot, user_input], outputs=[chatbot, user_input]) | |
| clear_btn.click(lambda: ([], ""), None, [chatbot, user_input]) | |
| export_btn.click(export_chat, inputs=[chatbot], outputs=gr.File(label="Download Log")) | |
| demo.launch() | |