Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,64 +1,39 @@
|
|
| 1 |
-
import gradio as gr
|
| 2 |
-
from fastapi import FastAPI, HTTPException
|
| 3 |
-
from pydantic import BaseModel
|
| 4 |
-
import uvicorn
|
| 5 |
-
import requests
|
| 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 |
-
|
| 18 |
-
return {"result": numbers.x
|
| 19 |
|
| 20 |
@app.post("/subtract")
|
| 21 |
-
|
| 22 |
-
return {"result": numbers.x
|
| 23 |
|
| 24 |
@app.post("/multiply")
|
| 25 |
-
|
| 26 |
-
return {"result": numbers.x
|
| 27 |
|
| 28 |
@app.post("/divide")
|
| 29 |
-
|
| 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) -> str:
|
| 36 |
-
# Construct the URL with the correct scheme and host
|
| 37 |
-
url = f"http://127.0.0.1:8000/{operation}"
|
| 38 |
try:
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
|
|
|
| 45 |
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
fn=perform_operation,
|
| 49 |
-
inputs=[
|
| 50 |
-
gr.Radio(choices=["add", "subtract", "multiply", "divide"], label="Operation"),
|
| 51 |
-
gr.Number(label="X"),
|
| 52 |
-
gr.Number(label="Y")
|
| 53 |
-
],
|
| 54 |
-
outputs="text",
|
| 55 |
-
title="Arithmetic Operations",
|
| 56 |
-
description="Select an operation and enter two numbers to perform arithmetic operations."
|
| 57 |
-
)
|
| 58 |
|
| 59 |
-
|
| 60 |
-
|
| 61 |
|
| 62 |
-
|
| 63 |
-
if
|
| 64 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
app = FastAPI()
|
| 2 |
|
|
|
|
| 3 |
class Numbers(BaseModel):
|
| 4 |
x: float
|
| 5 |
y: float
|
| 6 |
|
|
|
|
| 7 |
@app.post("/add")
|
| 8 |
+
def add_numbers_api(numbers: Numbers):
|
| 9 |
+
return {"result": add_numbers(numbers.x, numbers.y)}
|
| 10 |
|
| 11 |
@app.post("/subtract")
|
| 12 |
+
def subtract_numbers_api(numbers: Numbers):
|
| 13 |
+
return {"result": subtract_numbers(numbers.x, numbers.y)}
|
| 14 |
|
| 15 |
@app.post("/multiply")
|
| 16 |
+
def multiply_numbers_api(numbers: Numbers):
|
| 17 |
+
return {"result": multiply_numbers(numbers.x, numbers.y)}
|
| 18 |
|
| 19 |
@app.post("/divide")
|
| 20 |
+
def divide_numbers_api(numbers: Numbers):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
try:
|
| 22 |
+
result = divide_numbers(numbers.x, numbers.y)
|
| 23 |
+
except ValueError as e:
|
| 24 |
+
raise HTTPException(status_code=400, detail=str(e))
|
| 25 |
+
return {"result": result}
|
| 26 |
+
|
| 27 |
+
def add_numbers(x: float, y: float) -> float:
|
| 28 |
+
return x + y
|
| 29 |
|
| 30 |
+
def subtract_numbers(x: float, y: float) -> float:
|
| 31 |
+
return x - y
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 32 |
|
| 33 |
+
def multiply_numbers(x: float, y: float) -> float:
|
| 34 |
+
return x * y
|
| 35 |
|
| 36 |
+
def divide_numbers(x: float, y: float) -> float:
|
| 37 |
+
if y == 0:
|
| 38 |
+
raise ValueError("Cannot divide by zero")
|
| 39 |
+
return x / y
|