Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,47 +1,81 @@
|
|
| 1 |
-
|
|
|
|
|
|
|
| 2 |
from fastapi import FastAPI, HTTPException
|
|
|
|
|
|
|
|
|
|
| 3 |
|
|
|
|
| 4 |
app = FastAPI()
|
| 5 |
|
| 6 |
-
# Define the data model for incoming JSON data
|
| 7 |
class Numbers(BaseModel):
|
| 8 |
x: float
|
| 9 |
y: float
|
| 10 |
|
| 11 |
-
# Endpoint for addition
|
| 12 |
@app.post("/add")
|
| 13 |
def add_numbers_api(numbers: Numbers):
|
| 14 |
return {"result": add_numbers(numbers.x, numbers.y)}
|
| 15 |
|
| 16 |
-
# Endpoint for subtraction
|
| 17 |
@app.post("/subtract")
|
| 18 |
def subtract_numbers_api(numbers: Numbers):
|
| 19 |
return {"result": subtract_numbers(numbers.x, numbers.y)}
|
| 20 |
|
| 21 |
-
# Endpoint for multiplication
|
| 22 |
@app.post("/multiply")
|
| 23 |
def multiply_numbers_api(numbers: Numbers):
|
| 24 |
return {"result": multiply_numbers(numbers.x, numbers.y)}
|
| 25 |
|
| 26 |
-
# Endpoint for division
|
| 27 |
@app.post("/divide")
|
| 28 |
def divide_numbers_api(numbers: Numbers):
|
|
|
|
|
|
|
| 29 |
return {"result": divide_numbers(numbers.x, numbers.y)}
|
| 30 |
|
| 31 |
-
# Function to add two numbers
|
| 32 |
def add_numbers(x: float, y: float) -> float:
|
| 33 |
return x + y
|
| 34 |
|
| 35 |
-
# Function to subtract two numbers
|
| 36 |
def subtract_numbers(x: float, y: float) -> float:
|
| 37 |
return x - y
|
| 38 |
|
| 39 |
-
# Function to multiply two numbers
|
| 40 |
def multiply_numbers(x: float, y: float) -> float:
|
| 41 |
return x * y
|
| 42 |
|
| 43 |
-
# Function to divide two numbers
|
| 44 |
def divide_numbers(x: float, y: float) -> float:
|
| 45 |
-
if y == 0:
|
| 46 |
-
raise HTTPException(status_code=400, detail="Cannot divide by zero")
|
| 47 |
return x / y
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import threading
|
| 2 |
+
import time
|
| 3 |
+
import requests
|
| 4 |
from fastapi import FastAPI, HTTPException
|
| 5 |
+
from pydantic import BaseModel
|
| 6 |
+
import gradio as gr
|
| 7 |
+
import uvicorn
|
| 8 |
|
| 9 |
+
# FastAPI app setup
|
| 10 |
app = FastAPI()
|
| 11 |
|
|
|
|
| 12 |
class Numbers(BaseModel):
|
| 13 |
x: float
|
| 14 |
y: float
|
| 15 |
|
|
|
|
| 16 |
@app.post("/add")
|
| 17 |
def add_numbers_api(numbers: Numbers):
|
| 18 |
return {"result": add_numbers(numbers.x, numbers.y)}
|
| 19 |
|
|
|
|
| 20 |
@app.post("/subtract")
|
| 21 |
def subtract_numbers_api(numbers: Numbers):
|
| 22 |
return {"result": subtract_numbers(numbers.x, numbers.y)}
|
| 23 |
|
|
|
|
| 24 |
@app.post("/multiply")
|
| 25 |
def multiply_numbers_api(numbers: Numbers):
|
| 26 |
return {"result": multiply_numbers(numbers.x, numbers.y)}
|
| 27 |
|
|
|
|
| 28 |
@app.post("/divide")
|
| 29 |
def divide_numbers_api(numbers: Numbers):
|
| 30 |
+
if numbers.y == 0:
|
| 31 |
+
raise HTTPException(status_code=400, detail="Cannot divide by zero")
|
| 32 |
return {"result": divide_numbers(numbers.x, numbers.y)}
|
| 33 |
|
|
|
|
| 34 |
def add_numbers(x: float, y: float) -> float:
|
| 35 |
return x + y
|
| 36 |
|
|
|
|
| 37 |
def subtract_numbers(x: float, y: float) -> float:
|
| 38 |
return x - y
|
| 39 |
|
|
|
|
| 40 |
def multiply_numbers(x: float, y: float) -> float:
|
| 41 |
return x * y
|
| 42 |
|
|
|
|
| 43 |
def divide_numbers(x: float, y: float) -> float:
|
|
|
|
|
|
|
| 44 |
return x / y
|
| 45 |
+
|
| 46 |
+
# Function to run the FastAPI app
|
| 47 |
+
def run_fastapi():
|
| 48 |
+
uvicorn.run(app, host="127.0.0.1", port=8000)
|
| 49 |
+
|
| 50 |
+
# Start the FastAPI server in a separate thread
|
| 51 |
+
thread = threading.Thread(target=run_fastapi, daemon=True)
|
| 52 |
+
thread.start()
|
| 53 |
+
|
| 54 |
+
# Gradio frontend setup
|
| 55 |
+
BASE_URL = "http://127.0.0.1:8000"
|
| 56 |
+
|
| 57 |
+
def perform_operation(operation: str, x: float, y: float) -> str:
|
| 58 |
+
try:
|
| 59 |
+
url = f"{BASE_URL}/{operation}"
|
| 60 |
+
response = requests.post(url, json={"x": x, "y": y})
|
| 61 |
+
response.raise_for_status()
|
| 62 |
+
result = response.json().get("result")
|
| 63 |
+
return str(result)
|
| 64 |
+
except requests.exceptions.HTTPError as err:
|
| 65 |
+
return f"Error: {err.response.json().get('detail')}"
|
| 66 |
+
|
| 67 |
+
# Gradio interface
|
| 68 |
+
iface = gr.Interface(
|
| 69 |
+
fn=perform_operation,
|
| 70 |
+
inputs=[
|
| 71 |
+
gr.Radio(choices=["add", "subtract", "multiply", "divide"], label="Operation"),
|
| 72 |
+
gr.Number(label="X"),
|
| 73 |
+
gr.Number(label="Y")
|
| 74 |
+
],
|
| 75 |
+
outputs="text",
|
| 76 |
+
live=False
|
| 77 |
+
)
|
| 78 |
+
|
| 79 |
+
# Wait a moment to ensure the server is up before launching Gradio
|
| 80 |
+
time.sleep(1)
|
| 81 |
+
iface.launch()
|