Ali2206 commited on
Commit
29db05d
Β·
verified Β·
1 Parent(s): 707b929

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +32 -21
app.py CHANGED
@@ -10,6 +10,7 @@ MAX_MODEL_TOKENS = 131072
10
  MAX_NEW_TOKENS = 4096
11
  MAX_CHUNK_TOKENS = 8192
12
  PROMPT_OVERHEAD = 300
 
13
 
14
  # Paths
15
  persistent_dir = "/data/hf_cache"
@@ -83,14 +84,14 @@ def init_agent() -> TxAgent:
83
  agent.init_model()
84
  return agent
85
 
86
- # ⚑ Optimized parallel analysis
87
- def analyze_parallel(agent, chunks: List[str], max_workers: int = 6) -> List[str]:
88
- results = [None] * len(chunks)
89
 
90
- def worker(index, chunk):
91
- prompt = build_prompt(chunk)
92
  if estimate_tokens(prompt) > MAX_MODEL_TOKENS:
93
- return f"❌ Chunk {index+1} too long. Skipped."
94
  response = ""
95
  try:
96
  for r in agent.run_gradio_chat(
@@ -112,16 +113,16 @@ def analyze_parallel(agent, chunks: List[str], max_workers: int = 6) -> List[str
112
  response += r.content
113
  return clean_response(response)
114
  except Exception as e:
115
- return f"❌ Error in chunk {index+1}: {str(e)}"
116
 
117
  with ThreadPoolExecutor(max_workers=max_workers) as executor:
118
- futures = {executor.submit(worker, idx, chunk): idx for idx, chunk in enumerate(chunks)}
119
  for future in futures:
120
  idx = futures[future]
121
  try:
122
  results[idx] = future.result()
123
  except Exception as e:
124
- results[idx] = f"❌ Error in chunk {idx+1}: {str(e)}"
125
 
126
  gc.collect()
127
  return results
@@ -157,13 +158,14 @@ def process_report(agent, file, messages: List[Dict[str, str]]) -> Tuple[List[Di
157
  try:
158
  extracted = extract_text_from_excel(file.name)
159
  chunks = split_text(extracted)
160
- messages.append({"role": "assistant", "content": f"πŸ” Split into {len(chunks)} chunks. Analyzing..."})
 
161
 
162
- chunk_results = analyze_parallel(agent, chunks, max_workers=6)
163
  valid = [res for res in chunk_results if not res.startswith("❌")]
164
 
165
  if not valid:
166
- messages.append({"role": "assistant", "content": "❌ No valid chunk outputs."})
167
  return messages, None
168
 
169
  summary = generate_final_summary(agent, "\n\n".join(valid))
@@ -179,21 +181,20 @@ def process_report(agent, file, messages: List[Dict[str, str]]) -> Tuple[List[Di
179
  messages.append({"role": "assistant", "content": f"❌ Error: {str(e)}"})
180
  return messages, None
181
 
 
182
  def create_ui(agent):
183
  with gr.Blocks(css="""
184
  html, body, .gradio-container {
185
  background-color: #0e1621;
186
  color: #e0e0e0;
187
  font-family: 'Inter', sans-serif;
188
- padding: 0;
189
  margin: 0;
 
190
  }
191
-
192
  h2, h3, h4 {
193
  color: #89b4fa;
194
  font-weight: 600;
195
  }
196
-
197
  button.gr-button-primary {
198
  background-color: #007bff !important;
199
  color: white !important;
@@ -203,26 +204,35 @@ def create_ui(agent):
203
  font-size: 16px !important;
204
  border: none;
205
  }
206
-
207
  button.gr-button-primary:hover {
208
  background-color: #0056b3 !important;
209
  }
210
-
211
  .gr-chatbot, .gr-markdown, .gr-file-upload {
212
  border-radius: 16px;
213
  background-color: #1b2533;
214
  border: 1px solid #2a2f45;
215
  padding: 10px;
216
  }
217
-
218
  .gr-chatbot .message {
219
  font-size: 16px;
220
  line-height: 1.6;
221
- white-space: pre-wrap;
222
- word-break: break-word;
223
  margin: 8px 0;
 
 
 
 
 
 
 
 
 
 
 
 
 
224
  }
225
-
226
  .gr-file-upload .file-name {
227
  font-size: 14px;
228
  color: #89b4fa;
@@ -258,3 +268,4 @@ if __name__ == "__main__":
258
  except Exception as err:
259
  print(f"Startup failed: {err}")
260
  sys.exit(1)
 
 
10
  MAX_NEW_TOKENS = 4096
11
  MAX_CHUNK_TOKENS = 8192
12
  PROMPT_OVERHEAD = 300
13
+ BATCH_SIZE = 3 # NEW: batching chunks to avoid overload
14
 
15
  # Paths
16
  persistent_dir = "/data/hf_cache"
 
84
  agent.init_model()
85
  return agent
86
 
87
+ # ⚑ Parallel analyzer with batching
88
+ def analyze_parallel(agent, batch_chunks: List[List[str]], max_workers: int = 3) -> List[str]:
89
+ results = [None] * len(batch_chunks)
90
 
91
+ def worker(index, batch):
92
+ prompt = "\n\n".join(build_prompt(chunk) for chunk in batch)
93
  if estimate_tokens(prompt) > MAX_MODEL_TOKENS:
94
+ return f"❌ Batch {index+1} too long. Skipped."
95
  response = ""
96
  try:
97
  for r in agent.run_gradio_chat(
 
113
  response += r.content
114
  return clean_response(response)
115
  except Exception as e:
116
+ return f"❌ Error in batch {index+1}: {str(e)}"
117
 
118
  with ThreadPoolExecutor(max_workers=max_workers) as executor:
119
+ futures = {executor.submit(worker, idx, batch): idx for idx, batch in enumerate(batch_chunks)}
120
  for future in futures:
121
  idx = futures[future]
122
  try:
123
  results[idx] = future.result()
124
  except Exception as e:
125
+ results[idx] = f"❌ Error in batch {idx+1}: {str(e)}"
126
 
127
  gc.collect()
128
  return results
 
158
  try:
159
  extracted = extract_text_from_excel(file.name)
160
  chunks = split_text(extracted)
161
+ batch_chunks = [chunks[i:i+BATCH_SIZE] for i in range(0, len(chunks), BATCH_SIZE)]
162
+ messages.append({"role": "assistant", "content": f"πŸ” Split into {len(batch_chunks)} batches. Analyzing..."})
163
 
164
+ chunk_results = analyze_parallel(agent, batch_chunks, max_workers=3)
165
  valid = [res for res in chunk_results if not res.startswith("❌")]
166
 
167
  if not valid:
168
+ messages.append({"role": "assistant", "content": "❌ No valid batch outputs."})
169
  return messages, None
170
 
171
  summary = generate_final_summary(agent, "\n\n".join(valid))
 
181
  messages.append({"role": "assistant", "content": f"❌ Error: {str(e)}"})
182
  return messages, None
183
 
184
+
185
  def create_ui(agent):
186
  with gr.Blocks(css="""
187
  html, body, .gradio-container {
188
  background-color: #0e1621;
189
  color: #e0e0e0;
190
  font-family: 'Inter', sans-serif;
 
191
  margin: 0;
192
+ padding: 0;
193
  }
 
194
  h2, h3, h4 {
195
  color: #89b4fa;
196
  font-weight: 600;
197
  }
 
198
  button.gr-button-primary {
199
  background-color: #007bff !important;
200
  color: white !important;
 
204
  font-size: 16px !important;
205
  border: none;
206
  }
 
207
  button.gr-button-primary:hover {
208
  background-color: #0056b3 !important;
209
  }
 
210
  .gr-chatbot, .gr-markdown, .gr-file-upload {
211
  border-radius: 16px;
212
  background-color: #1b2533;
213
  border: 1px solid #2a2f45;
214
  padding: 10px;
215
  }
 
216
  .gr-chatbot .message {
217
  font-size: 16px;
218
  line-height: 1.6;
219
+ padding: 12px 16px;
220
+ border-radius: 18px;
221
  margin: 8px 0;
222
+ max-width: 80%;
223
+ word-break: break-word;
224
+ white-space: pre-wrap;
225
+ }
226
+ .gr-chatbot .message.user {
227
+ background-color: #334155;
228
+ align-self: flex-end;
229
+ margin-left: auto;
230
+ }
231
+ .gr-chatbot .message.assistant {
232
+ background-color: #1e293b;
233
+ align-self: flex-start;
234
+ margin-right: auto;
235
  }
 
236
  .gr-file-upload .file-name {
237
  font-size: 14px;
238
  color: #89b4fa;
 
268
  except Exception as err:
269
  print(f"Startup failed: {err}")
270
  sys.exit(1)
271
+