Update app.py
Browse files
app.py
CHANGED
|
@@ -3,7 +3,7 @@ import os
|
|
| 3 |
import pandas as pd
|
| 4 |
import json
|
| 5 |
import gradio as gr
|
| 6 |
-
from typing import List, Tuple, Dict, Any,
|
| 7 |
import hashlib
|
| 8 |
import shutil
|
| 9 |
import re
|
|
@@ -119,21 +119,17 @@ def init_agent():
|
|
| 119 |
agent.init_model()
|
| 120 |
return agent
|
| 121 |
|
| 122 |
-
def
|
| 123 |
-
messages = []
|
| 124 |
report_output = {"visible": False, "value": None}
|
| 125 |
|
| 126 |
if file is None or not hasattr(file, "name"):
|
| 127 |
-
messages
|
| 128 |
-
|
| 129 |
-
return
|
| 130 |
|
| 131 |
try:
|
| 132 |
-
messages
|
| 133 |
-
|
| 134 |
-
{"role": "assistant", "content": "β³ Extracting and analyzing data..."}
|
| 135 |
-
]
|
| 136 |
-
yield messages, report_output
|
| 137 |
|
| 138 |
extracted_text = extract_text_from_excel(file.name)
|
| 139 |
chunks = split_text_into_chunks(extracted_text)
|
|
@@ -141,7 +137,6 @@ def stream_final_report(agent, file) -> Generator[Tuple[List[Dict[str, str]], Di
|
|
| 141 |
|
| 142 |
for i, chunk in enumerate(chunks):
|
| 143 |
messages.append({"role": "assistant", "content": f"π Analyzing chunk {i+1}/{len(chunks)}..."})
|
| 144 |
-
yield messages, report_output
|
| 145 |
|
| 146 |
prompt = build_prompt_from_text(chunk)
|
| 147 |
response = ""
|
|
@@ -165,11 +160,9 @@ def stream_final_report(agent, file) -> Generator[Tuple[List[Dict[str, str]], Di
|
|
| 165 |
|
| 166 |
chunk_responses.append(clean_response(response))
|
| 167 |
messages.append({"role": "assistant", "content": f"β
Chunk {i+1} analysis complete"})
|
| 168 |
-
yield messages, report_output
|
| 169 |
|
| 170 |
final_prompt = "\n\n".join(chunk_responses) + "\n\nSummarize the key findings above."
|
| 171 |
messages.append({"role": "assistant", "content": "π Generating final report..."})
|
| 172 |
-
yield messages, report_output
|
| 173 |
|
| 174 |
stream_text = ""
|
| 175 |
for result in agent.run_gradio_chat(
|
|
@@ -189,11 +182,10 @@ def stream_final_report(agent, file) -> Generator[Tuple[List[Dict[str, str]], Di
|
|
| 189 |
for r in result:
|
| 190 |
if hasattr(r, "content"):
|
| 191 |
stream_text += r.content
|
| 192 |
-
|
| 193 |
-
messages[-1]["content"] = f"π Generating final report...\n\n{clean_response(stream_text)}"
|
| 194 |
-
yield messages, report_output
|
| 195 |
-
|
| 196 |
final_report = f"# \U0001f9e0 Final Patient Report\n\n{clean_response(stream_text)}"
|
|
|
|
|
|
|
| 197 |
timestamp = datetime.now().strftime('%Y%m%d_%H%M%S')
|
| 198 |
report_path = os.path.join(report_dir, f"report_{timestamp}.md")
|
| 199 |
|
|
@@ -202,11 +194,11 @@ def stream_final_report(agent, file) -> Generator[Tuple[List[Dict[str, str]], Di
|
|
| 202 |
|
| 203 |
messages.append({"role": "assistant", "content": f"β
Report generated and saved: report_{timestamp}.md"})
|
| 204 |
report_output = {"visible": True, "value": report_path}
|
| 205 |
-
yield messages, report_output
|
| 206 |
|
| 207 |
except Exception as e:
|
| 208 |
messages.append({"role": "assistant", "content": f"β Error processing file: {str(e)}"})
|
| 209 |
-
|
|
|
|
| 210 |
|
| 211 |
def create_ui(agent):
|
| 212 |
with gr.Blocks(title="Patient History Chat", css=".gradio-container {max-width: 900px !important}") as demo:
|
|
@@ -240,10 +232,17 @@ def create_ui(agent):
|
|
| 240 |
interactive=False
|
| 241 |
)
|
| 242 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 243 |
analyze_btn.click(
|
| 244 |
-
fn=
|
| 245 |
-
inputs=[file_upload],
|
| 246 |
-
outputs=[chatbot, report_output],
|
| 247 |
api_name="analyze"
|
| 248 |
)
|
| 249 |
|
|
|
|
| 3 |
import pandas as pd
|
| 4 |
import json
|
| 5 |
import gradio as gr
|
| 6 |
+
from typing import List, Tuple, Dict, Any, Union
|
| 7 |
import hashlib
|
| 8 |
import shutil
|
| 9 |
import re
|
|
|
|
| 119 |
agent.init_model()
|
| 120 |
return agent
|
| 121 |
|
| 122 |
+
def process_final_report(agent, file, chatbot_state: List[Dict[str, str]]) -> Tuple[List[Dict[str, str]], Dict[str, Any]]:
|
| 123 |
+
messages = chatbot_state if chatbot_state else []
|
| 124 |
report_output = {"visible": False, "value": None}
|
| 125 |
|
| 126 |
if file is None or not hasattr(file, "name"):
|
| 127 |
+
messages.append({"role": "assistant", "content": "β Please upload a valid Excel file before analyzing."})
|
| 128 |
+
return messages, report_output
|
|
|
|
| 129 |
|
| 130 |
try:
|
| 131 |
+
messages.append({"role": "user", "content": f"Processing Excel file: {os.path.basename(file.name)}"})
|
| 132 |
+
messages.append({"role": "assistant", "content": "β³ Extracting and analyzing data..."})
|
|
|
|
|
|
|
|
|
|
| 133 |
|
| 134 |
extracted_text = extract_text_from_excel(file.name)
|
| 135 |
chunks = split_text_into_chunks(extracted_text)
|
|
|
|
| 137 |
|
| 138 |
for i, chunk in enumerate(chunks):
|
| 139 |
messages.append({"role": "assistant", "content": f"π Analyzing chunk {i+1}/{len(chunks)}..."})
|
|
|
|
| 140 |
|
| 141 |
prompt = build_prompt_from_text(chunk)
|
| 142 |
response = ""
|
|
|
|
| 160 |
|
| 161 |
chunk_responses.append(clean_response(response))
|
| 162 |
messages.append({"role": "assistant", "content": f"β
Chunk {i+1} analysis complete"})
|
|
|
|
| 163 |
|
| 164 |
final_prompt = "\n\n".join(chunk_responses) + "\n\nSummarize the key findings above."
|
| 165 |
messages.append({"role": "assistant", "content": "π Generating final report..."})
|
|
|
|
| 166 |
|
| 167 |
stream_text = ""
|
| 168 |
for result in agent.run_gradio_chat(
|
|
|
|
| 182 |
for r in result:
|
| 183 |
if hasattr(r, "content"):
|
| 184 |
stream_text += r.content
|
| 185 |
+
|
|
|
|
|
|
|
|
|
|
| 186 |
final_report = f"# \U0001f9e0 Final Patient Report\n\n{clean_response(stream_text)}"
|
| 187 |
+
messages[-1]["content"] = f"π Final Report:\n\n{clean_response(stream_text)}"
|
| 188 |
+
|
| 189 |
timestamp = datetime.now().strftime('%Y%m%d_%H%M%S')
|
| 190 |
report_path = os.path.join(report_dir, f"report_{timestamp}.md")
|
| 191 |
|
|
|
|
| 194 |
|
| 195 |
messages.append({"role": "assistant", "content": f"β
Report generated and saved: report_{timestamp}.md"})
|
| 196 |
report_output = {"visible": True, "value": report_path}
|
|
|
|
| 197 |
|
| 198 |
except Exception as e:
|
| 199 |
messages.append({"role": "assistant", "content": f"β Error processing file: {str(e)}"})
|
| 200 |
+
|
| 201 |
+
return messages, report_output
|
| 202 |
|
| 203 |
def create_ui(agent):
|
| 204 |
with gr.Blocks(title="Patient History Chat", css=".gradio-container {max-width: 900px !important}") as demo:
|
|
|
|
| 232 |
interactive=False
|
| 233 |
)
|
| 234 |
|
| 235 |
+
# State to maintain chatbot messages
|
| 236 |
+
chatbot_state = gr.State(value=[])
|
| 237 |
+
|
| 238 |
+
def update_ui(file, current_state):
|
| 239 |
+
messages, report_output = process_final_report(agent, file, current_state)
|
| 240 |
+
return messages, report_output, messages
|
| 241 |
+
|
| 242 |
analyze_btn.click(
|
| 243 |
+
fn=update_ui,
|
| 244 |
+
inputs=[file_upload, chatbot_state],
|
| 245 |
+
outputs=[chatbot, report_output, chatbot_state],
|
| 246 |
api_name="analyze"
|
| 247 |
)
|
| 248 |
|