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
|
@@ -126,8 +126,13 @@ def init_agent():
|
|
126 |
return agent
|
127 |
|
128 |
|
129 |
-
def
|
130 |
-
|
|
|
|
|
|
|
|
|
|
|
131 |
response = ""
|
132 |
for result in agent.run_gradio_chat(
|
133 |
message=prompt,
|
@@ -138,45 +143,44 @@ def analyze_with_agent(agent, prompt: str) -> str:
|
|
138 |
call_agent=False,
|
139 |
conversation=[],
|
140 |
):
|
141 |
-
if isinstance(result,
|
|
|
|
|
|
|
|
|
142 |
for r in result:
|
143 |
-
if hasattr(r,
|
144 |
-
response +=
|
145 |
-
|
146 |
-
|
147 |
-
|
148 |
-
|
149 |
-
|
150 |
-
|
151 |
-
|
152 |
-
|
153 |
-
|
154 |
-
|
155 |
-
|
156 |
-
|
157 |
-
|
158 |
-
|
159 |
-
|
160 |
-
|
161 |
-
|
162 |
-
|
163 |
-
|
164 |
-
|
165 |
-
|
166 |
-
|
167 |
-
|
168 |
-
|
169 |
-
|
170 |
-
|
171 |
-
|
172 |
-
|
173 |
-
|
174 |
-
|
175 |
-
|
176 |
-
return [("user", f"[Excel Uploaded: {file.name}]"), ("assistant", full_report)], report_path
|
177 |
-
|
178 |
-
except Exception as e:
|
179 |
-
raise gr.Error(f"Error: {str(e)}")
|
180 |
|
181 |
|
182 |
def create_ui(agent):
|
@@ -186,8 +190,8 @@ def create_ui(agent):
|
|
186 |
analyze_btn = gr.Button("🧠 Analyze Patient History")
|
187 |
report_output = gr.File(label="Download Report")
|
188 |
|
189 |
-
analyze_btn.
|
190 |
-
|
191 |
inputs=[file_upload],
|
192 |
outputs=[chatbot, report_output]
|
193 |
)
|
@@ -207,4 +211,4 @@ if __name__ == "__main__":
|
|
207 |
)
|
208 |
except Exception as e:
|
209 |
print(f"Error: {str(e)}")
|
210 |
-
sys.exit(1)
|
|
|
3 |
import pandas as pd
|
4 |
import json
|
5 |
import gradio as gr
|
6 |
+
from typing import List, Tuple, Dict, Any, Generator, Union
|
7 |
import hashlib
|
8 |
import shutil
|
9 |
import re
|
|
|
126 |
return agent
|
127 |
|
128 |
|
129 |
+
def stream_final_report(agent, file) -> Generator[Union[Dict[str, str], Tuple[List[Dict[str, str]], str]], None, None]:
|
130 |
+
extracted_text = extract_text_from_excel(file.name)
|
131 |
+
chunks = split_text_into_chunks(extracted_text)
|
132 |
+
chunk_responses = []
|
133 |
+
|
134 |
+
for chunk in chunks:
|
135 |
+
prompt = build_prompt_from_text(chunk)
|
136 |
response = ""
|
137 |
for result in agent.run_gradio_chat(
|
138 |
message=prompt,
|
|
|
143 |
call_agent=False,
|
144 |
conversation=[],
|
145 |
):
|
146 |
+
if isinstance(result, str):
|
147 |
+
response += result
|
148 |
+
elif hasattr(result, "content"):
|
149 |
+
response += result.content
|
150 |
+
elif isinstance(result, list):
|
151 |
for r in result:
|
152 |
+
if hasattr(r, "content"):
|
153 |
+
response += r.content
|
154 |
+
chunk_responses.append(clean_response(response))
|
155 |
+
|
156 |
+
final_prompt = "\n\n".join(chunk_responses) + "\n\nSummarize the key findings above."
|
157 |
+
yield {"role": "user", "content": f"[Excel Uploaded: {file.name}]"}
|
158 |
+
stream_text = ""
|
159 |
+
for result in agent.run_gradio_chat(
|
160 |
+
message=final_prompt,
|
161 |
+
history=[],
|
162 |
+
temperature=0.2,
|
163 |
+
max_new_tokens=MAX_NEW_TOKENS,
|
164 |
+
max_token=MAX_TOKENS,
|
165 |
+
call_agent=False,
|
166 |
+
conversation=[],
|
167 |
+
):
|
168 |
+
if isinstance(result, str):
|
169 |
+
stream_text += result
|
170 |
+
elif hasattr(result, "content"):
|
171 |
+
stream_text += result.content
|
172 |
+
elif isinstance(result, list):
|
173 |
+
for r in result:
|
174 |
+
if hasattr(r, "content"):
|
175 |
+
stream_text += r.content
|
176 |
+
yield {"role": "assistant", "content": clean_response(stream_text)}
|
177 |
+
|
178 |
+
final_report = f"# \U0001f9e0 Final Patient Report\n\n{clean_response(stream_text)}"
|
179 |
+
report_path = os.path.join(report_dir, f"report_{datetime.now().strftime('%Y%m%d_%H%M%S')}.md")
|
180 |
+
with open(report_path, 'w') as f:
|
181 |
+
f.write(final_report)
|
182 |
+
|
183 |
+
yield [{"role": "user", "content": f"[Excel Uploaded: {file.name}]"}, {"role": "assistant", "content": final_report}], report_path
|
|
|
|
|
|
|
|
|
|
|
184 |
|
185 |
|
186 |
def create_ui(agent):
|
|
|
190 |
analyze_btn = gr.Button("🧠 Analyze Patient History")
|
191 |
report_output = gr.File(label="Download Report")
|
192 |
|
193 |
+
analyze_btn.stream(
|
194 |
+
fn=stream_final_report,
|
195 |
inputs=[file_upload],
|
196 |
outputs=[chatbot, report_output]
|
197 |
)
|
|
|
211 |
)
|
212 |
except Exception as e:
|
213 |
print(f"Error: {str(e)}")
|
214 |
+
sys.exit(1)
|