elsamueldev commited on
Commit
2e02765
1 Parent(s): 24c0d9a

separated model, better type for `operation`

Browse files
Files changed (2) hide show
  1. app.py +8 -11
  2. models.py +16 -0
app.py CHANGED
@@ -1,31 +1,28 @@
1
  # main.py
2
  import gradio as gr
3
  from fastapi import FastAPI, HTTPException
4
- from pydantic import BaseModel
 
5
 
6
  # FastAPI setup
7
  app = FastAPI()
8
 
9
- class CalculationRequest(BaseModel):
10
- operation: str
11
- x: float
12
- y: float
13
 
14
  @app.post("/calculate")
15
  def calculate(request: CalculationRequest):
 
 
16
  try:
17
- if request.operation == "add":
18
  answer = request.x + request.y
19
- elif request.operation == "subtract":
20
  answer = request.x - request.y
21
- elif request.operation == "multiply":
22
  answer = request.x * request.y
23
- elif request.operation == "divide":
24
  if request.y == 0:
25
  raise HTTPException(status_code=400, detail="Division by zero is not allowed")
26
  answer = request.x / request.y
27
- else:
28
- raise HTTPException(status_code=400, detail="Invalid operation")
29
 
30
  return {"result": answer}
31
  except ValueError:
 
1
  # main.py
2
  import gradio as gr
3
  from fastapi import FastAPI, HTTPException
4
+
5
+ from models import CalculationRequest
6
 
7
  # FastAPI setup
8
  app = FastAPI()
9
 
 
 
 
 
10
 
11
  @app.post("/calculate")
12
  def calculate(request: CalculationRequest):
13
+ operation = request.operation.value
14
+
15
  try:
16
+ if operation == "add":
17
  answer = request.x + request.y
18
+ elif operation == "subtract":
19
  answer = request.x - request.y
20
+ elif operation == "multiply":
21
  answer = request.x * request.y
22
+ else: # only option left is divide
23
  if request.y == 0:
24
  raise HTTPException(status_code=400, detail="Division by zero is not allowed")
25
  answer = request.x / request.y
 
 
26
 
27
  return {"result": answer}
28
  except ValueError:
models.py ADDED
@@ -0,0 +1,16 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from enum import Enum
2
+
3
+ from pydantic import BaseModel
4
+
5
+
6
+ class Operation(Enum):
7
+ ADD = "add"
8
+ SUBTRACT = "subtract"
9
+ MULTIPLY = "multiply"
10
+ DIVIDE = "divide"
11
+
12
+
13
+ class CalculationRequest(BaseModel):
14
+ operation: Operation
15
+ x: float
16
+ y: float