ankarb commited on
Commit
6bdf619
·
verified ·
1 Parent(s): 910a416

Update hardware_qa_assistant_lang_graph_gradio_demo_version_users.py

Browse files
hardware_qa_assistant_lang_graph_gradio_demo_version_users.py CHANGED
@@ -1309,21 +1309,26 @@ APP = build_graph()
1309
  # Gradio UI — simple but expressive for demos
1310
  # =====================
1311
 
1312
- def to_pairs(history: List[Dict[str, str]]):
1313
- """Convert role-wise history into Chatbot (user, assistant) pairs."""
1314
- pairs: List[Tuple[str, str]] = []
1315
- u: Optional[str] = None
1316
- for m in history:
1317
- if m['role'] == 'user':
1318
- u = m['content']
1319
- elif m['role'] == 'assistant' and u is not None:
1320
- pairs.append((u, m['content']))
1321
- u = None
1322
- return pairs
 
 
 
 
 
1323
 
1324
  def run_once(request: gr.Request,
1325
  thread_id: str,
1326
- chat_pairs: List[Tuple[str, str]],
1327
  msg: str,
1328
  files: List[gr.File],
1329
  project: str,
@@ -1337,13 +1342,8 @@ def run_once(request: gr.Request,
1337
  # Who is logged in?
1338
  user_email = current_user(request)
1339
 
1340
- # Rehydrate conversation history into role messages
1341
- history: List[Dict[str, str]] = []
1342
- for u_msg, a_msg in (chat_pairs or []):
1343
- history += [
1344
- {"role": "user", "content": u_msg},
1345
- {"role": "assistant", "content": a_msg},
1346
- ]
1347
 
1348
  # Read uploaded files as bytes now (Gradio gives temp filepaths)
1349
  uploads: List[Dict[str, Any]] = []
@@ -1392,7 +1392,7 @@ def run_once(request: gr.Request,
1392
 
1393
  # Expose a Markdown report file if the session ended this turn
1394
  report_path = out.get('report_path', None)
1395
- return to_pairs(history), (report_path if report_path else None), trace_md, dag_img
1396
 
1397
  def build_gradio_blocks(auth_secret: Optional[str] = None):
1398
  # 👇 Force the same secret that FastAPI used to sign the JWT
@@ -1439,7 +1439,7 @@ def build_gradio_blocks(auth_secret: Optional[str] = None):
1439
  ocr_backend = gr.Dropdown(choices=["paddle","tesseract","auto"], value=OCR_BACKEND, label="OCR backend")
1440
  do_web_search = gr.Checkbox(value=True if tavily else False, label="Use Tavily web search if needed")
1441
 
1442
- chat = gr.Chatbot(height=560, elem_id="chatui", type="tuples")
1443
 
1444
  # Uploader + Vision checkbox side-by-side (compact)
1445
 
@@ -1577,10 +1577,12 @@ def build_gradio_blocks(auth_secret: Optional[str] = None):
1577
  # Put message into chat so user understands what to do
1578
  if c is None:
1579
  c = []
1580
- c = c + [("", "❗Please enter your name first (required).")]
 
1581
  return c, gr.update(value=None, visible=False), "(Enter your name first)", None, gr.update(), gr.update()
1582
 
1583
- chat_pairs, summary_path, trace_md_val, dag_img_val = \
 
1584
  run_once(request, t, c, m, f, proj, prov, mdl, ocr, web, schem, progress=progress)
1585
 
1586
  upload_reset = gr.update(value=None)
@@ -1590,7 +1592,7 @@ def build_gradio_blocks(auth_secret: Optional[str] = None):
1590
  summary_upd = (gr.update(value=summary_path, visible=True)
1591
  if summary_path else gr.update(value=None, visible=False))
1592
 
1593
- return chat_pairs, summary_upd, trace_md_val, dag_img_val, upload_reset, msg_reset
1594
 
1595
 
1596
  refresh_btn.click(ui_refresh, inputs=None, outputs=[project_dd, user_md])
 
1309
  # Gradio UI — simple but expressive for demos
1310
  # =====================
1311
 
1312
+ def normalize_messages(chat_history):
1313
+ """
1314
+ Ensure Chatbot history is a list of {"role":..., "content":...}.
1315
+ """
1316
+ if not chat_history:
1317
+ return []
1318
+ # Already messages format
1319
+ if isinstance(chat_history, list) and isinstance(chat_history[0], dict):
1320
+ return chat_history
1321
+ # If something else sneaks in, fail safe:
1322
+ out = []
1323
+ for item in chat_history:
1324
+ if isinstance(item, dict) and "role" in item and "content" in item:
1325
+ out.append({"role": item["role"], "content": str(item["content"])})
1326
+ return out
1327
+
1328
 
1329
  def run_once(request: gr.Request,
1330
  thread_id: str,
1331
+ chat_history: List[Dict[str, str]],
1332
  msg: str,
1333
  files: List[gr.File],
1334
  project: str,
 
1342
  # Who is logged in?
1343
  user_email = current_user(request)
1344
 
1345
+ # Chatbot now stores OpenAI-style messages directly
1346
+ history: List[Dict[str, str]] = normalize_messages(chat_history)
 
 
 
 
 
1347
 
1348
  # Read uploaded files as bytes now (Gradio gives temp filepaths)
1349
  uploads: List[Dict[str, Any]] = []
 
1392
 
1393
  # Expose a Markdown report file if the session ended this turn
1394
  report_path = out.get('report_path', None)
1395
+ return history, (report_path if report_path else None), trace_md, dag_img
1396
 
1397
  def build_gradio_blocks(auth_secret: Optional[str] = None):
1398
  # 👇 Force the same secret that FastAPI used to sign the JWT
 
1439
  ocr_backend = gr.Dropdown(choices=["paddle","tesseract","auto"], value=OCR_BACKEND, label="OCR backend")
1440
  do_web_search = gr.Checkbox(value=True if tavily else False, label="Use Tavily web search if needed")
1441
 
1442
+ chat = gr.Chatbot(height=560, elem_id="chatui", type="messages")
1443
 
1444
  # Uploader + Vision checkbox side-by-side (compact)
1445
 
 
1577
  # Put message into chat so user understands what to do
1578
  if c is None:
1579
  c = []
1580
+ c = normalize_messages(c)
1581
+ c = c + [{"role": "assistant", "content": "❗Please enter your name first (required)."}]
1582
  return c, gr.update(value=None, visible=False), "(Enter your name first)", None, gr.update(), gr.update()
1583
 
1584
+
1585
+ chat_history, summary_path, trace_md_val, dag_img_val = \
1586
  run_once(request, t, c, m, f, proj, prov, mdl, ocr, web, schem, progress=progress)
1587
 
1588
  upload_reset = gr.update(value=None)
 
1592
  summary_upd = (gr.update(value=summary_path, visible=True)
1593
  if summary_path else gr.update(value=None, visible=False))
1594
 
1595
+ return chat_history, summary_upd, trace_md_val, dag_img_val, upload_reset, msg_reset
1596
 
1597
 
1598
  refresh_btn.click(ui_refresh, inputs=None, outputs=[project_dd, user_md])