YauheniShe commited on
Commit
2d7d3ef
·
1 Parent(s): d8ebeb8

Пофиксил баг с таймаутом

Browse files
Files changed (1) hide show
  1. app/main.py +11 -60
app/main.py CHANGED
@@ -1,7 +1,5 @@
1
- import asyncio
2
  import base64
3
  import io
4
- import multiprocessing
5
  import os
6
  from contextlib import asynccontextmanager
7
 
@@ -170,62 +168,20 @@ async def get_index(request: Request):
170
  return templates.TemplateResponse("index.html", {"request": request})
171
 
172
 
173
- def _eval_worker(formula_str, mode, return_dict):
174
- """Изолированный рабочий процесс для опасных вычислений SymPy"""
175
- try:
176
- if mode == "render":
177
- x_dense = np.linspace(-10, 10, 500)
178
- formula_str_clean = (
179
- formula_str.replace("^", "**").replace("np.", "").replace("math.", "")
180
- )
181
- t = standard_transformations + (implicit_multiplication_application,)
182
- expr = parse_expr(formula_str_clean, transformations=t)
183
- y_dense = evaluate_expr_to_points(expr, x_dense)
184
- return_dict["result"] = (x_dense.tolist(), y_dense)
185
-
186
- elif mode == "ideal":
187
- x, y, mask = get_ideal_math(formula_str)
188
- return_dict["result"] = (x, y, mask)
189
- except Exception as e:
190
- return_dict["error"] = str(e)
191
-
192
-
193
- async def safe_eval_math(formula_str, mode="render", timeout=2.0):
194
-
195
- loop = asyncio.get_running_loop()
196
-
197
- def _run_process():
198
- manager = multiprocessing.Manager()
199
- return_dict = manager.dict()
200
- p = multiprocessing.Process(
201
- target=_eval_worker, args=(formula_str, mode, return_dict)
202
- )
203
- p.start()
204
- p.join(timeout)
205
-
206
- if p.is_alive():
207
- p.terminate()
208
- p.join()
209
- return {"error": f"Таймаут: слишком сложная формула: {formula_str}"}
210
-
211
- if "error" in return_dict:
212
- return {"error": return_dict["error"]}
213
-
214
- return {"result": return_dict.get("result", (None, None, None))}
215
-
216
- return await loop.run_in_executor(None, _run_process)
217
-
218
-
219
  @app.post("/render_formula")
220
  async def render_formula(req: FormulaRequest):
221
- res = await safe_eval_math(req.formula, mode="render", timeout=2.0)
222
-
223
- if "error" in res:
 
 
 
 
 
 
 
224
  return {"x": [], "y": []}
225
 
226
- x_dense, y_dense = res["result"] # type: ignore
227
- return {"x": x_dense, "y": y_dense}
228
-
229
 
230
  @app.post("/predict")
231
  async def predict(data: PredictRequest):
@@ -239,12 +195,7 @@ async def predict(data: PredictRequest):
239
  else:
240
  if not data.formula:
241
  return {"error": "Введите формулу."}
242
-
243
- res = await safe_eval_math(data.formula, mode="ideal", timeout=2.0)
244
- if "error" in res:
245
- return {"error": res["error"]}
246
-
247
- x_math, y_math, mask = res["result"]
248
 
249
  if mask is None or not np.any(mask):
250
  return {"error": "Не удалось распознать математику."}
 
 
1
  import base64
2
  import io
 
3
  import os
4
  from contextlib import asynccontextmanager
5
 
 
168
  return templates.TemplateResponse("index.html", {"request": request})
169
 
170
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
171
  @app.post("/render_formula")
172
  async def render_formula(req: FormulaRequest):
173
+ x_dense = np.linspace(-10, 10, 500)
174
+ try:
175
+ formula_str = (
176
+ req.formula.replace("^", "**").replace("np.", "").replace("math.", "")
177
+ )
178
+ t = standard_transformations + (implicit_multiplication_application,)
179
+ expr = parse_expr(formula_str, transformations=t)
180
+ y_dense = evaluate_expr_to_points(expr, x_dense)
181
+ return {"x": x_dense.tolist(), "y": y_dense}
182
+ except Exception:
183
  return {"x": [], "y": []}
184
 
 
 
 
185
 
186
  @app.post("/predict")
187
  async def predict(data: PredictRequest):
 
195
  else:
196
  if not data.formula:
197
  return {"error": "Введите формулу."}
198
+ x_math, y_math, mask = get_ideal_math(data.formula)
 
 
 
 
 
199
 
200
  if mask is None or not np.any(mask):
201
  return {"error": "Не удалось распознать математику."}