File size: 2,382 Bytes
115594b
d8854e3
 
 
 
ba7bcae
d8854e3
 
ba7bcae
d8854e3
e2ae74a
d8854e3
 
 
ba7bcae
d8854e3
 
ba7bcae
 
 
 
 
 
d8854e3
 
 
ba7bcae
 
d8854e3
 
 
ba7bcae
d8854e3
 
 
 
 
 
ba7bcae
d8854e3
 
 
 
 
 
 
 
 
ba7bcae
d8854e3
ba7bcae
d8854e3
 
 
 
 
ba7bcae
d8854e3
 
 
 
 
 
 
 
 
 
 
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
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()