Update app.py
Browse files
app.py
CHANGED
@@ -1,9 +1,9 @@
|
|
1 |
import gradio as gr
|
2 |
import os
|
3 |
import requests
|
|
|
4 |
|
5 |
-
|
6 |
-
HF_API_TOKEN = os.getenv("HF_TOKEN") # Set this in Hugging Face Secrets
|
7 |
MODEL_ID = "rohitnagareddy/Qwen3-0.6B-Coding-Finetuned-v1"
|
8 |
API_URL = f"https://api-inference.huggingface.co/models/{MODEL_ID}"
|
9 |
HEADERS = {"Authorization": f"Bearer {HF_API_TOKEN}"}
|
@@ -16,26 +16,33 @@ def query_hf_api(prompt):
|
|
16 |
|
17 |
def chat_fn(prompt, chat_history):
|
18 |
response = query_hf_api(prompt)
|
19 |
-
chat_history.append(
|
|
|
20 |
return chat_history, chat_history
|
21 |
|
22 |
-
def
|
23 |
-
|
|
|
|
|
|
|
24 |
|
25 |
with gr.Blocks(theme=gr.themes.Monochrome()) as demo:
|
26 |
-
gr.Markdown("#
|
|
|
27 |
with gr.Row():
|
28 |
clear = gr.Button("🧹 Clear Chat")
|
29 |
-
|
30 |
|
31 |
-
chat = gr.Chatbot(label="
|
32 |
-
msg = gr.Textbox(label="Your message")
|
33 |
submit = gr.Button("🚀 Send")
|
34 |
history = gr.State([])
|
|
|
35 |
|
36 |
submit.click(chat_fn, [msg, history], [chat, history])
|
37 |
msg.submit(chat_fn, [msg, history], [chat, history])
|
38 |
clear.click(lambda: ([], []), None, [chat, history])
|
39 |
-
|
|
|
|
|
40 |
|
41 |
-
demo.launch(auth=[("Admin", "Password")]) # 🔒 Change this
|
|
|
1 |
import gradio as gr
|
2 |
import os
|
3 |
import requests
|
4 |
+
import tempfile
|
5 |
|
6 |
+
HF_API_TOKEN = os.getenv("HF_TOKEN")
|
|
|
7 |
MODEL_ID = "rohitnagareddy/Qwen3-0.6B-Coding-Finetuned-v1"
|
8 |
API_URL = f"https://api-inference.huggingface.co/models/{MODEL_ID}"
|
9 |
HEADERS = {"Authorization": f"Bearer {HF_API_TOKEN}"}
|
|
|
16 |
|
17 |
def chat_fn(prompt, chat_history):
|
18 |
response = query_hf_api(prompt)
|
19 |
+
chat_history.append({"role": "user", "content": prompt})
|
20 |
+
chat_history.append({"role": "assistant", "content": response})
|
21 |
return chat_history, chat_history
|
22 |
|
23 |
+
def save_chat(chat_history):
|
24 |
+
with tempfile.NamedTemporaryFile(delete=False, suffix=".txt", mode="w", encoding="utf-8") as f:
|
25 |
+
for entry in chat_history:
|
26 |
+
f.write(f"{entry['role'].capitalize()}: {entry['content']}\n\n")
|
27 |
+
return f.name # return path to file
|
28 |
|
29 |
with gr.Blocks(theme=gr.themes.Monochrome()) as demo:
|
30 |
+
gr.Markdown("# 🤖 Qwen3 Coding Chatbot (Gradio + HF API)")
|
31 |
+
|
32 |
with gr.Row():
|
33 |
clear = gr.Button("🧹 Clear Chat")
|
34 |
+
download_btn = gr.Button("⬇️ Download Chat")
|
35 |
|
36 |
+
chat = gr.Chatbot(label="Qwen Chat", type="messages")
|
37 |
+
msg = gr.Textbox(label="Your message", placeholder="Ask me something...")
|
38 |
submit = gr.Button("🚀 Send")
|
39 |
history = gr.State([])
|
40 |
+
download_file = gr.File(label="Download", visible=False)
|
41 |
|
42 |
submit.click(chat_fn, [msg, history], [chat, history])
|
43 |
msg.submit(chat_fn, [msg, history], [chat, history])
|
44 |
clear.click(lambda: ([], []), None, [chat, history])
|
45 |
+
download_btn.click(save_chat, [history], download_file)
|
46 |
+
|
47 |
+
demo.launch(auth=[("admin", "securepass")])
|
48 |
|
|