Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -218,7 +218,7 @@ def choose_model(token: str, requested: Optional[str]) -> tuple[str, Optional[st
|
|
| 218 |
|
| 219 |
return (requested or "meta-llama/Llama-3.1-8B-Instruct"), "γγΌγ―γ³γ§ε©η¨ε―θ½γͺγ’γγ«γθ¦γ€γγγΎγγγ"
|
| 220 |
|
| 221 |
-
# ---------- Stream chunk extractor
|
| 222 |
def _extract_piece(chunk) -> Optional[str]:
|
| 223 |
try:
|
| 224 |
choices = getattr(chunk, "choices", None)
|
|
@@ -239,7 +239,7 @@ def _extract_piece(chunk) -> Optional[str]:
|
|
| 239 |
piece = delta.get("content")
|
| 240 |
return piece
|
| 241 |
except Exception:
|
| 242 |
-
return None
|
| 243 |
|
| 244 |
# ---------- Streaming via HF Router ----------
|
| 245 |
def stream_chat(
|
|
@@ -277,7 +277,7 @@ def stream_chat(
|
|
| 277 |
else:
|
| 278 |
yield f"η³γ訳γγγΎγγοΌγ¨γ©γΌγηΊηγγΎγγ: {msg}"
|
| 279 |
|
| 280 |
-
# ---------- Gradio callback (
|
| 281 |
def respond(
|
| 282 |
message: Union[str, List[GradioMessage]],
|
| 283 |
history: History,
|
|
@@ -302,18 +302,24 @@ def respond(
|
|
| 302 |
return
|
| 303 |
|
| 304 |
chosen_model, note = choose_model(token, (model_id or "").strip() or None)
|
| 305 |
-
if note
|
| 306 |
-
yield f"βΉοΈ {note}"
|
| 307 |
|
| 308 |
client = InferenceClient(api_key=token)
|
| 309 |
-
|
|
|
|
|
|
|
| 310 |
client,
|
| 311 |
model_id=chosen_model,
|
| 312 |
messages=msgs,
|
| 313 |
max_tokens=max_tokens,
|
| 314 |
temperature=temperature,
|
| 315 |
top_p=top_p,
|
| 316 |
-
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 317 |
|
| 318 |
# ---------- UI ----------
|
| 319 |
with gr.Blocks(css=css) as demo:
|
|
@@ -364,8 +370,10 @@ with gr.Blocks(css=css) as demo:
|
|
| 364 |
fn=respond,
|
| 365 |
additional_inputs=[user_level, max_tokens, temperature, top_p, model_id, hf_token],
|
| 366 |
type="messages",
|
| 367 |
-
fill_height=True, # β
helps ChatInterface size correctly in the flex panel
|
| 368 |
)
|
| 369 |
|
|
|
|
|
|
|
|
|
|
| 370 |
if __name__ == "__main__":
|
| 371 |
demo.launch(server_name="0.0.0.0", server_port=7860, share=False)
|
|
|
|
| 218 |
|
| 219 |
return (requested or "meta-llama/Llama-3.1-8B-Instruct"), "γγΌγ―γ³γ§ε©η¨ε―θ½γͺγ’γγ«γθ¦γ€γγγΎγγγ"
|
| 220 |
|
| 221 |
+
# ---------- Stream chunk extractor ----------
|
| 222 |
def _extract_piece(chunk) -> Optional[str]:
|
| 223 |
try:
|
| 224 |
choices = getattr(chunk, "choices", None)
|
|
|
|
| 239 |
piece = delta.get("content")
|
| 240 |
return piece
|
| 241 |
except Exception:
|
| 242 |
+
return None
|
| 243 |
|
| 244 |
# ---------- Streaming via HF Router ----------
|
| 245 |
def stream_chat(
|
|
|
|
| 277 |
else:
|
| 278 |
yield f"η³γ訳γγγΎγγοΌγ¨γ©γΌγηΊηγγΎγγ: {msg}"
|
| 279 |
|
| 280 |
+
# ---------- Gradio callback (FIXED: single continuous stream, no separate note-yield) ----------
|
| 281 |
def respond(
|
| 282 |
message: Union[str, List[GradioMessage]],
|
| 283 |
history: History,
|
|
|
|
| 302 |
return
|
| 303 |
|
| 304 |
chosen_model, note = choose_model(token, (model_id or "").strip() or None)
|
| 305 |
+
prefix = f"βΉοΈ {note}\n\n" if note else ""
|
|
|
|
| 306 |
|
| 307 |
client = InferenceClient(api_key=token)
|
| 308 |
+
|
| 309 |
+
first = True
|
| 310 |
+
for chunk in stream_chat(
|
| 311 |
client,
|
| 312 |
model_id=chosen_model,
|
| 313 |
messages=msgs,
|
| 314 |
max_tokens=max_tokens,
|
| 315 |
temperature=temperature,
|
| 316 |
top_p=top_p,
|
| 317 |
+
):
|
| 318 |
+
if first and prefix:
|
| 319 |
+
yield prefix + chunk
|
| 320 |
+
first = False
|
| 321 |
+
else:
|
| 322 |
+
yield chunk
|
| 323 |
|
| 324 |
# ---------- UI ----------
|
| 325 |
with gr.Blocks(css=css) as demo:
|
|
|
|
| 370 |
fn=respond,
|
| 371 |
additional_inputs=[user_level, max_tokens, temperature, top_p, model_id, hf_token],
|
| 372 |
type="messages",
|
|
|
|
| 373 |
)
|
| 374 |
|
| 375 |
+
# β
Recommended on Spaces for stable streaming behind proxies
|
| 376 |
+
demo = demo.queue()
|
| 377 |
+
|
| 378 |
if __name__ == "__main__":
|
| 379 |
demo.launch(server_name="0.0.0.0", server_port=7860, share=False)
|