fast_chatbot / app.py
harshSethi's picture
made changes to app.py
a00bb17
raw
history blame
1.16 kB
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"
)
# Launch the Gradio interface
demo.launch(share=True)
if __name__ == "__main__":
import uvicorn
import calc_fast_api
uvicorn.run("calc_fast_api:app",host="127.0.0.1",port="8000")