Update handler.py
Browse files311 unmodified lines3123133143153163173183197 unmodified lines3273283293303313323335 unmodified lines339340341342343344345346347348349350351352311 unmodified linesdbg(f"[gen] temperature={temperature} top_p={top_p} max_new_tokens={max_new_tokens} rep={repetition_penalty} seed={det_seed} mode={output_mode}")# For "report_en", compose by calling json + narrative branchesif output_mode == "report_en":first = generate_response(message_text=message_text,image_input=image_input,7 unmodified linesif not isinstance(first, dict) or "response" not in first or not isinstance(first["response"], dict):return firstdata = first["response"]second = generate_response(message_text=message_text,image_input=image_input,5 unmodified linesoutput_mode="narrative",)narrative = second.get("response") if isinstance(second, dict) else Nonetable_txt = render_ecg_table_en(data)return {"status": "success","report": {"table_text": table_txt, "json": data, "narrative": narrative},"conversation_id": id(chatbot) # not conversation; narrative branch already logged}chatbot = chat_manager.get_chatbot(args, args.model_path, tokenizer, model, image_processor, context_len)if conv_mode_override and conv_mode_override in conv_templates:chatbot.conversation = conv_templates[conv_mode_override].copy()311 unmodified lines3123133143153163173183193203213223233243253267 unmodified lines3343353363373383393403415 unmodified lines347348349350351352353354355356357358359360361311 unmodified linesdbg(f"[gen] temperature={temperature} top_p={top_p} max_new_tokens={max_new_tokens} rep={repetition_penalty} seed={det_seed} mode={output_mode}")if output_mode == "report_en":# Ensure a session exists so we can safely expose a conversation_idtry:_cb = chat_manager.get_chatbot(args, args.model_path, tokenizer, model, image_processor, context_len)conv_id = id(_cb.conversation)except Exception:conv_id = None# 1) Produce strict JSON (machine-readable)first = generate_response(message_text=message_text,image_input=image_input,7 unmodified linesif not isinstance(first, dict) or "response" not in first or not isinstance(first["response"], dict):return firstdata = first["response"]# 2) Produce short narrative (human-readable)second = generate_response(message_text=message_text,image_input=image_input,5 unmodified linesoutput_mode="narrative",)narrative = second.get("response") if isinstance(second, dict) else Nonetable_txt = render_ecg_table_en(data)return {"status": "success","report": {"table_text": table_txt, "json": data, "narrative": narrative},"conversation_id": conv_id}chatbot = chat_manager.get_chatbot(args, args.model_path, tokenizer, model, image_processor, context_len)if conv_mode_override and conv_mode_override in conv_templates:chatbot.conversation = conv_templates[conv_mode_override].copy()
handler.py+13-4