Spaces:
Sleeping
Sleeping
Update backend.py
Browse files- backend.py +4 -8
backend.py
CHANGED
|
@@ -1,9 +1,9 @@
|
|
| 1 |
-
from fastapi import
|
| 2 |
from pydantic import BaseModel, field_validator
|
| 3 |
import math
|
| 4 |
import re
|
| 5 |
|
| 6 |
-
|
| 7 |
|
| 8 |
ALLOWED_FUNCTIONS = {'sin', 'cos', 'tan', 'log', 'ln', 'sqrt', 'abs', 'pow'}
|
| 9 |
ALLOWED_CONSTANTS = {'pi', 'e'}
|
|
@@ -31,7 +31,7 @@ class Expression(BaseModel):
|
|
| 31 |
|
| 32 |
return v
|
| 33 |
|
| 34 |
-
@
|
| 35 |
async def calculate_api(expression: Expression):
|
| 36 |
try:
|
| 37 |
result = evaluate_expression(expression.expr)
|
|
@@ -57,8 +57,4 @@ def evaluate_expression(expr: str) -> float:
|
|
| 57 |
except ValueError as e:
|
| 58 |
raise ValueError(f"Math domain error: {str(e)}")
|
| 59 |
except Exception as e:
|
| 60 |
-
raise ValueError(f"Invalid expression: {str(e)}")
|
| 61 |
-
|
| 62 |
-
if __name__ == "__main__":
|
| 63 |
-
import uvicorn
|
| 64 |
-
uvicorn.run(app, host="0.0.0.0", port=8000)
|
|
|
|
| 1 |
+
from fastapi import APIRouter, HTTPException
|
| 2 |
from pydantic import BaseModel, field_validator
|
| 3 |
import math
|
| 4 |
import re
|
| 5 |
|
| 6 |
+
router = APIRouter()
|
| 7 |
|
| 8 |
ALLOWED_FUNCTIONS = {'sin', 'cos', 'tan', 'log', 'ln', 'sqrt', 'abs', 'pow'}
|
| 9 |
ALLOWED_CONSTANTS = {'pi', 'e'}
|
|
|
|
| 31 |
|
| 32 |
return v
|
| 33 |
|
| 34 |
+
@router.post("/calculate")
|
| 35 |
async def calculate_api(expression: Expression):
|
| 36 |
try:
|
| 37 |
result = evaluate_expression(expression.expr)
|
|
|
|
| 57 |
except ValueError as e:
|
| 58 |
raise ValueError(f"Math domain error: {str(e)}")
|
| 59 |
except Exception as e:
|
| 60 |
+
raise ValueError(f"Invalid expression: {str(e)}")
|
|
|
|
|
|
|
|
|
|
|
|