Update app.py
Browse files
app.py
CHANGED
@@ -105,12 +105,12 @@ def init_agent():
|
|
105 |
agent.init_model()
|
106 |
return agent
|
107 |
|
108 |
-
def process_final_report(agent, file, chatbot_state: List[
|
109 |
messages = chatbot_state if chatbot_state else []
|
110 |
if file is None or not hasattr(file, "name"):
|
111 |
-
return messages + [
|
112 |
|
113 |
-
messages.append(
|
114 |
text = extract_text_from_excel(file.name)
|
115 |
chunks = split_text_into_chunks(text)
|
116 |
chunk_responses = [None] * len(chunks)
|
@@ -141,10 +141,10 @@ def process_final_report(agent, file, chatbot_state: List[Dict[str, str]]) -> Tu
|
|
141 |
|
142 |
valid = [r for r in chunk_responses if r and not r.startswith("β")]
|
143 |
if not valid:
|
144 |
-
return messages + [
|
145 |
|
146 |
summary_prompt = f"Summarize this analysis in a final structured report:\n\n" + "\n\n".join(valid)
|
147 |
-
messages.append(
|
148 |
|
149 |
final_report = ""
|
150 |
for res in agent.run_gradio_chat(
|
@@ -162,8 +162,8 @@ def process_final_report(agent, file, chatbot_state: List[Dict[str, str]]) -> Tu
|
|
162 |
with open(report_path, 'w') as f:
|
163 |
f.write(f"# π§ Final Patient Report\n\n{cleaned}")
|
164 |
|
165 |
-
messages.append(
|
166 |
-
messages.append(
|
167 |
return messages, report_path
|
168 |
|
169 |
def create_ui(agent):
|
@@ -206,7 +206,7 @@ def create_ui(agent):
|
|
206 |
gr.Markdown("""# π§ Clinical Reasoning Assistant
|
207 |
Upload clinical Excel records below and click **Analyze** to generate a medical summary.
|
208 |
""")
|
209 |
-
chatbot = gr.Chatbot(elem_classes="chatbot")
|
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)
|
@@ -214,8 +214,7 @@ Upload clinical Excel records below and click **Analyze** to generate a medical
|
|
214 |
|
215 |
def update_ui(file, current_state):
|
216 |
messages, report_path = process_final_report(agent, file, current_state)
|
217 |
-
|
218 |
-
return chat_format, gr.update(visible=report_path is not None, value=report_path), messages
|
219 |
|
220 |
analyze_btn.click(
|
221 |
fn=update_ui,
|
@@ -232,4 +231,4 @@ if __name__ == "__main__":
|
|
232 |
demo.launch(server_name="0.0.0.0", server_port=7860, allowed_paths=["/data/hf_cache/reports"], share=False)
|
233 |
except Exception as e:
|
234 |
print(f"Error: {str(e)}")
|
235 |
-
sys.exit(1)
|
|
|
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)
|
115 |
chunks = split_text_into_chunks(text)
|
116 |
chunk_responses = [None] * len(chunks)
|
|
|
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..."))
|
148 |
|
149 |
final_report = ""
|
150 |
for res in agent.run_gradio_chat(
|
|
|
162 |
with open(report_path, 'w') as f:
|
163 |
f.write(f"# π§ Final Patient Report\n\n{cleaned}")
|
164 |
|
165 |
+
messages.append(("assistant", f"π Final Report:\n\n{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):
|
|
|
206 |
gr.Markdown("""# π§ Clinical Reasoning Assistant
|
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="bot")
|
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)
|
|
|
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,
|
|
|
231 |
demo.launch(server_name="0.0.0.0", server_port=7860, allowed_paths=["/data/hf_cache/reports"], share=False)
|
232 |
except Exception as e:
|
233 |
print(f"Error: {str(e)}")
|
234 |
+
sys.exit(1)
|