Update handler.py
Browse files- handler.py +13 -4
handler.py
CHANGED
|
@@ -312,8 +312,15 @@ def generate_response(
|
|
| 312 |
|
| 313 |
dbg(f"[gen] temperature={temperature} top_p={top_p} max_new_tokens={max_new_tokens} rep={repetition_penalty} seed={det_seed} mode={output_mode}")
|
| 314 |
|
| 315 |
-
# For "report_en", compose by calling json + narrative branches
|
| 316 |
if output_mode == "report_en":
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 317 |
first = generate_response(
|
| 318 |
message_text=message_text,
|
| 319 |
image_input=image_input,
|
|
@@ -327,7 +334,8 @@ def generate_response(
|
|
| 327 |
if not isinstance(first, dict) or "response" not in first or not isinstance(first["response"], dict):
|
| 328 |
return first
|
| 329 |
data = first["response"]
|
| 330 |
-
|
|
|
|
| 331 |
second = generate_response(
|
| 332 |
message_text=message_text,
|
| 333 |
image_input=image_input,
|
|
@@ -339,14 +347,15 @@ def generate_response(
|
|
| 339 |
output_mode="narrative",
|
| 340 |
)
|
| 341 |
narrative = second.get("response") if isinstance(second, dict) else None
|
| 342 |
-
|
| 343 |
table_txt = render_ecg_table_en(data)
|
| 344 |
return {
|
| 345 |
"status": "success",
|
| 346 |
"report": {"table_text": table_txt, "json": data, "narrative": narrative},
|
| 347 |
-
"conversation_id":
|
| 348 |
}
|
| 349 |
|
|
|
|
| 350 |
chatbot = chat_manager.get_chatbot(args, args.model_path, tokenizer, model, image_processor, context_len)
|
| 351 |
if conv_mode_override and conv_mode_override in conv_templates:
|
| 352 |
chatbot.conversation = conv_templates[conv_mode_override].copy()
|
|
|
|
| 312 |
|
| 313 |
dbg(f"[gen] temperature={temperature} top_p={top_p} max_new_tokens={max_new_tokens} rep={repetition_penalty} seed={det_seed} mode={output_mode}")
|
| 314 |
|
|
|
|
| 315 |
if output_mode == "report_en":
|
| 316 |
+
# Ensure a session exists so we can safely expose a conversation_id
|
| 317 |
+
try:
|
| 318 |
+
_cb = chat_manager.get_chatbot(args, args.model_path, tokenizer, model, image_processor, context_len)
|
| 319 |
+
conv_id = id(_cb.conversation)
|
| 320 |
+
except Exception:
|
| 321 |
+
conv_id = None
|
| 322 |
+
|
| 323 |
+
# 1) Produce strict JSON (machine-readable)
|
| 324 |
first = generate_response(
|
| 325 |
message_text=message_text,
|
| 326 |
image_input=image_input,
|
|
|
|
| 334 |
if not isinstance(first, dict) or "response" not in first or not isinstance(first["response"], dict):
|
| 335 |
return first
|
| 336 |
data = first["response"]
|
| 337 |
+
|
| 338 |
+
# 2) Produce short narrative (human-readable)
|
| 339 |
second = generate_response(
|
| 340 |
message_text=message_text,
|
| 341 |
image_input=image_input,
|
|
|
|
| 347 |
output_mode="narrative",
|
| 348 |
)
|
| 349 |
narrative = second.get("response") if isinstance(second, dict) else None
|
| 350 |
+
|
| 351 |
table_txt = render_ecg_table_en(data)
|
| 352 |
return {
|
| 353 |
"status": "success",
|
| 354 |
"report": {"table_text": table_txt, "json": data, "narrative": narrative},
|
| 355 |
+
"conversation_id": conv_id
|
| 356 |
}
|
| 357 |
|
| 358 |
+
|
| 359 |
chatbot = chat_manager.get_chatbot(args, args.model_path, tokenizer, model, image_processor, context_len)
|
| 360 |
if conv_mode_override and conv_mode_override in conv_templates:
|
| 361 |
chatbot.conversation = conv_templates[conv_mode_override].copy()
|