Update app.py
Browse files
app.py
CHANGED
|
@@ -83,13 +83,14 @@ def init_agent() -> TxAgent:
|
|
| 83 |
agent.init_model()
|
| 84 |
return agent
|
| 85 |
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
|
|
|
|
|
|
| 89 |
prompt = build_prompt(chunk)
|
| 90 |
if estimate_tokens(prompt) > MAX_MODEL_TOKENS:
|
| 91 |
-
|
| 92 |
-
continue
|
| 93 |
response = ""
|
| 94 |
try:
|
| 95 |
for r in agent.run_gradio_chat(
|
|
@@ -109,10 +110,20 @@ def analyze_serial(agent, chunks: List[str]) -> List[str]:
|
|
| 109 |
response += m.content
|
| 110 |
elif hasattr(r, "content"):
|
| 111 |
response += r.content
|
| 112 |
-
|
| 113 |
-
results.append(clean_response(response))
|
| 114 |
except Exception as e:
|
| 115 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 116 |
return results
|
| 117 |
|
| 118 |
def generate_final_summary(agent, combined: str) -> str:
|
|
@@ -148,7 +159,7 @@ def process_report(agent, file, messages: List[Dict[str, str]]) -> Tuple[List[Di
|
|
| 148 |
chunks = split_text(extracted)
|
| 149 |
messages.append({"role": "assistant", "content": f"π Split into {len(chunks)} chunks. Analyzing..."})
|
| 150 |
|
| 151 |
-
chunk_results =
|
| 152 |
valid = [res for res in chunk_results if not res.startswith("β")]
|
| 153 |
|
| 154 |
if not valid:
|
|
|
|
| 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(
|
|
|
|
| 110 |
response += m.content
|
| 111 |
elif hasattr(r, "content"):
|
| 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
|
| 128 |
|
| 129 |
def generate_final_summary(agent, combined: str) -> str:
|
|
|
|
| 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:
|