Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -8,21 +8,63 @@ DEFAULT_SYSTEM_PROMPT = "You are a helpful assistant."
|
|
| 8 |
|
| 9 |
HF_TOKEN = os.getenv("HF_TOKEN")
|
| 10 |
if not HF_TOKEN:
|
| 11 |
-
raise RuntimeError("HF_TOKEN is missing. Add it in Space Settings → Secrets.")
|
| 12 |
|
| 13 |
HEADERS = {
|
| 14 |
"Authorization": f"Bearer {HF_TOKEN}",
|
| 15 |
"Content-Type": "application/json",
|
| 16 |
}
|
| 17 |
|
| 18 |
-
def
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 26 |
return messages
|
| 27 |
|
| 28 |
def call_router(messages, temperature=0.7, max_tokens=400):
|
|
@@ -33,19 +75,27 @@ def call_router(messages, temperature=0.7, max_tokens=400):
|
|
| 33 |
"max_tokens": int(max_tokens),
|
| 34 |
}
|
| 35 |
resp = requests.post(ROUTER_URL, headers=HEADERS, json=payload, timeout=120)
|
|
|
|
| 36 |
if resp.status_code != 200:
|
| 37 |
try:
|
| 38 |
err = resp.json()
|
| 39 |
except Exception:
|
| 40 |
err = resp.text
|
| 41 |
raise RuntimeError(f"HTTP {resp.status_code}: {err}")
|
|
|
|
| 42 |
data = resp.json()
|
| 43 |
return data["choices"][0]["message"]["content"]
|
| 44 |
|
| 45 |
def respond(message, history, system_prompt, temperature, max_tokens):
|
| 46 |
try:
|
| 47 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 48 |
return call_router(messages, temperature=temperature, max_tokens=max_tokens)
|
|
|
|
| 49 |
except Exception as e:
|
| 50 |
return f"⚠️ {type(e).__name__}: {e}"
|
| 51 |
|
|
|
|
| 8 |
|
| 9 |
HF_TOKEN = os.getenv("HF_TOKEN")
|
| 10 |
if not HF_TOKEN:
|
| 11 |
+
raise RuntimeError("HF_TOKEN is missing. Add it in Space Settings → Secrets as HF_TOKEN.")
|
| 12 |
|
| 13 |
HEADERS = {
|
| 14 |
"Authorization": f"Bearer {HF_TOKEN}",
|
| 15 |
"Content-Type": "application/json",
|
| 16 |
}
|
| 17 |
|
| 18 |
+
def _get_content(x):
|
| 19 |
+
"""Extract text content from different Gradio message shapes."""
|
| 20 |
+
if isinstance(x, dict):
|
| 21 |
+
return x.get("content") or x.get("text") or x.get("value") or ""
|
| 22 |
+
return "" if x is None else str(x)
|
| 23 |
+
|
| 24 |
+
def history_to_messages(history):
|
| 25 |
+
"""
|
| 26 |
+
Supports multiple Gradio history formats:
|
| 27 |
+
1) [(user, bot), ...]
|
| 28 |
+
2) [{"role":"user","content":"..."}, {"role":"assistant","content":"..."}, ...]
|
| 29 |
+
3) other tuple/list shapes (take first two items)
|
| 30 |
+
"""
|
| 31 |
+
messages = []
|
| 32 |
+
|
| 33 |
+
if not history:
|
| 34 |
+
return messages
|
| 35 |
+
|
| 36 |
+
# Format 2: list of dict messages
|
| 37 |
+
if isinstance(history, list) and isinstance(history[0], dict):
|
| 38 |
+
for h in history:
|
| 39 |
+
role = h.get("role") or h.get("from") or h.get("speaker")
|
| 40 |
+
content = _get_content(h)
|
| 41 |
+
if not role or content == "":
|
| 42 |
+
continue
|
| 43 |
+
|
| 44 |
+
role = role.lower()
|
| 45 |
+
if role in ("human", "user"):
|
| 46 |
+
role = "user"
|
| 47 |
+
elif role in ("bot", "assistant", "ai", "model"):
|
| 48 |
+
role = "assistant"
|
| 49 |
+
|
| 50 |
+
if role in ("system", "user", "assistant"):
|
| 51 |
+
messages.append({"role": role, "content": content})
|
| 52 |
+
return messages
|
| 53 |
+
|
| 54 |
+
# Format 1 (and variants): list of tuples/lists
|
| 55 |
+
if isinstance(history, list):
|
| 56 |
+
for item in history:
|
| 57 |
+
if isinstance(item, (list, tuple)):
|
| 58 |
+
# take first two values if more than 2 exist
|
| 59 |
+
user_text = _get_content(item[0]) if len(item) >= 1 else ""
|
| 60 |
+
bot_text = _get_content(item[1]) if len(item) >= 2 else ""
|
| 61 |
+
|
| 62 |
+
if user_text:
|
| 63 |
+
messages.append({"role": "user", "content": user_text})
|
| 64 |
+
if bot_text:
|
| 65 |
+
messages.append({"role": "assistant", "content": bot_text})
|
| 66 |
+
# if it's some other type, ignore safely
|
| 67 |
+
|
| 68 |
return messages
|
| 69 |
|
| 70 |
def call_router(messages, temperature=0.7, max_tokens=400):
|
|
|
|
| 75 |
"max_tokens": int(max_tokens),
|
| 76 |
}
|
| 77 |
resp = requests.post(ROUTER_URL, headers=HEADERS, json=payload, timeout=120)
|
| 78 |
+
|
| 79 |
if resp.status_code != 200:
|
| 80 |
try:
|
| 81 |
err = resp.json()
|
| 82 |
except Exception:
|
| 83 |
err = resp.text
|
| 84 |
raise RuntimeError(f"HTTP {resp.status_code}: {err}")
|
| 85 |
+
|
| 86 |
data = resp.json()
|
| 87 |
return data["choices"][0]["message"]["content"]
|
| 88 |
|
| 89 |
def respond(message, history, system_prompt, temperature, max_tokens):
|
| 90 |
try:
|
| 91 |
+
user_msg = _get_content(message)
|
| 92 |
+
|
| 93 |
+
messages = [{"role": "system", "content": (system_prompt.strip() or DEFAULT_SYSTEM_PROMPT)}]
|
| 94 |
+
messages.extend(history_to_messages(history))
|
| 95 |
+
messages.append({"role": "user", "content": user_msg})
|
| 96 |
+
|
| 97 |
return call_router(messages, temperature=temperature, max_tokens=max_tokens)
|
| 98 |
+
|
| 99 |
except Exception as e:
|
| 100 |
return f"⚠️ {type(e).__name__}: {e}"
|
| 101 |
|