Spaces:
Sleeping
Sleeping
| import gradio as gr | |
| import requests | |
| # Define the predict function that communicates with the FastAPI backend | |
| def predict(num1, num2, operation): | |
| # API request to FastAPI backend | |
| response = requests.post("http://127.0.0.1:8000/calculate", | |
| json={"operation": operation, "num1": num1, "num2": num2}) | |
| if response.status_code == 200: | |
| response_data = response.json() | |
| return response_data['result'] | |
| elif (response.json())['detail'] == "Division by zero is not allowed": | |
| return "Error: Division by zero is not allowed" | |
| else: | |
| return "Error: Unable to connect to the FastAPI backend." | |
| # Gradio Interface | |
| demo = gr.Interface( | |
| fn=predict, | |
| inputs=[gr.Number(label="Number 1"), gr.Number(label="Number 2"), | |
| gr.Dropdown(choices=["+", "-", "*", "/"], label="Operation")], | |
| outputs=gr.Textbox(label="Result"), | |
| title="Simple Calculator", | |
| allow_flagging="never" | |
| ) | |
| def launch_gradio(): | |
| demo.launch(server_name="0.0.0.0", server_port=7860, share=True) | |