import gradio as gr import os import requests import tempfile # Set this directly or via Environment Variables (safe for HF Spaces) HF_API_TOKEN = os.getenv("HF_TOKEN") if not HF_API_TOKEN: raise ValueError("HF_TOKEN environment variable is not set.") MODEL_ID = "google/flan-t5-base" API_URL = f"https://api-inference.huggingface.co/models/{MODEL_ID}" HEADERS = {"Authorization": f"Bearer {HF_API_TOKEN}"} def query_hf_api(prompt): try: payload = { "inputs": prompt, "parameters": {"max_new_tokens": 100} } response = requests.post(API_URL, headers=HEADERS, json=payload) response.raise_for_status() data = response.json() if isinstance(data, list) and "generated_text" in data[0]: return data[0]["generated_text"] else: return "[Error] Unexpected response format." except requests.exceptions.RequestException as e: return f"[Error] API request failed: {e}" def chat_fn(prompt, chat_history): response = query_hf_api(prompt) chat_history.append({"role": "user", "content": prompt}) chat_history.append({"role": "assistant", "content": response}) return chat_history, chat_history def save_chat(chat_history): try: with tempfile.NamedTemporaryFile(delete=False, suffix=".txt", mode="w", encoding="utf-8") as f: for entry in chat_history: f.write(f"{entry['role'].capitalize()}: {entry['content']}\n\n") return gr.File.update(value=f.name, visible=True) except Exception as e: return f"[Error] Failed to save chat: {e}" with gr.Blocks(theme=gr.themes.Monochrome()) as demo: gr.Markdown("# ๐Ÿค– Flan-T5 Chatbot (Free Coding/QA Model)") with gr.Row(): clear = gr.Button("๐Ÿงน Clear Chat") download_btn = gr.Button("โฌ‡๏ธ Download Chat") chat = gr.Chatbot(label="Chatbot", type="messages") msg = gr.Textbox(label="Your message", placeholder="Ask me something...") submit = gr.Button("๐Ÿš€ Send") history = gr.State([]) download_file = gr.File(label="Download", visible=False) submit.click(chat_fn, [msg, history], [chat, history]) msg.submit(chat_fn, [msg, history], [chat, history]) clear.click(lambda: ([], []), None, [chat, history]) download_btn.click(save_chat, [history], download_file) demo.launch()