Update app.py
Browse files
app.py
CHANGED
|
@@ -3,32 +3,40 @@ import os
|
|
| 3 |
import requests
|
| 4 |
import tempfile
|
| 5 |
|
| 6 |
-
#
|
| 7 |
HF_API_TOKEN = os.getenv("HF_TOKEN")
|
| 8 |
if not HF_API_TOKEN:
|
| 9 |
-
raise ValueError("HF_TOKEN environment variable is not set.
|
| 10 |
|
| 11 |
MODEL_ID = "google/flan-t5-base"
|
| 12 |
API_URL = f"https://api-inference.huggingface.co/models/{MODEL_ID}"
|
| 13 |
HEADERS = {"Authorization": f"Bearer {HF_API_TOKEN}"}
|
| 14 |
|
|
|
|
| 15 |
def query_hf_api(prompt):
|
| 16 |
try:
|
| 17 |
-
|
| 18 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
data = response.json()
|
| 20 |
if isinstance(data, list) and "generated_text" in data[0]:
|
| 21 |
return data[0]["generated_text"]
|
| 22 |
-
|
|
|
|
| 23 |
except requests.exceptions.RequestException as e:
|
| 24 |
return f"[Error] API request failed: {e}"
|
| 25 |
|
|
|
|
| 26 |
def chat_fn(prompt, chat_history):
|
| 27 |
response = query_hf_api(prompt)
|
| 28 |
chat_history.append({"role": "user", "content": prompt})
|
| 29 |
chat_history.append({"role": "assistant", "content": response})
|
| 30 |
return chat_history, chat_history
|
| 31 |
|
|
|
|
| 32 |
def save_chat(chat_history):
|
| 33 |
try:
|
| 34 |
with tempfile.NamedTemporaryFile(delete=False, suffix=".txt", mode="w", encoding="utf-8") as f:
|
|
@@ -38,14 +46,15 @@ def save_chat(chat_history):
|
|
| 38 |
except Exception as e:
|
| 39 |
return f"[Error] Failed to save chat: {e}"
|
| 40 |
|
|
|
|
| 41 |
with gr.Blocks(theme=gr.themes.Monochrome()) as demo:
|
| 42 |
-
gr.Markdown("# 🤖
|
| 43 |
|
| 44 |
with gr.Row():
|
| 45 |
clear = gr.Button("🧹 Clear Chat")
|
| 46 |
download_btn = gr.Button("⬇️ Download Chat")
|
| 47 |
|
| 48 |
-
chat = gr.Chatbot(label="
|
| 49 |
msg = gr.Textbox(label="Your message", placeholder="Ask me something...")
|
| 50 |
submit = gr.Button("🚀 Send")
|
| 51 |
history = gr.State([])
|
|
|
|
| 3 |
import requests
|
| 4 |
import tempfile
|
| 5 |
|
| 6 |
+
# Set this directly or via Environment Variables (safe for HF Spaces)
|
| 7 |
HF_API_TOKEN = os.getenv("HF_TOKEN")
|
| 8 |
if not HF_API_TOKEN:
|
| 9 |
+
raise ValueError("HF_TOKEN environment variable is not set.")
|
| 10 |
|
| 11 |
MODEL_ID = "google/flan-t5-base"
|
| 12 |
API_URL = f"https://api-inference.huggingface.co/models/{MODEL_ID}"
|
| 13 |
HEADERS = {"Authorization": f"Bearer {HF_API_TOKEN}"}
|
| 14 |
|
| 15 |
+
|
| 16 |
def query_hf_api(prompt):
|
| 17 |
try:
|
| 18 |
+
payload = {
|
| 19 |
+
"inputs": prompt,
|
| 20 |
+
"parameters": {"max_new_tokens": 100}
|
| 21 |
+
}
|
| 22 |
+
response = requests.post(API_URL, headers=HEADERS, json=payload)
|
| 23 |
+
response.raise_for_status()
|
| 24 |
data = response.json()
|
| 25 |
if isinstance(data, list) and "generated_text" in data[0]:
|
| 26 |
return data[0]["generated_text"]
|
| 27 |
+
else:
|
| 28 |
+
return "[Error] Unexpected response format."
|
| 29 |
except requests.exceptions.RequestException as e:
|
| 30 |
return f"[Error] API request failed: {e}"
|
| 31 |
|
| 32 |
+
|
| 33 |
def chat_fn(prompt, chat_history):
|
| 34 |
response = query_hf_api(prompt)
|
| 35 |
chat_history.append({"role": "user", "content": prompt})
|
| 36 |
chat_history.append({"role": "assistant", "content": response})
|
| 37 |
return chat_history, chat_history
|
| 38 |
|
| 39 |
+
|
| 40 |
def save_chat(chat_history):
|
| 41 |
try:
|
| 42 |
with tempfile.NamedTemporaryFile(delete=False, suffix=".txt", mode="w", encoding="utf-8") as f:
|
|
|
|
| 46 |
except Exception as e:
|
| 47 |
return f"[Error] Failed to save chat: {e}"
|
| 48 |
|
| 49 |
+
|
| 50 |
with gr.Blocks(theme=gr.themes.Monochrome()) as demo:
|
| 51 |
+
gr.Markdown("# 🤖 Flan-T5 Chatbot (Free Coding/QA Model)")
|
| 52 |
|
| 53 |
with gr.Row():
|
| 54 |
clear = gr.Button("🧹 Clear Chat")
|
| 55 |
download_btn = gr.Button("⬇️ Download Chat")
|
| 56 |
|
| 57 |
+
chat = gr.Chatbot(label="Chatbot", type="messages")
|
| 58 |
msg = gr.Textbox(label="Your message", placeholder="Ask me something...")
|
| 59 |
submit = gr.Button("🚀 Send")
|
| 60 |
history = gr.State([])
|