Update app.py
Browse files
app.py
CHANGED
@@ -109,7 +109,59 @@ def init_agent():
|
|
109 |
return agent
|
110 |
|
111 |
def stream_report(agent, file: Union[str, 'file'], full_output: str) -> Generator[Tuple[str, Union[str, None], str], None, None]:
|
112 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
113 |
|
114 |
def create_ui(agent):
|
115 |
with gr.Blocks(css="""
|
|
|
109 |
return agent
|
110 |
|
111 |
def stream_report(agent, file: Union[str, 'file'], full_output: str) -> Generator[Tuple[str, Union[str, None], str], None, None]:
|
112 |
+
accumulated_text = ""
|
113 |
+
try:
|
114 |
+
if file is None:
|
115 |
+
yield "❌ Please upload a valid Excel file.", None, ""
|
116 |
+
return
|
117 |
+
|
118 |
+
if hasattr(file, "read"):
|
119 |
+
text = extract_text_from_excel(file)
|
120 |
+
elif isinstance(file, str) and os.path.exists(file):
|
121 |
+
text = extract_text_from_excel(file)
|
122 |
+
else:
|
123 |
+
raise ValueError("❌ Invalid or missing file.")
|
124 |
+
|
125 |
+
chunks = split_text_into_chunks(text)
|
126 |
+
|
127 |
+
for i, chunk in enumerate(chunks):
|
128 |
+
prompt = build_prompt_from_text(chunk)
|
129 |
+
partial = ""
|
130 |
+
for res in agent.run_gradio_chat(
|
131 |
+
message=prompt, history=[], temperature=0.2,
|
132 |
+
max_new_tokens=MAX_NEW_TOKENS, max_token=MAX_MODEL_TOKENS,
|
133 |
+
call_agent=False, conversation=[]
|
134 |
+
):
|
135 |
+
if isinstance(res, str):
|
136 |
+
partial += res
|
137 |
+
elif hasattr(res, "content"):
|
138 |
+
partial += res.content
|
139 |
+
cleaned = clean_response(partial)
|
140 |
+
accumulated_text += f"\n\n📄 **Chunk {i+1}**:\n{cleaned}"
|
141 |
+
yield accumulated_text, None, ""
|
142 |
+
|
143 |
+
summary_prompt = f"Summarize this analysis in a final structured report:\n\n" + accumulated_text
|
144 |
+
final_report = ""
|
145 |
+
for res in agent.run_gradio_chat(
|
146 |
+
message=summary_prompt, history=[], temperature=0.2,
|
147 |
+
max_new_tokens=MAX_NEW_TOKENS, max_token=MAX_MODEL_TOKENS,
|
148 |
+
call_agent=False, conversation=[]
|
149 |
+
):
|
150 |
+
if isinstance(res, str):
|
151 |
+
final_report += res
|
152 |
+
elif hasattr(res, "content"):
|
153 |
+
final_report += res.content
|
154 |
+
|
155 |
+
cleaned = clean_response(final_report)
|
156 |
+
accumulated_text += f"\n\n📊 **Final Summary**:\n{cleaned}"
|
157 |
+
report_path = os.path.join(report_dir, f"report_{datetime.now().strftime('%Y%m%d_%H%M%S')}.md")
|
158 |
+
with open(report_path, 'w') as f:
|
159 |
+
f.write(f"# 🧠 Final Patient Report\n\n{cleaned}")
|
160 |
+
|
161 |
+
yield accumulated_text, report_path, cleaned
|
162 |
+
|
163 |
+
except Exception as e:
|
164 |
+
yield f"❌ Error: {str(e)}", None, ""
|
165 |
|
166 |
def create_ui(agent):
|
167 |
with gr.Blocks(css="""
|