Spaces:
Sleeping
Sleeping
Commit ·
6be1e82
1
Parent(s): f7bdd9d
added files
Browse files- calc_fast_api.py +40 -0
- gradio_calc.py +29 -0
calc_fast_api.py
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from fastapi import FastAPI, HTTPException
|
| 2 |
+
from pydantic import BaseModel
|
| 3 |
+
from enum import Enum
|
| 4 |
+
|
| 5 |
+
# Initialize the FastAPI app
|
| 6 |
+
app = FastAPI()
|
| 7 |
+
|
| 8 |
+
# Define allowed operations using Enum for better validation
|
| 9 |
+
class Operation(str, Enum):
|
| 10 |
+
add = "+"
|
| 11 |
+
subtract = "-"
|
| 12 |
+
multiply = "*"
|
| 13 |
+
divide = "/"
|
| 14 |
+
|
| 15 |
+
# Define the request model for the calculator
|
| 16 |
+
class CalculationRequest(BaseModel):
|
| 17 |
+
operation: Operation # Operation can be '+', '-', '*', '/'
|
| 18 |
+
num1: float
|
| 19 |
+
num2: float
|
| 20 |
+
|
| 21 |
+
# Calculator logic function
|
| 22 |
+
def calculate(operation: str, num1: float, num2: float):
|
| 23 |
+
if operation == '+':
|
| 24 |
+
return num1 + num2
|
| 25 |
+
elif operation == '-':
|
| 26 |
+
return num1 - num2
|
| 27 |
+
elif operation == '*':
|
| 28 |
+
return num1 * num2
|
| 29 |
+
elif operation == '/':
|
| 30 |
+
if num2 == 0:
|
| 31 |
+
raise HTTPException(status_code=400, detail="Division by zero is not allowed")
|
| 32 |
+
return num1 / num2
|
| 33 |
+
else:
|
| 34 |
+
raise HTTPException(status_code=400, detail="Invalid operation")
|
| 35 |
+
|
| 36 |
+
# Define the POST endpoint for calculations
|
| 37 |
+
@app.post("/calculate")
|
| 38 |
+
def perform_calculation(request: CalculationRequest):
|
| 39 |
+
result = calculate(request.operation, request.num1, request.num2)
|
| 40 |
+
return {"result": result}
|
gradio_calc.py
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import requests
|
| 3 |
+
|
| 4 |
+
# Define the predict function that communicates with the FastAPI backend
|
| 5 |
+
def predict(num1, num2, operation):
|
| 6 |
+
# API request to FastAPI backend
|
| 7 |
+
response = requests.post("http://127.0.0.1:8000/calculate",
|
| 8 |
+
json={"operation": operation, "num1": num1, "num2": num2})
|
| 9 |
+
|
| 10 |
+
if response.status_code == 200:
|
| 11 |
+
response_data = response.json()
|
| 12 |
+
return response_data['result']
|
| 13 |
+
elif (response.json())['detail'] == "Division by zero is not allowed":
|
| 14 |
+
return "Error: Division by zero is not allowed"
|
| 15 |
+
else:
|
| 16 |
+
return "Error: Unable to connect to the FastAPI backend."
|
| 17 |
+
|
| 18 |
+
# Gradio Interface
|
| 19 |
+
demo = gr.Interface(
|
| 20 |
+
fn=predict,
|
| 21 |
+
inputs=[gr.Number(label="Number 1"), gr.Number(label="Number 2"),
|
| 22 |
+
gr.Dropdown(choices=["+", "-", "*", "/"], label="Operation")],
|
| 23 |
+
outputs=gr.Textbox(label="Result"),
|
| 24 |
+
title="Simple Calculator",
|
| 25 |
+
allow_flagging="never"
|
| 26 |
+
)
|
| 27 |
+
|
| 28 |
+
# Launch the Gradio interface
|
| 29 |
+
demo.launch(share=True)
|