Update app.py
Browse files
app.py
CHANGED
@@ -108,63 +108,8 @@ def init_agent():
|
|
108 |
agent.init_model()
|
109 |
return agent
|
110 |
|
111 |
-
def
|
112 |
-
|
113 |
-
accumulated_text = ""
|
114 |
-
try:
|
115 |
-
if file is None:
|
116 |
-
yield "❌ Please upload a valid Excel file.", None, ""
|
117 |
-
return
|
118 |
-
|
119 |
-
if hasattr(file, "read"):
|
120 |
-
text = extract_text_from_excel(file)
|
121 |
-
elif isinstance(file, str) and os.path.exists(file):
|
122 |
-
text = extract_text_from_excel(file)
|
123 |
-
else:
|
124 |
-
raise ValueError("❌ Invalid or missing file.")
|
125 |
-
|
126 |
-
chunks = split_text_into_chunks(text)
|
127 |
-
|
128 |
-
for i, chunk in enumerate(chunks):
|
129 |
-
prompt = build_prompt_from_text(chunk)
|
130 |
-
partial = ""
|
131 |
-
for res in agent.run_gradio_chat(
|
132 |
-
message=prompt, history=[], temperature=0.2,
|
133 |
-
max_new_tokens=MAX_NEW_TOKENS, max_token=MAX_MODEL_TOKENS,
|
134 |
-
call_agent=False, conversation=[]
|
135 |
-
):
|
136 |
-
if isinstance(res, str):
|
137 |
-
partial += res
|
138 |
-
elif hasattr(res, "content"):
|
139 |
-
partial += res.content
|
140 |
-
cleaned = clean_response(partial)
|
141 |
-
accumulated_text += f"\n\n📄 **Chunk {i+1}**:\n{cleaned}"
|
142 |
-
yield accumulated_text, None, ""
|
143 |
-
|
144 |
-
summary_prompt = f"Summarize this analysis in a final structured report:\n\n" + accumulated_text
|
145 |
-
final_report = ""
|
146 |
-
for res in agent.run_gradio_chat(
|
147 |
-
message=summary_prompt, history=[], temperature=0.2,
|
148 |
-
max_new_tokens=MAX_NEW_TOKENS, max_token=MAX_MODEL_TOKENS,
|
149 |
-
call_agent=False, conversation=[]
|
150 |
-
):
|
151 |
-
if isinstance(res, str):
|
152 |
-
final_report += res
|
153 |
-
elif hasattr(res, "content"):
|
154 |
-
final_report += res.content
|
155 |
-
|
156 |
-
cleaned = clean_response(final_report)
|
157 |
-
accumulated_text += f"\n\n📊 **Final Summary**:\n{cleaned}"
|
158 |
-
report_path = os.path.join(report_dir, f"report_{datetime.now().strftime('%Y%m%d_%H%M%S')}.md")
|
159 |
-
with open(report_path, 'w') as f:
|
160 |
-
f.write(f"# 🧠 Final Patient Report\n\n{cleaned}")
|
161 |
-
|
162 |
-
yield accumulated_text, report_path, cleaned
|
163 |
-
|
164 |
-
except Exception as e:
|
165 |
-
yield f"❌ Error: {str(e)}", None, ""
|
166 |
-
|
167 |
-
return wrapped
|
168 |
|
169 |
def create_ui(agent):
|
170 |
with gr.Blocks(css="""
|
@@ -212,13 +157,8 @@ Upload clinical Excel records below and click **Analyze** to generate a medical
|
|
212 |
report_file = gr.File(label="Download Report", visible=False)
|
213 |
full_output = gr.State(value="")
|
214 |
|
215 |
-
streaming_fn = stream_report_wrapper(agent)
|
216 |
-
|
217 |
-
def run_streaming(file, state):
|
218 |
-
return streaming_fn(file, state)
|
219 |
-
|
220 |
analyze_btn.click(
|
221 |
-
fn=
|
222 |
inputs=[file_upload, full_output],
|
223 |
outputs=[report_output_markdown, report_file, full_output]
|
224 |
)
|
@@ -229,7 +169,7 @@ if __name__ == "__main__":
|
|
229 |
try:
|
230 |
agent = init_agent()
|
231 |
demo = create_ui(agent)
|
232 |
-
demo.launch(server_name="0.0.0.0", server_port=7860, allowed_paths=["/data/hf_cache/reports"], share=
|
233 |
except Exception as e:
|
234 |
print(f"Error: {str(e)}")
|
235 |
sys.exit(1)
|
|
|
108 |
agent.init_model()
|
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 |
+
yield from stream_report_wrapper(agent)(file, full_output)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
113 |
|
114 |
def create_ui(agent):
|
115 |
with gr.Blocks(css="""
|
|
|
157 |
report_file = gr.File(label="Download Report", visible=False)
|
158 |
full_output = gr.State(value="")
|
159 |
|
|
|
|
|
|
|
|
|
|
|
160 |
analyze_btn.click(
|
161 |
+
fn=stream_report,
|
162 |
inputs=[file_upload, full_output],
|
163 |
outputs=[report_output_markdown, report_file, full_output]
|
164 |
)
|
|
|
169 |
try:
|
170 |
agent = init_agent()
|
171 |
demo = create_ui(agent)
|
172 |
+
demo.launch(server_name="0.0.0.0", server_port=7860, allowed_paths=["/data/hf_cache/reports"], share=True)
|
173 |
except Exception as e:
|
174 |
print(f"Error: {str(e)}")
|
175 |
sys.exit(1)
|