abhishekjoel commited on
Commit
07db2da
·
verified ·
1 Parent(s): 0ab0fa3

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +10 -8
app.py CHANGED
@@ -1,4 +1,5 @@
1
- import gradio as gr
 
2
  from fastapi import FastAPI, HTTPException
3
  from pydantic import BaseModel
4
  import uvicorn
@@ -32,14 +33,16 @@ async def divide(numbers: Numbers):
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
  # Construct the URL with the correct scheme and host
37
  url = f"http://127.0.0.1:8000/{operation}"
38
- response = requests.post(url, json={"x": x, "y": y})
39
- if response.status_code == 200:
40
- return response.json().get("result")
41
- else:
42
- return f"Error: {response.json().get('detail', 'Unknown error')}"
 
 
43
 
44
  # Define the Gradio interface
45
  demo = gr.Interface(
@@ -60,4 +63,3 @@ gr.mount_gradio_app(app, demo, path="/")
60
  # Run the FastAPI app with Uvicorn
61
  if __name__ == "__main__":
62
  uvicorn.run(app, host="0.0.0.0", port=8000)
63
-
 
1
+
2
+ import gradio as gr
3
  from fastapi import FastAPI, HTTPException
4
  from pydantic import BaseModel
5
  import uvicorn
 
33
  return {"result": numbers.x / numbers.y}
34
 
35
  # Define the Gradio interface function
36
+ def perform_operation(operation: str, x: float, y: float) -> str:
37
  # Construct the URL with the correct scheme and host
38
  url = f"http://127.0.0.1:8000/{operation}"
39
+ try:
40
+ response = requests.post(url, json={"x": x, "y": y})
41
+ response.raise_for_status() # Raise an error for bad responses
42
+ result = response.json().get("result")
43
+ return str(result) if result is not None else "Error: Invalid response"
44
+ except requests.exceptions.RequestException as e:
45
+ return f"Error: {e}"
46
 
47
  # Define the Gradio interface
48
  demo = gr.Interface(
 
63
  # Run the FastAPI app with Uvicorn
64
  if __name__ == "__main__":
65
  uvicorn.run(app, host="0.0.0.0", port=8000)