abhishekjoel commited on
Commit
198c437
·
verified ·
1 Parent(s): b3c958d

Update app.py

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