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

Create main.py

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