Update app.py
Browse files
app.py
CHANGED
@@ -35,7 +35,6 @@ from txagent.txagent import TxAgent
|
|
35 |
MAX_TOKENS = 32768
|
36 |
MAX_NEW_TOKENS = 2048
|
37 |
|
38 |
-
|
39 |
def clean_response(text: str) -> str:
|
40 |
try:
|
41 |
text = text.encode('utf-8', 'surrogatepass').decode('utf-8')
|
@@ -46,11 +45,9 @@ def clean_response(text: str) -> str:
|
|
46 |
text = re.sub(r"[^\n#\-\*\w\s\.,:\(\)]+", "", text)
|
47 |
return text.strip()
|
48 |
|
49 |
-
|
50 |
def estimate_tokens(text: str) -> int:
|
51 |
return len(text) // 3.5
|
52 |
|
53 |
-
|
54 |
def extract_text_from_excel(file_path: str) -> str:
|
55 |
all_text = []
|
56 |
xls = pd.ExcelFile(file_path)
|
@@ -62,7 +59,6 @@ def extract_text_from_excel(file_path: str) -> str:
|
|
62 |
all_text.extend(sheet_text)
|
63 |
return "\n".join(all_text)
|
64 |
|
65 |
-
|
66 |
def split_text_into_chunks(text: str, max_tokens: int = MAX_TOKENS) -> List[str]:
|
67 |
lines = text.split("\n")
|
68 |
chunks = []
|
@@ -83,7 +79,6 @@ def split_text_into_chunks(text: str, max_tokens: int = MAX_TOKENS) -> List[str]
|
|
83 |
chunks.append("\n".join(current_chunk))
|
84 |
return chunks
|
85 |
|
86 |
-
|
87 |
def build_prompt_from_text(chunk: str) -> str:
|
88 |
return f"""
|
89 |
### Unstructured Clinical Records
|
@@ -104,7 +99,6 @@ Please analyze the above and provide:
|
|
104 |
- Follow-up Recommendations
|
105 |
"""
|
106 |
|
107 |
-
|
108 |
def init_agent():
|
109 |
default_tool_path = os.path.abspath("data/new_tool.json")
|
110 |
target_tool_path = os.path.join(tool_cache_dir, "new_tool.json")
|
@@ -125,31 +119,29 @@ def init_agent():
|
|
125 |
agent.init_model()
|
126 |
return agent
|
127 |
|
128 |
-
|
129 |
-
def stream_final_report(agent, file) -> Generator[Tuple[List[Dict[str, str]], Union[str, None]], None, None]:
|
130 |
-
# Initialize with empty values
|
131 |
messages = []
|
132 |
-
|
133 |
-
|
134 |
if file is None or not hasattr(file, "name"):
|
135 |
messages = [{"role": "assistant", "content": "β Please upload a valid Excel file before analyzing."}]
|
136 |
-
yield messages,
|
137 |
return
|
138 |
|
139 |
try:
|
140 |
-
|
141 |
-
|
142 |
-
|
143 |
-
|
|
|
144 |
|
145 |
extracted_text = extract_text_from_excel(file.name)
|
146 |
chunks = split_text_into_chunks(extracted_text)
|
147 |
chunk_responses = []
|
148 |
|
149 |
-
# Process each chunk
|
150 |
for i, chunk in enumerate(chunks):
|
151 |
messages.append({"role": "assistant", "content": f"π Analyzing chunk {i+1}/{len(chunks)}..."})
|
152 |
-
yield messages,
|
153 |
|
154 |
prompt = build_prompt_from_text(chunk)
|
155 |
response = ""
|
@@ -173,12 +165,11 @@ def stream_final_report(agent, file) -> Generator[Tuple[List[Dict[str, str]], Un
|
|
173 |
|
174 |
chunk_responses.append(clean_response(response))
|
175 |
messages.append({"role": "assistant", "content": f"β
Chunk {i+1} analysis complete"})
|
176 |
-
yield messages,
|
177 |
|
178 |
-
# Final summarization
|
179 |
final_prompt = "\n\n".join(chunk_responses) + "\n\nSummarize the key findings above."
|
180 |
messages.append({"role": "assistant", "content": "π Generating final report..."})
|
181 |
-
yield messages,
|
182 |
|
183 |
stream_text = ""
|
184 |
for result in agent.run_gradio_chat(
|
@@ -200,9 +191,8 @@ def stream_final_report(agent, file) -> Generator[Tuple[List[Dict[str, str]], Un
|
|
200 |
stream_text += r.content
|
201 |
|
202 |
messages[-1]["content"] = f"π Generating final report...\n\n{clean_response(stream_text)}"
|
203 |
-
yield messages,
|
204 |
|
205 |
-
# Save final report
|
206 |
final_report = f"# \U0001f9e0 Final Patient Report\n\n{clean_response(stream_text)}"
|
207 |
timestamp = datetime.now().strftime('%Y%m%d_%H%M%S')
|
208 |
report_path = os.path.join(report_dir, f"report_{timestamp}.md")
|
@@ -211,12 +201,12 @@ def stream_final_report(agent, file) -> Generator[Tuple[List[Dict[str, str]], Un
|
|
211 |
f.write(final_report)
|
212 |
|
213 |
messages.append({"role": "assistant", "content": f"β
Report generated and saved: report_{timestamp}.md"})
|
214 |
-
|
|
|
215 |
|
216 |
except Exception as e:
|
217 |
messages.append({"role": "assistant", "content": f"β Error processing file: {str(e)}"})
|
218 |
-
yield messages,
|
219 |
-
|
220 |
|
221 |
def create_ui(agent):
|
222 |
with gr.Blocks(title="Patient History Chat", css=".gradio-container {max-width: 900px !important}") as demo:
|
@@ -230,8 +220,8 @@ def create_ui(agent):
|
|
230 |
height=600,
|
231 |
type="messages",
|
232 |
avatar_images=(
|
233 |
-
None,
|
234 |
-
"https://i.imgur.com/6wX7Zb4.png"
|
235 |
)
|
236 |
)
|
237 |
with gr.Column(scale=1):
|
@@ -257,14 +247,8 @@ def create_ui(agent):
|
|
257 |
api_name="analyze"
|
258 |
)
|
259 |
|
260 |
-
def show_report(report_path):
|
261 |
-
if report_path:
|
262 |
-
return gr.File(visible=True, value=report_path)
|
263 |
-
return gr.File(visible=False)
|
264 |
-
|
265 |
return demo
|
266 |
|
267 |
-
|
268 |
if __name__ == "__main__":
|
269 |
try:
|
270 |
agent = init_agent()
|
|
|
35 |
MAX_TOKENS = 32768
|
36 |
MAX_NEW_TOKENS = 2048
|
37 |
|
|
|
38 |
def clean_response(text: str) -> str:
|
39 |
try:
|
40 |
text = text.encode('utf-8', 'surrogatepass').decode('utf-8')
|
|
|
45 |
text = re.sub(r"[^\n#\-\*\w\s\.,:\(\)]+", "", text)
|
46 |
return text.strip()
|
47 |
|
|
|
48 |
def estimate_tokens(text: str) -> int:
|
49 |
return len(text) // 3.5
|
50 |
|
|
|
51 |
def extract_text_from_excel(file_path: str) -> str:
|
52 |
all_text = []
|
53 |
xls = pd.ExcelFile(file_path)
|
|
|
59 |
all_text.extend(sheet_text)
|
60 |
return "\n".join(all_text)
|
61 |
|
|
|
62 |
def split_text_into_chunks(text: str, max_tokens: int = MAX_TOKENS) -> List[str]:
|
63 |
lines = text.split("\n")
|
64 |
chunks = []
|
|
|
79 |
chunks.append("\n".join(current_chunk))
|
80 |
return chunks
|
81 |
|
|
|
82 |
def build_prompt_from_text(chunk: str) -> str:
|
83 |
return f"""
|
84 |
### Unstructured Clinical Records
|
|
|
99 |
- Follow-up Recommendations
|
100 |
"""
|
101 |
|
|
|
102 |
def init_agent():
|
103 |
default_tool_path = os.path.abspath("data/new_tool.json")
|
104 |
target_tool_path = os.path.join(tool_cache_dir, "new_tool.json")
|
|
|
119 |
agent.init_model()
|
120 |
return agent
|
121 |
|
122 |
+
def stream_final_report(agent, file) -> Generator[Tuple[List[Dict[str, str]], Dict[str, Any]], None, None]:
|
|
|
|
|
123 |
messages = []
|
124 |
+
report_output = {"visible": False, "value": None}
|
125 |
+
|
126 |
if file is None or not hasattr(file, "name"):
|
127 |
messages = [{"role": "assistant", "content": "β Please upload a valid Excel file before analyzing."}]
|
128 |
+
yield messages, report_output
|
129 |
return
|
130 |
|
131 |
try:
|
132 |
+
messages = [
|
133 |
+
{"role": "user", "content": f"Processing Excel file: {os.path.basename(file.name)}"},
|
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)
|
140 |
chunk_responses = []
|
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 |
|
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(
|
|
|
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")
|
|
|
201 |
f.write(final_report)
|
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 |
+
yield messages, report_output
|
|
|
210 |
|
211 |
def create_ui(agent):
|
212 |
with gr.Blocks(title="Patient History Chat", css=".gradio-container {max-width: 900px !important}") as demo:
|
|
|
220 |
height=600,
|
221 |
type="messages",
|
222 |
avatar_images=(
|
223 |
+
None,
|
224 |
+
"https://i.imgur.com/6wX7Zb4.png"
|
225 |
)
|
226 |
)
|
227 |
with gr.Column(scale=1):
|
|
|
247 |
api_name="analyze"
|
248 |
)
|
249 |
|
|
|
|
|
|
|
|
|
|
|
250 |
return demo
|
251 |
|
|
|
252 |
if __name__ == "__main__":
|
253 |
try:
|
254 |
agent = init_agent()
|