Abhlash commited on
Commit
79c86bb
·
verified ·
1 Parent(s): e301ce6

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +32 -0
app.py ADDED
@@ -0,0 +1,32 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from fastapi import FastAPI, HTTPException
2
+ from pydantic import BaseModel
3
+
4
+ app = FastAPI()
5
+
6
+ class CalculationRequest(BaseModel):
7
+ a: float
8
+ b: float
9
+ operation: str
10
+
11
+ @app.post("/calculate")
12
+ def calculate(request: CalculationRequest):
13
+ a = request.a
14
+ b = request.b
15
+ operation = request.operation.lower()
16
+
17
+ if operation == "add":
18
+ result = a + b
19
+ elif operation == "subtract":
20
+ result = a - b
21
+ elif operation == "multiply":
22
+ result = a * b
23
+ elif operation == "divide":
24
+ if b == 0:
25
+ raise HTTPException(status_code=400, detail="Division by zero is not allowed")
26
+ result = a / b
27
+ else:
28
+ raise HTTPException(status_code=400, detail="Invalid operation")
29
+
30
+ return {"result": result}
31
+
32
+ # To run the application, use the command: uvicorn app.main:app --reload