dpv007 commited on
Commit
d65b44e
·
verified ·
1 Parent(s): f7b05f0

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +29 -11
app.py CHANGED
@@ -348,26 +348,43 @@ def generate_chat(message, history, system_prompt, max_tokens, temperature, top_
348
  yield f"🚨 MODEL FAILED TO LOAD 🚨\n\nError Details:\n{LOAD_ERROR}"
349
  return
350
 
351
- if not message.strip():
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
352
  yield "Please provide a valid text string."
353
  return
354
 
355
  # --- BUILD CHATML PROMPT FORMAT USING THE TOKENIZER ---
356
- messages = [{"role": "system", "content": system_prompt}]
357
 
358
  for msg in history:
359
- # Robust history parsing for older Gradio versions (list of lists) and newer versions (list of dicts)
360
  if isinstance(msg, dict):
361
- messages.append({"role": msg.get("role", "user"), "content": msg.get("content", "")})
362
  elif hasattr(msg, "role"):
363
- messages.append({"role": msg.role, "content": msg.content})
364
- elif isinstance(msg, (list, tuple)) and len(msg) == 2:
365
- messages.append({"role": "user", "content": msg[0]})
366
- messages.append({"role": "assistant", "content": msg[1]})
367
-
368
- messages.append({"role": "user", "content": message})
 
 
369
 
370
- # Use tokenizer's built in template to guarantee perfect tokenization and trailing space handling
371
  prompt = TOKENIZER.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)
372
  input_ids = TOKENIZER(prompt, return_tensors="pt")["input_ids"].to(DEVICE)
373
 
@@ -411,6 +428,7 @@ def generate_chat(message, history, system_prompt, max_tokens, temperature, top_
411
  next_token_logits = outputs["logits"][0, -1, :].clone()
412
  past_key_values = outputs["past_key_values"]
413
 
 
414
  with gr.Blocks(title="🤖 OPTIMUS Web Inference Hub") as demo:
415
  gr.Markdown(
416
  """
 
348
  yield f"🚨 MODEL FAILED TO LOAD 🚨\n\nError Details:\n{LOAD_ERROR}"
349
  return
350
 
351
+ # Helper function to guarantee we always get a string out of Gradio's weird formats
352
+ def force_str(item):
353
+ if item is None:
354
+ return ""
355
+ if isinstance(item, str):
356
+ return item
357
+ if isinstance(item, dict) and "text" in item:
358
+ return str(item["text"])
359
+ if isinstance(item, (list, tuple)):
360
+ if len(item) > 0 and isinstance(item[0], str):
361
+ return item[0]
362
+ return " ".join(str(x) for x in item)
363
+ return str(item)
364
+
365
+ safe_message = force_str(message)
366
+ if not safe_message.strip():
367
  yield "Please provide a valid text string."
368
  return
369
 
370
  # --- BUILD CHATML PROMPT FORMAT USING THE TOKENIZER ---
371
+ messages = [{"role": "system", "content": force_str(system_prompt)}]
372
 
373
  for msg in history:
374
+ # Robust history parsing for all Gradio versions
375
  if isinstance(msg, dict):
376
+ messages.append({"role": force_str(msg.get("role", "user")), "content": force_str(msg.get("content", ""))})
377
  elif hasattr(msg, "role"):
378
+ messages.append({"role": force_str(msg.role), "content": force_str(msg.content)})
379
+ elif isinstance(msg, (list, tuple)) and len(msg) >= 2:
380
+ if msg[0] is not None:
381
+ messages.append({"role": "user", "content": force_str(msg[0])})
382
+ if msg[1] is not None:
383
+ messages.append({"role": "assistant", "content": force_str(msg[1])})
384
+
385
+ messages.append({"role": "user", "content": safe_message})
386
 
387
+ # Use tokenizer's built in template to guarantee perfect tokenization
388
  prompt = TOKENIZER.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)
389
  input_ids = TOKENIZER(prompt, return_tensors="pt")["input_ids"].to(DEVICE)
390
 
 
428
  next_token_logits = outputs["logits"][0, -1, :].clone()
429
  past_key_values = outputs["past_key_values"]
430
 
431
+
432
  with gr.Blocks(title="🤖 OPTIMUS Web Inference Hub") as demo:
433
  gr.Markdown(
434
  """