abhishekjoel commited on
Commit
2874038
·
verified ·
1 Parent(s): f2c6421

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +46 -27
app.py CHANGED
@@ -1,42 +1,61 @@
1
- from pydantic import BaseModel
 
2
  from fastapi import FastAPI, HTTPException
 
 
3
 
 
4
  app = FastAPI()
5
 
 
6
  class Numbers(BaseModel):
7
  x: float
8
  y: float
9
 
 
10
  @app.post("/add")
11
- def add_numbers_api(numbers: Numbers):
12
- return {"result": add_numbers(numbers.x, numbers.y)}
13
 
14
  @app.post("/subtract")
15
- def subtract_numbers_api(numbers: Numbers):
16
- return {"result": subtract_numbers(numbers.x, numbers.y)}
17
 
18
  @app.post("/multiply")
19
- def multiply_numbers_api(numbers: Numbers):
20
- return {"result": multiply_numbers(numbers.x, numbers.y)}
21
 
22
  @app.post("/divide")
23
- def divide_numbers_api(numbers: Numbers):
24
- try:
25
- result = divide_numbers(numbers.x, numbers.y)
26
- except ValueError as e:
27
- raise HTTPException(status_code=400, detail=str(e))
28
- return {"result": result}
29
-
30
- def add_numbers(x: float, y: float) -> float:
31
- return x + y
32
-
33
- def subtract_numbers(x: float, y: float) -> float:
34
- return x - y
35
-
36
- def multiply_numbers(x: float, y: float) -> float:
37
- return x * y
38
-
39
- def divide_numbers(x: float, y: float) -> float:
40
- if y == 0:
41
- raise ValueError("Cannot divide by zero")
42
- return x / y
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+
2
+ import gradio as gr
3
  from fastapi import FastAPI, HTTPException
4
+ from pydantic import BaseModel
5
+ import uvicorn
6
 
7
+ # Initialize FastAPI app
8
  app = FastAPI()
9
 
10
+ # Define the Pydantic model for input validation
11
  class Numbers(BaseModel):
12
  x: float
13
  y: float
14
 
15
+ # Define the arithmetic operations
16
  @app.post("/add")
17
+ async def add(numbers: Numbers):
18
+ return {"result": numbers.x + numbers.y}
19
 
20
  @app.post("/subtract")
21
+ async def subtract(numbers: Numbers):
22
+ return {"result": numbers.x - numbers.y}
23
 
24
  @app.post("/multiply")
25
+ async def multiply(numbers: Numbers):
26
+ return {"result": numbers.x * numbers.y"}
27
 
28
  @app.post("/divide")
29
+ async def divide(numbers: Numbers):
30
+ if numbers.y == 0:
31
+ raise HTTPException(status_code=400, detail="Cannot divide by zero")
32
+ return {"result": numbers.x / numbers.y}
33
+
34
+ # Define the Gradio interface function
35
+ def perform_operation(operation: str, x: float, y: float) -> float:
36
+ url = f"http://127.0.0.1:8000/{operation}"
37
+ response = requests.post(url, json={"x": x, "y": y})
38
+ if response.status_code == 200:
39
+ return response.json().get("result")
40
+ else:
41
+ return f"Error: {response.json().get('detail', 'Unknown error')}"
42
+
43
+ # Define the Gradio interface
44
+ demo = gr.Interface(
45
+ fn=perform_operation,
46
+ inputs=[
47
+ gr.Radio(choices=["add", "subtract", "multiply", "divide"], label="Operation"),
48
+ gr.Number(label="X"),
49
+ gr.Number(label="Y")
50
+ ],
51
+ outputs="text",
52
+ title="Arithmetic Operations",
53
+ description="Select an operation and enter two numbers to perform arithmetic operations."
54
+ )
55
+
56
+ # Mount Gradio app onto FastAPI
57
+ gr.mount_gradio_app(app, demo, path="/")
58
+
59
+ # Run the FastAPI app with Uvicorn
60
+ if __name__ == "__main__":
61
+ uvicorn.run(app, host="0.0.0.0", port=7680)