Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,42 +1,61 @@
|
|
| 1 |
-
|
|
|
|
| 2 |
from fastapi import FastAPI, HTTPException
|
|
|
|
|
|
|
| 3 |
|
|
|
|
| 4 |
app = FastAPI()
|
| 5 |
|
|
|
|
| 6 |
class Numbers(BaseModel):
|
| 7 |
x: float
|
| 8 |
y: float
|
| 9 |
|
|
|
|
| 10 |
@app.post("/add")
|
| 11 |
-
def
|
| 12 |
-
return {"result":
|
| 13 |
|
| 14 |
@app.post("/subtract")
|
| 15 |
-
def
|
| 16 |
-
return {"result":
|
| 17 |
|
| 18 |
@app.post("/multiply")
|
| 19 |
-
def
|
| 20 |
-
return {"result":
|
| 21 |
|
| 22 |
@app.post("/divide")
|
| 23 |
-
def
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
|
| 2 |
+
import gradio as gr
|
| 3 |
from fastapi import FastAPI, HTTPException
|
| 4 |
+
from pydantic import BaseModel
|
| 5 |
+
import uvicorn
|
| 6 |
|
| 7 |
+
# Initialize FastAPI app
|
| 8 |
app = FastAPI()
|
| 9 |
|
| 10 |
+
# Define the Pydantic model for input validation
|
| 11 |
class Numbers(BaseModel):
|
| 12 |
x: float
|
| 13 |
y: float
|
| 14 |
|
| 15 |
+
# Define the arithmetic operations
|
| 16 |
@app.post("/add")
|
| 17 |
+
async def add(numbers: Numbers):
|
| 18 |
+
return {"result": numbers.x + numbers.y}
|
| 19 |
|
| 20 |
@app.post("/subtract")
|
| 21 |
+
async def subtract(numbers: Numbers):
|
| 22 |
+
return {"result": numbers.x - numbers.y}
|
| 23 |
|
| 24 |
@app.post("/multiply")
|
| 25 |
+
async def multiply(numbers: Numbers):
|
| 26 |
+
return {"result": numbers.x * numbers.y"}
|
| 27 |
|
| 28 |
@app.post("/divide")
|
| 29 |
+
async def divide(numbers: Numbers):
|
| 30 |
+
if numbers.y == 0:
|
| 31 |
+
raise HTTPException(status_code=400, detail="Cannot divide by zero")
|
| 32 |
+
return {"result": numbers.x / numbers.y}
|
| 33 |
+
|
| 34 |
+
# Define the Gradio interface function
|
| 35 |
+
def perform_operation(operation: str, x: float, y: float) -> float:
|
| 36 |
+
url = f"http://127.0.0.1:8000/{operation}"
|
| 37 |
+
response = requests.post(url, json={"x": x, "y": y})
|
| 38 |
+
if response.status_code == 200:
|
| 39 |
+
return response.json().get("result")
|
| 40 |
+
else:
|
| 41 |
+
return f"Error: {response.json().get('detail', 'Unknown error')}"
|
| 42 |
+
|
| 43 |
+
# Define the Gradio interface
|
| 44 |
+
demo = gr.Interface(
|
| 45 |
+
fn=perform_operation,
|
| 46 |
+
inputs=[
|
| 47 |
+
gr.Radio(choices=["add", "subtract", "multiply", "divide"], label="Operation"),
|
| 48 |
+
gr.Number(label="X"),
|
| 49 |
+
gr.Number(label="Y")
|
| 50 |
+
],
|
| 51 |
+
outputs="text",
|
| 52 |
+
title="Arithmetic Operations",
|
| 53 |
+
description="Select an operation and enter two numbers to perform arithmetic operations."
|
| 54 |
+
)
|
| 55 |
+
|
| 56 |
+
# Mount Gradio app onto FastAPI
|
| 57 |
+
gr.mount_gradio_app(app, demo, path="/")
|
| 58 |
+
|
| 59 |
+
# Run the FastAPI app with Uvicorn
|
| 60 |
+
if __name__ == "__main__":
|
| 61 |
+
uvicorn.run(app, host="0.0.0.0", port=7680)
|