Spaces:
Sleeping
Sleeping
NikhilSetiya
commited on
Commit
·
4cefd33
1
Parent(s):
9005dd0
Add calculator application files
Browse files- backend.py +37 -0
- frontend.py +45 -0
- requirements.txt +5 -0
backend.py
ADDED
@@ -0,0 +1,37 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from fastapi import FastAPI
|
2 |
+
from pydantic import BaseModel
|
3 |
+
from typing import Union
|
4 |
+
|
5 |
+
app = FastAPI()
|
6 |
+
|
7 |
+
class CalculatorInput(BaseModel):
|
8 |
+
num1: float
|
9 |
+
num2: float
|
10 |
+
operation: str
|
11 |
+
|
12 |
+
class CalculatorResponse(BaseModel):
|
13 |
+
result: float
|
14 |
+
operation: str
|
15 |
+
|
16 |
+
@app.post("/calculate", response_model=CalculatorResponse)
|
17 |
+
async def calculate(input_data: CalculatorInput):
|
18 |
+
result = 0.0
|
19 |
+
|
20 |
+
if input_data.operation == "add":
|
21 |
+
result = input_data.num1 + input_data.num2
|
22 |
+
elif input_data.operation == "subtract":
|
23 |
+
result = input_data.num1 - input_data.num2
|
24 |
+
elif input_data.operation == "multiply":
|
25 |
+
result = input_data.num1 * input_data.num2
|
26 |
+
elif input_data.operation == "divide":
|
27 |
+
if input_data.num2 == 0:
|
28 |
+
raise ValueError("Cannot divide by zero")
|
29 |
+
result = input_data.num1 / input_data.num2
|
30 |
+
else:
|
31 |
+
raise ValueError("Invalid operation")
|
32 |
+
|
33 |
+
return CalculatorResponse(result=result, operation=input_data.operation)
|
34 |
+
|
35 |
+
if __name__ == "__main__":
|
36 |
+
import uvicorn
|
37 |
+
uvicorn.run(app, host="0.0.0.0", port=8000)
|
frontend.py
ADDED
@@ -0,0 +1,45 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import requests
|
3 |
+
|
4 |
+
def calculate(num1: float, num2: float, operation: str) -> str:
|
5 |
+
try:
|
6 |
+
response = requests.post(
|
7 |
+
"http://localhost:8000/calculate",
|
8 |
+
json={
|
9 |
+
"num1": num1,
|
10 |
+
"num2": num2,
|
11 |
+
"operation": operation
|
12 |
+
}
|
13 |
+
)
|
14 |
+
response.raise_for_status()
|
15 |
+
result = response.json()
|
16 |
+
return f"Result: {result['result']}"
|
17 |
+
except requests.exceptions.RequestException as e:
|
18 |
+
return f"Error: {str(e)}"
|
19 |
+
|
20 |
+
# Create the Gradio interface
|
21 |
+
with gr.Blocks(title="Calculator") as demo:
|
22 |
+
gr.Markdown("# Calculator")
|
23 |
+
with gr.Row():
|
24 |
+
num1 = gr.Number(label="First Number")
|
25 |
+
num2 = gr.Number(label="Second Number")
|
26 |
+
|
27 |
+
with gr.Row():
|
28 |
+
operation = gr.Radio(
|
29 |
+
choices=["add", "subtract", "multiply", "divide"],
|
30 |
+
label="Operation",
|
31 |
+
value="add"
|
32 |
+
)
|
33 |
+
|
34 |
+
with gr.Row():
|
35 |
+
calculate_btn = gr.Button("Calculate")
|
36 |
+
result = gr.Textbox(label="Result")
|
37 |
+
|
38 |
+
calculate_btn.click(
|
39 |
+
fn=calculate,
|
40 |
+
inputs=[num1, num2, operation],
|
41 |
+
outputs=result
|
42 |
+
)
|
43 |
+
|
44 |
+
if __name__ == "__main__":
|
45 |
+
demo.launch()
|
requirements.txt
ADDED
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
fastapi==0.104.1
|
2 |
+
uvicorn==0.24.0
|
3 |
+
pydantic==2.5.2
|
4 |
+
gradio==4.12.0
|
5 |
+
requests==2.31.0
|