Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -7,7 +7,7 @@ BASE_URL = "https://router.huggingface.co/v1"
|
|
| 7 |
def get_headers():
|
| 8 |
key = os.environ.get("HF_API_KEY")
|
| 9 |
if not key:
|
| 10 |
-
return None, "HF_API_KEY missing (Settings → Variables)"
|
| 11 |
return {
|
| 12 |
"Authorization": f"Bearer {key}",
|
| 13 |
"Content-Type": "application/json",
|
|
@@ -20,7 +20,7 @@ def list_models(headers):
|
|
| 20 |
data = r.json()
|
| 21 |
models = [m["id"] for m in data.get("data", []) if "id" in m]
|
| 22 |
if not models:
|
| 23 |
-
return None, "No models available for this account"
|
| 24 |
return models, None
|
| 25 |
|
| 26 |
def chat_call(headers, model, history, user_msg):
|
|
@@ -33,7 +33,12 @@ def chat_call(headers, model, history, user_msg):
|
|
| 33 |
r = requests.post(
|
| 34 |
f"{BASE_URL}/chat/completions",
|
| 35 |
headers=headers,
|
| 36 |
-
json={
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 37 |
timeout=60,
|
| 38 |
)
|
| 39 |
if r.status_code != 200:
|
|
@@ -42,31 +47,56 @@ def chat_call(headers, model, history, user_msg):
|
|
| 42 |
|
| 43 |
with gr.Blocks(title="My AI Chatbot") as demo:
|
| 44 |
status = gr.Markdown("Starting…")
|
| 45 |
-
model_dd = gr.Dropdown(label="Model", choices=[], interactive=True)
|
| 46 |
-
chat = gr.Chatbot(height=
|
| 47 |
-
msg = gr.Textbox(placeholder="Type a message…")
|
| 48 |
send = gr.Button("Send")
|
|
|
|
| 49 |
|
| 50 |
def init():
|
| 51 |
headers, err = get_headers()
|
| 52 |
if err:
|
| 53 |
-
|
|
|
|
|
|
|
| 54 |
models, err = list_models(headers)
|
| 55 |
if err:
|
| 56 |
-
|
| 57 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 58 |
|
| 59 |
def respond(user_msg, history, model):
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 63 |
return "", history
|
| 64 |
-
bot = chat_call(headers, model, history, user_msg)
|
| 65 |
-
history.append([user_msg, bot])
|
| 66 |
-
return "", history
|
| 67 |
|
| 68 |
demo.load(init, outputs=[status, model_dd])
|
| 69 |
send.click(respond, inputs=[msg, chat, model_dd], outputs=[msg, chat])
|
| 70 |
msg.submit(respond, inputs=[msg, chat, model_dd], outputs=[msg, chat])
|
|
|
|
| 71 |
|
| 72 |
demo.launch()
|
|
|
|
| 7 |
def get_headers():
|
| 8 |
key = os.environ.get("HF_API_KEY")
|
| 9 |
if not key:
|
| 10 |
+
return None, "HF_API_KEY missing (Space Settings → Variables)"
|
| 11 |
return {
|
| 12 |
"Authorization": f"Bearer {key}",
|
| 13 |
"Content-Type": "application/json",
|
|
|
|
| 20 |
data = r.json()
|
| 21 |
models = [m["id"] for m in data.get("data", []) if "id" in m]
|
| 22 |
if not models:
|
| 23 |
+
return None, "No models available for this account (enable a provider in HF settings)"
|
| 24 |
return models, None
|
| 25 |
|
| 26 |
def chat_call(headers, model, history, user_msg):
|
|
|
|
| 33 |
r = requests.post(
|
| 34 |
f"{BASE_URL}/chat/completions",
|
| 35 |
headers=headers,
|
| 36 |
+
json={
|
| 37 |
+
"model": model,
|
| 38 |
+
"messages": messages,
|
| 39 |
+
"temperature": 0.7,
|
| 40 |
+
"max_tokens": 200,
|
| 41 |
+
},
|
| 42 |
timeout=60,
|
| 43 |
)
|
| 44 |
if r.status_code != 200:
|
|
|
|
| 47 |
|
| 48 |
with gr.Blocks(title="My AI Chatbot") as demo:
|
| 49 |
status = gr.Markdown("Starting…")
|
| 50 |
+
model_dd = gr.Dropdown(label="Model", choices=[], value=None, interactive=True)
|
| 51 |
+
chat = gr.Chatbot(height=520)
|
| 52 |
+
msg = gr.Textbox(placeholder="Type a message…", show_label=False)
|
| 53 |
send = gr.Button("Send")
|
| 54 |
+
clear = gr.Button("Clear")
|
| 55 |
|
| 56 |
def init():
|
| 57 |
headers, err = get_headers()
|
| 58 |
if err:
|
| 59 |
+
status_md = f"❌ {err}"
|
| 60 |
+
return status_md, gr.update(choices=[], value=None)
|
| 61 |
+
|
| 62 |
models, err = list_models(headers)
|
| 63 |
if err:
|
| 64 |
+
status_md = f"❌ {err}"
|
| 65 |
+
return status_md, gr.update(choices=[], value=None)
|
| 66 |
+
|
| 67 |
+
default_model = models[0]
|
| 68 |
+
status_md = f"✅ Ready. Using model: `{default_model}`"
|
| 69 |
+
return status_md, gr.update(choices=models, value=default_model)
|
| 70 |
|
| 71 |
def respond(user_msg, history, model):
|
| 72 |
+
try:
|
| 73 |
+
history = history or []
|
| 74 |
+
|
| 75 |
+
headers, err = get_headers()
|
| 76 |
+
if err:
|
| 77 |
+
history.append([user_msg, f"Setup error: {err}"])
|
| 78 |
+
return "", history
|
| 79 |
+
|
| 80 |
+
# If model is not selected for any reason, pick the first available
|
| 81 |
+
if not model:
|
| 82 |
+
models, m_err = list_models(headers)
|
| 83 |
+
if m_err:
|
| 84 |
+
history.append([user_msg, f"Model error: {m_err}"])
|
| 85 |
+
return "", history
|
| 86 |
+
model = models[0]
|
| 87 |
+
|
| 88 |
+
bot = chat_call(headers, model, history, user_msg)
|
| 89 |
+
history.append([user_msg, bot])
|
| 90 |
+
return "", history
|
| 91 |
+
|
| 92 |
+
except Exception as e:
|
| 93 |
+
history = history or []
|
| 94 |
+
history.append([user_msg, f"Exception: {type(e).__name__}: {e}"])
|
| 95 |
return "", history
|
|
|
|
|
|
|
|
|
|
| 96 |
|
| 97 |
demo.load(init, outputs=[status, model_dd])
|
| 98 |
send.click(respond, inputs=[msg, chat, model_dd], outputs=[msg, chat])
|
| 99 |
msg.submit(respond, inputs=[msg, chat, model_dd], outputs=[msg, chat])
|
| 100 |
+
clear.click(lambda: [], outputs=chat)
|
| 101 |
|
| 102 |
demo.launch()
|