makekali commited on
Commit
99dffda
·
verified ·
1 Parent(s): 01dec09

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +18 -11
app.py CHANGED
@@ -1,9 +1,9 @@
1
  import gradio as gr
2
  import os
3
  import requests
 
4
 
5
- # Hugging Face API config
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((prompt, response))
 
20
  return chat_history, chat_history
21
 
22
- def download_chat(history):
23
- return "\n\n".join([f"You: {user}\nAI: {bot}" for user, bot in history])
 
 
 
24
 
25
  with gr.Blocks(theme=gr.themes.Monochrome()) as demo:
26
- gr.Markdown("# 🧠 Qwen3 Coding Chatbot")
 
27
  with gr.Row():
28
  clear = gr.Button("🧹 Clear Chat")
29
- download = gr.Button("⬇️ Download Chat")
30
 
31
- chat = gr.Chatbot(label="Qwen3 Chat")
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
- download.click(download_chat, [history], file_name="chat.txt")
 
 
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