Update app.py
Browse files
app.py
CHANGED
@@ -105,10 +105,10 @@ def init_agent():
|
|
105 |
agent.init_model()
|
106 |
return agent
|
107 |
|
108 |
-
def process_final_report(agent, file, chatbot_state: List[Tuple[str, str]]) -> Tuple[List[Tuple[str, str]], Union[str, None]]:
|
109 |
messages = chatbot_state if chatbot_state else []
|
110 |
if file is None or not hasattr(file, "name"):
|
111 |
-
return messages + [("assistant", "β Please upload a valid Excel file.")], None
|
112 |
|
113 |
messages.append(("user", f"Processing Excel file: {os.path.basename(file.name)}"))
|
114 |
text = extract_text_from_excel(file.name)
|
@@ -141,7 +141,7 @@ def process_final_report(agent, file, chatbot_state: List[Tuple[str, str]]) -> T
|
|
141 |
|
142 |
valid = [r for r in chunk_responses if r and not r.startswith("β")]
|
143 |
if not valid:
|
144 |
-
return messages + [("assistant", "β No valid chunk results.")], None
|
145 |
|
146 |
summary_prompt = f"Summarize this analysis in a final structured report:\n\n" + "\n\n".join(valid)
|
147 |
messages.append(("assistant", "π Generating final report..."))
|
@@ -162,9 +162,8 @@ def process_final_report(agent, file, chatbot_state: List[Tuple[str, str]]) -> T
|
|
162 |
with open(report_path, 'w') as f:
|
163 |
f.write(f"# π§ Final Patient Report\n\n{cleaned}")
|
164 |
|
165 |
-
messages.append(("assistant", cleaned))
|
166 |
messages.append(("assistant", f"β
Report generated and saved: {os.path.basename(report_path)}"))
|
167 |
-
return messages, report_path
|
168 |
|
169 |
def create_ui(agent):
|
170 |
with gr.Blocks(css="""
|
@@ -207,19 +206,20 @@ def create_ui(agent):
|
|
207 |
Upload clinical Excel records below and click **Analyze** to generate a medical summary.
|
208 |
""")
|
209 |
chatbot = gr.Chatbot(label="Chatbot", elem_classes="chatbot", type="tuples")
|
|
|
210 |
file_upload = gr.File(label="Upload Excel File", file_types=[".xlsx"])
|
211 |
analyze_btn = gr.Button("Analyze")
|
212 |
report_output = gr.File(label="Download Report", visible=False)
|
213 |
chatbot_state = gr.State(value=[])
|
214 |
|
215 |
def update_ui(file, current_state):
|
216 |
-
messages, report_path = process_final_report(agent, file, current_state)
|
217 |
-
return messages, gr.update(visible=report_path is not None, value=report_path), messages
|
218 |
|
219 |
analyze_btn.click(
|
220 |
fn=update_ui,
|
221 |
inputs=[file_upload, chatbot_state],
|
222 |
-
outputs=[chatbot, report_output, chatbot_state]
|
223 |
)
|
224 |
|
225 |
return demo
|
|
|
105 |
agent.init_model()
|
106 |
return agent
|
107 |
|
108 |
+
def process_final_report(agent, file, chatbot_state: List[Tuple[str, str]]) -> Tuple[List[Tuple[str, str]], Union[str, None, str]]:
|
109 |
messages = chatbot_state if chatbot_state else []
|
110 |
if file is None or not hasattr(file, "name"):
|
111 |
+
return messages + [("assistant", "β Please upload a valid Excel file.")], None, ""
|
112 |
|
113 |
messages.append(("user", f"Processing Excel file: {os.path.basename(file.name)}"))
|
114 |
text = extract_text_from_excel(file.name)
|
|
|
141 |
|
142 |
valid = [r for r in chunk_responses if r and not r.startswith("β")]
|
143 |
if not valid:
|
144 |
+
return messages + [("assistant", "β No valid chunk results.")], None, ""
|
145 |
|
146 |
summary_prompt = f"Summarize this analysis in a final structured report:\n\n" + "\n\n".join(valid)
|
147 |
messages.append(("assistant", "π Generating final report..."))
|
|
|
162 |
with open(report_path, 'w') as f:
|
163 |
f.write(f"# π§ Final Patient Report\n\n{cleaned}")
|
164 |
|
|
|
165 |
messages.append(("assistant", f"β
Report generated and saved: {os.path.basename(report_path)}"))
|
166 |
+
return messages, report_path, cleaned
|
167 |
|
168 |
def create_ui(agent):
|
169 |
with gr.Blocks(css="""
|
|
|
206 |
Upload clinical Excel records below and click **Analyze** to generate a medical summary.
|
207 |
""")
|
208 |
chatbot = gr.Chatbot(label="Chatbot", elem_classes="chatbot", type="tuples")
|
209 |
+
report_output_markdown = gr.Markdown(visible=False)
|
210 |
file_upload = gr.File(label="Upload Excel File", file_types=[".xlsx"])
|
211 |
analyze_btn = gr.Button("Analyze")
|
212 |
report_output = gr.File(label="Download Report", visible=False)
|
213 |
chatbot_state = gr.State(value=[])
|
214 |
|
215 |
def update_ui(file, current_state):
|
216 |
+
messages, report_path, final_text = process_final_report(agent, file, current_state)
|
217 |
+
return messages, gr.update(visible=report_path is not None, value=report_path), messages, gr.update(visible=True, value=final_text)
|
218 |
|
219 |
analyze_btn.click(
|
220 |
fn=update_ui,
|
221 |
inputs=[file_upload, chatbot_state],
|
222 |
+
outputs=[chatbot, report_output, chatbot_state, report_output_markdown]
|
223 |
)
|
224 |
|
225 |
return demo
|