File size: 2,344 Bytes
5f90001
8713898
 
99dffda
8713898
6eb6b73
99dffda
6eb6b73
 
 
8713898
 
 
 
 
6eb6b73
 
 
 
 
 
 
 
 
8713898
 
 
99dffda
 
8713898
 
99dffda
6eb6b73
 
 
 
 
 
 
8713898
 
99dffda
6eb6b73
8713898
 
99dffda
6eb6b73
99dffda
 
8713898
 
99dffda
8713898
 
 
 
99dffda
 
7942b70
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
import gradio as gr
import os
import requests
import tempfile

# Validate HF_TOKEN environment variable
HF_API_TOKEN = os.getenv("HF_TOKEN")
if not HF_API_TOKEN:
    raise ValueError("HF_TOKEN environment variable is not set. Please set it to your Hugging Face API token.")

MODEL_ID = "rohitnagareddy/Qwen3-0.6B-Coding-Finetuned-v1"
API_URL = f"https://api-inference.huggingface.co/models/{MODEL_ID}"
HEADERS = {"Authorization": f"Bearer {HF_API_TOKEN}"}

def query_hf_api(prompt):
    try:
        response = requests.post(API_URL, headers=HEADERS, json={"inputs": prompt})
        response.raise_for_status()  # Raise an exception for HTTP errors
        data = response.json()
        if isinstance(data, list) and "generated_text" in data[0]:
            return data[0]["generated_text"]
        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("# 🤖 Qwen3 Coding Chatbot (Gradio + HF API)")

    with gr.Row():
        clear = gr.Button("🧹 Clear Chat")
        download_btn = gr.Button("⬇️ Download Chat")

    chat = gr.Chatbot(label="Qwen Chat", 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()