Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,24 +1,66 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
from fastapi import FastAPI
|
| 3 |
-
from
|
| 4 |
-
from backend import router as backend_router
|
| 5 |
from frontend import create_interface
|
|
|
|
|
|
|
| 6 |
import os
|
| 7 |
|
| 8 |
-
# Create the main FastAPI app
|
| 9 |
app = FastAPI()
|
| 10 |
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
CORSMiddleware,
|
| 14 |
-
allow_origins=["*"],
|
| 15 |
-
allow_credentials=True,
|
| 16 |
-
allow_methods=["*"],
|
| 17 |
-
allow_headers=["*"],
|
| 18 |
-
)
|
| 19 |
|
| 20 |
-
|
| 21 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
|
| 23 |
# Create the Gradio interface
|
| 24 |
iface = create_interface()
|
|
@@ -26,9 +68,7 @@ iface = create_interface()
|
|
| 26 |
# Mount the Gradio app
|
| 27 |
app = gr.mount_gradio_app(app, iface, path="/")
|
| 28 |
|
| 29 |
-
# Hugging Face Spaces provides the port number in the PORT environment variable
|
| 30 |
-
port = int(os.environ.get("PORT", 7860))
|
| 31 |
-
|
| 32 |
if __name__ == "__main__":
|
| 33 |
import uvicorn
|
|
|
|
| 34 |
uvicorn.run(app, host="0.0.0.0", port=port)
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
from fastapi import FastAPI, HTTPException
|
| 3 |
+
from pydantic import BaseModel, field_validator
|
|
|
|
| 4 |
from frontend import create_interface
|
| 5 |
+
import math
|
| 6 |
+
import re
|
| 7 |
import os
|
| 8 |
|
|
|
|
| 9 |
app = FastAPI()
|
| 10 |
|
| 11 |
+
ALLOWED_FUNCTIONS = {'sin', 'cos', 'tan', 'log', 'ln', 'sqrt', 'abs', 'pow'}
|
| 12 |
+
ALLOWED_CONSTANTS = {'pi', 'e'}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
|
| 14 |
+
class Expression(BaseModel):
|
| 15 |
+
expr: str
|
| 16 |
+
|
| 17 |
+
@field_validator('expr')
|
| 18 |
+
@classmethod
|
| 19 |
+
def validate_expression(cls, v: str) -> str:
|
| 20 |
+
if not v:
|
| 21 |
+
raise ValueError("Expression cannot be empty")
|
| 22 |
+
|
| 23 |
+
# Check for invalid characters
|
| 24 |
+
valid_chars = set('0123456789.+-*/()^ \t\nabcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ')
|
| 25 |
+
invalid_chars = set(v) - valid_chars
|
| 26 |
+
if invalid_chars:
|
| 27 |
+
raise ValueError(f"Invalid characters found: {', '.join(invalid_chars)}")
|
| 28 |
+
|
| 29 |
+
# Check for invalid function names or constants
|
| 30 |
+
tokens = re.findall(r'\b[a-zA-Z_]\w*\b', v)
|
| 31 |
+
invalid_tokens = [token for token in tokens if token not in ALLOWED_FUNCTIONS and token not in ALLOWED_CONSTANTS]
|
| 32 |
+
if invalid_tokens:
|
| 33 |
+
raise ValueError(f"Invalid function or constant names: {', '.join(invalid_tokens)}")
|
| 34 |
+
|
| 35 |
+
return v
|
| 36 |
+
|
| 37 |
+
@app.post("/calculate")
|
| 38 |
+
async def calculate_api(expression: Expression):
|
| 39 |
+
try:
|
| 40 |
+
result = evaluate_expression(expression.expr)
|
| 41 |
+
return {"result": result}
|
| 42 |
+
except ValueError as e:
|
| 43 |
+
raise HTTPException(status_code=400, detail=str(e))
|
| 44 |
+
except Exception as e:
|
| 45 |
+
raise HTTPException(status_code=500, detail=f"An unexpected error occurred: {str(e)}")
|
| 46 |
+
|
| 47 |
+
def evaluate_expression(expr: str) -> float:
|
| 48 |
+
safe_dict = {
|
| 49 |
+
'sin': math.sin, 'cos': math.cos, 'tan': math.tan,
|
| 50 |
+
'log': math.log10, 'ln': math.log, 'sqrt': math.sqrt,
|
| 51 |
+
'pi': math.pi, 'e': math.e,
|
| 52 |
+
'abs': abs, 'pow': pow
|
| 53 |
+
}
|
| 54 |
+
try:
|
| 55 |
+
# Replace '^' with '**' for exponentiation
|
| 56 |
+
expr = expr.replace('^', '**')
|
| 57 |
+
return eval(expr, {"__builtins__": None}, safe_dict)
|
| 58 |
+
except ZeroDivisionError:
|
| 59 |
+
raise ValueError("Division by zero is not allowed")
|
| 60 |
+
except ValueError as e:
|
| 61 |
+
raise ValueError(f"Math domain error: {str(e)}")
|
| 62 |
+
except Exception as e:
|
| 63 |
+
raise ValueError(f"Invalid expression: {str(e)}")
|
| 64 |
|
| 65 |
# Create the Gradio interface
|
| 66 |
iface = create_interface()
|
|
|
|
| 68 |
# Mount the Gradio app
|
| 69 |
app = gr.mount_gradio_app(app, iface, path="/")
|
| 70 |
|
|
|
|
|
|
|
|
|
|
| 71 |
if __name__ == "__main__":
|
| 72 |
import uvicorn
|
| 73 |
+
port = int(os.environ.get("PORT", 7860))
|
| 74 |
uvicorn.run(app, host="0.0.0.0", port=port)
|