Spaces:
Running on CPU Upgrade
Running on CPU Upgrade
Upload data_updating_scripts/generate_reports.py with huggingface_hub
Browse files
data_updating_scripts/generate_reports.py
CHANGED
|
@@ -152,17 +152,24 @@ def create_detailed_report(
|
|
| 152 |
bill_json = json.dumps(bill_copy, ensure_ascii=False, indent=2)
|
| 153 |
|
| 154 |
# Thread-based 180-second timeout — signal.alarm doesn't work because
|
| 155 |
-
# the openai SDK resets SIGALRM internally
|
|
|
|
|
|
|
|
|
|
| 156 |
def _do_invoke():
|
| 157 |
chain = DETAILED_REPORT_PROMPT | llm
|
| 158 |
return chain.invoke({"bill_json": bill_json})
|
| 159 |
|
| 160 |
-
|
| 161 |
-
|
| 162 |
-
|
| 163 |
-
|
| 164 |
-
|
| 165 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 166 |
|
| 167 |
# result can be AIMessage; get text
|
| 168 |
report_text = getattr(result, "content", str(result))
|
|
|
|
| 152 |
bill_json = json.dumps(bill_copy, ensure_ascii=False, indent=2)
|
| 153 |
|
| 154 |
# Thread-based 180-second timeout — signal.alarm doesn't work because
|
| 155 |
+
# the openai SDK resets SIGALRM internally.
|
| 156 |
+
# IMPORTANT: Don't use `with ThreadPoolExecutor` — the context manager's
|
| 157 |
+
# __exit__ calls shutdown(wait=True) which blocks forever if the thread
|
| 158 |
+
# is stuck in a deadlocked HTTP connection.
|
| 159 |
def _do_invoke():
|
| 160 |
chain = DETAILED_REPORT_PROMPT | llm
|
| 161 |
return chain.invoke({"bill_json": bill_json})
|
| 162 |
|
| 163 |
+
executor = ThreadPoolExecutor(max_workers=1)
|
| 164 |
+
future = executor.submit(_do_invoke)
|
| 165 |
+
try:
|
| 166 |
+
result = future.result(timeout=180)
|
| 167 |
+
except FuturesTimeout:
|
| 168 |
+
future.cancel()
|
| 169 |
+
executor.shutdown(wait=False)
|
| 170 |
+
raise TimeoutError("API call exceeded 180s hard timeout")
|
| 171 |
+
else:
|
| 172 |
+
executor.shutdown(wait=False)
|
| 173 |
|
| 174 |
# result can be AIMessage; get text
|
| 175 |
report_text = getattr(result, "content", str(result))
|