Spaces:
Sleeping
Sleeping
Commit
·
f5d1132
1
Parent(s):
f501fe1
added a third file and implemented threading
Browse files- app.py +11 -31
- gradio_app.py +29 -0
app.py
CHANGED
@@ -1,34 +1,14 @@
|
|
1 |
-
import
|
2 |
-
import
|
|
|
|
|
3 |
|
4 |
-
#
|
5 |
-
def
|
6 |
-
|
7 |
-
response = requests.post("http://127.0.0.1:8000/calculate",
|
8 |
-
json={"operation": operation, "num1": num1, "num2": num2})
|
9 |
-
|
10 |
-
if response.status_code == 200:
|
11 |
-
response_data = response.json()
|
12 |
-
return response_data['result']
|
13 |
-
elif (response.json())['detail'] == "Division by zero is not allowed":
|
14 |
-
return "Error: Division by zero is not allowed"
|
15 |
-
else:
|
16 |
-
return "Error: Unable to connect to the FastAPI backend."
|
17 |
|
18 |
-
#
|
19 |
-
|
20 |
-
|
21 |
-
inputs=[gr.Number(label="Number 1"), gr.Number(label="Number 2"),
|
22 |
-
gr.Dropdown(choices=["+", "-", "*", "/"], label="Operation")],
|
23 |
-
outputs=gr.Textbox(label="Result"),
|
24 |
-
title="Simple Calculator",
|
25 |
-
allow_flagging="never"
|
26 |
-
)
|
27 |
|
28 |
-
|
29 |
-
demo.launch(share=True)
|
30 |
-
|
31 |
-
if __name__ == "__main__":
|
32 |
-
import uvicorn
|
33 |
-
from calc_fast_api import app
|
34 |
-
uvicorn.run("app",host="127.0.0.1",port=8000)
|
|
|
1 |
+
import uvicorn
|
2 |
+
import calc_fast_api
|
3 |
+
import gradio_app
|
4 |
+
import threading
|
5 |
|
6 |
+
# Start FastAPI in a separate thread
|
7 |
+
def start_fastapi():
|
8 |
+
uvicorn.run("fastapi_app:app", host="0.0.0.0", port=8000)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
9 |
|
10 |
+
# Start FastAPI thread
|
11 |
+
fastapi_thread = threading.Thread(target= start_fastapi)
|
12 |
+
fastapi_thread.start()
|
|
|
|
|
|
|
|
|
|
|
|
|
13 |
|
14 |
+
gradio_app.launch_gradio()
|
|
|
|
|
|
|
|
|
|
|
|
gradio_app.py
ADDED
@@ -0,0 +1,29 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import requests
|
3 |
+
|
4 |
+
# Define the predict function that communicates with the FastAPI backend
|
5 |
+
def predict(num1, num2, operation):
|
6 |
+
# API request to FastAPI backend
|
7 |
+
response = requests.post("http://127.0.0.1:8000/calculate",
|
8 |
+
json={"operation": operation, "num1": num1, "num2": num2})
|
9 |
+
|
10 |
+
if response.status_code == 200:
|
11 |
+
response_data = response.json()
|
12 |
+
return response_data['result']
|
13 |
+
elif (response.json())['detail'] == "Division by zero is not allowed":
|
14 |
+
return "Error: Division by zero is not allowed"
|
15 |
+
else:
|
16 |
+
return "Error: Unable to connect to the FastAPI backend."
|
17 |
+
|
18 |
+
# Gradio Interface
|
19 |
+
demo = gr.Interface(
|
20 |
+
fn=predict,
|
21 |
+
inputs=[gr.Number(label="Number 1"), gr.Number(label="Number 2"),
|
22 |
+
gr.Dropdown(choices=["+", "-", "*", "/"], label="Operation")],
|
23 |
+
outputs=gr.Textbox(label="Result"),
|
24 |
+
title="Simple Calculator",
|
25 |
+
allow_flagging="never"
|
26 |
+
)
|
27 |
+
|
28 |
+
def launch_gradio():
|
29 |
+
demo.launch(server_name="0.0.0.0", server_port=7860, share=True)
|