Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -7,88 +7,91 @@ BASE_URL = "https://router.huggingface.co/v1"
|
|
| 7 |
def get_headers():
|
| 8 |
key = os.environ.get("HF_API_KEY")
|
| 9 |
if not key:
|
| 10 |
-
|
| 11 |
-
return {
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
r
|
| 18 |
-
if r.status_code != 200:
|
| 19 |
-
return None, f"HTTP {r.status_code}: {r.text}"
|
| 20 |
data = r.json()
|
| 21 |
models = [m.get("id") for m in data.get("data", []) if m.get("id")]
|
| 22 |
if not models:
|
| 23 |
-
|
| 24 |
-
return models
|
| 25 |
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
for u, b in history:
|
| 29 |
-
messages.append({"role": "user", "content": u})
|
| 30 |
-
messages.append({"role": "assistant", "content": b})
|
| 31 |
-
messages.append({"role": "user", "content": user_msg})
|
| 32 |
|
|
|
|
| 33 |
r = requests.post(
|
| 34 |
f"{BASE_URL}/chat/completions",
|
| 35 |
-
headers=
|
| 36 |
-
json={
|
| 37 |
-
"model": model,
|
| 38 |
-
"messages": messages,
|
| 39 |
-
"temperature": 0.7,
|
| 40 |
-
"max_tokens": 200,
|
| 41 |
-
},
|
| 42 |
timeout=60,
|
| 43 |
)
|
| 44 |
-
|
| 45 |
if r.status_code != 200:
|
| 46 |
return f"HTTP {r.status_code}: {r.text}"
|
| 47 |
-
|
| 48 |
return r.json()["choices"][0]["message"]["content"]
|
| 49 |
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 74 |
history = history or []
|
|
|
|
|
|
|
|
|
|
| 75 |
|
| 76 |
-
|
| 77 |
-
if err:
|
| 78 |
-
history.append((user_msg, err))
|
| 79 |
-
return "", history
|
| 80 |
-
|
| 81 |
-
if not model:
|
| 82 |
-
models, _ = list_models(headers)
|
| 83 |
-
model = models[0]
|
| 84 |
|
| 85 |
-
|
| 86 |
-
history.append(
|
| 87 |
return "", history
|
| 88 |
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
clear.click(lambda: [], outputs=chat)
|
| 93 |
|
| 94 |
demo.launch()
|
|
|
|
| 7 |
def get_headers():
|
| 8 |
key = os.environ.get("HF_API_KEY")
|
| 9 |
if not key:
|
| 10 |
+
raise RuntimeError("HF_API_KEY missing (Space Settings → Variables)")
|
| 11 |
+
return {"Authorization": f"Bearer {key}", "Content-Type": "application/json"}
|
| 12 |
+
|
| 13 |
+
HEADERS = get_headers()
|
| 14 |
+
|
| 15 |
+
def list_models():
|
| 16 |
+
r = requests.get(f"{BASE_URL}/models", headers=HEADERS, timeout=20)
|
| 17 |
+
r.raise_for_status()
|
|
|
|
|
|
|
| 18 |
data = r.json()
|
| 19 |
models = [m.get("id") for m in data.get("data", []) if m.get("id")]
|
| 20 |
if not models:
|
| 21 |
+
raise RuntimeError("No models available for this account (enable a provider in HF settings)")
|
| 22 |
+
return models
|
| 23 |
|
| 24 |
+
MODELS = list_models()
|
| 25 |
+
DEFAULT_MODEL = MODELS[0]
|
|
|
|
|
|
|
|
|
|
|
|
|
| 26 |
|
| 27 |
+
def chat_call(model, messages):
|
| 28 |
r = requests.post(
|
| 29 |
f"{BASE_URL}/chat/completions",
|
| 30 |
+
headers=HEADERS,
|
| 31 |
+
json={"model": model, "messages": messages, "temperature": 0.7, "max_tokens": 200},
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 32 |
timeout=60,
|
| 33 |
)
|
|
|
|
| 34 |
if r.status_code != 200:
|
| 35 |
return f"HTTP {r.status_code}: {r.text}"
|
|
|
|
| 36 |
return r.json()["choices"][0]["message"]["content"]
|
| 37 |
|
| 38 |
+
CSS = """
|
| 39 |
+
:root{--bg:#0b0f19;--panel:rgba(255,255,255,.06);--panel2:rgba(255,255,255,.09);--text:rgba(255,255,255,.92);--muted:rgba(255,255,255,.65);--border:rgba(255,255,255,.12);--accent:#7c3aed;}
|
| 40 |
+
body.light{--bg:#f6f7fb;--panel:rgba(0,0,0,.04);--panel2:rgba(0,0,0,.06);--text:rgba(0,0,0,.88);--muted:rgba(0,0,0,.60);--border:rgba(0,0,0,.10);--accent:#2563eb;}
|
| 41 |
+
.gradio-container{background:var(--bg)!important;color:var(--text)!important;}
|
| 42 |
+
.card{background:var(--panel);border:1px solid var(--border);border-radius:18px;}
|
| 43 |
+
"""
|
| 44 |
+
|
| 45 |
+
TOGGLE = """
|
| 46 |
+
<div style="display:flex;align-items:center;gap:10px;user-select:none;">
|
| 47 |
+
<span style="color:var(--muted);font-size:13px;">Theme</span>
|
| 48 |
+
<label style="display:flex;align-items:center;gap:10px;">
|
| 49 |
+
<span style="color:var(--muted);font-size:13px;">Light</span>
|
| 50 |
+
<input id="themeToggle" type="checkbox"/>
|
| 51 |
+
<span style="color:var(--muted);font-size:13px;">Dark</span>
|
| 52 |
+
</label>
|
| 53 |
+
</div>
|
| 54 |
+
<script>
|
| 55 |
+
const saved = localStorage.getItem("theme") || "dark";
|
| 56 |
+
if(saved==="light") document.body.classList.add("light");
|
| 57 |
+
const t=document.getElementById("themeToggle");
|
| 58 |
+
t.checked=(saved!=="light");
|
| 59 |
+
t.addEventListener("change",()=>{
|
| 60 |
+
if(t.checked){document.body.classList.remove("light");localStorage.setItem("theme","dark");}
|
| 61 |
+
else{document.body.classList.add("light");localStorage.setItem("theme","light");}
|
| 62 |
+
});
|
| 63 |
+
</script>
|
| 64 |
+
"""
|
| 65 |
+
|
| 66 |
+
with gr.Blocks(css=CSS, title="My AI Chatbot") as demo:
|
| 67 |
+
with gr.Column():
|
| 68 |
+
with gr.Group(elem_classes=["card"]):
|
| 69 |
+
with gr.Row():
|
| 70 |
+
gr.Markdown(f"## My AI Chatbot\nModel: `{DEFAULT_MODEL}`")
|
| 71 |
+
gr.HTML(TOGGLE)
|
| 72 |
+
|
| 73 |
+
model = gr.Dropdown(choices=MODELS, value=DEFAULT_MODEL, label="Model")
|
| 74 |
+
|
| 75 |
+
chatbot = gr.Chatbot(type="messages", height=520, show_copy_button=True)
|
| 76 |
+
|
| 77 |
+
msg = gr.Textbox(placeholder="Type a message…", show_label=False)
|
| 78 |
+
send = gr.Button("Send")
|
| 79 |
+
clear = gr.Button("Clear")
|
| 80 |
+
|
| 81 |
+
def respond(user_msg, history, model_name):
|
| 82 |
history = history or []
|
| 83 |
+
messages = [{"role": "system", "content": "You are a helpful assistant."}]
|
| 84 |
+
messages.extend(history)
|
| 85 |
+
messages.append({"role": "user", "content": user_msg})
|
| 86 |
|
| 87 |
+
bot = chat_call(model_name, messages)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 88 |
|
| 89 |
+
history.append({"role": "user", "content": user_msg})
|
| 90 |
+
history.append({"role": "assistant", "content": bot})
|
| 91 |
return "", history
|
| 92 |
|
| 93 |
+
send.click(respond, [msg, chatbot, model], [msg, chatbot])
|
| 94 |
+
msg.submit(respond, [msg, chatbot, model], [msg, chatbot])
|
| 95 |
+
clear.click(lambda: [], outputs=chatbot)
|
|
|
|
| 96 |
|
| 97 |
demo.launch()
|