Spaces:
Runtime error
Runtime error
| import gradio as gr | |
| import requests | |
| from MathEngine import calculate, CalculationRequest, app as fastapi_app | |
| import uvicorn | |
| from multiprocessing import Process | |
| # Gradio Interface | |
| history = [] | |
| current_input = "" | |
| def gradio_calculate(a, b, operation): | |
| request = CalculationRequest(a=a, b=b, operation=operation) | |
| response = calculate(request) | |
| result = response["result"] | |
| history.append(f"{a} {operation} {b} = {result}") | |
| return "\n".join(history) | |
| def update_input(digit): | |
| global current_input | |
| current_input += str(digit) | |
| return current_input | |
| def clear_input(): | |
| global current_input | |
| current_input = "" | |
| return current_input | |
| def backspace(): | |
| global current_input | |
| current_input = current_input[:-1] | |
| return current_input | |
| def test_fastapi(a, b, operation): | |
| url = "http://0.0.0.0:8000/calculate" | |
| payload = {"a": a, "b": b, "operation": operation} | |
| headers = {"Content-Type": "application/json"} | |
| response = requests.post(url, json=payload, headers=headers) | |
| return response.json() | |
| def create_interface(): | |
| with gr.Blocks() as demo: | |
| a = gr.Number(label="A", interactive=True) | |
| b = gr.Number(label="B", interactive=True) | |
| result = gr.Textbox(label="Result", lines=10, interactive=False) | |
| current = gr.Textbox(label="Current Input", interactive=False) | |
| with gr.Row(): | |
| for i in range(10): | |
| gr.Button(str(i)).click(lambda x=i: update_input(x), inputs=[], outputs=current) | |
| gr.Button("Back").click(backspace, inputs=[], outputs=current) | |
| gr.Button("Clear").click(clear_input, inputs=[], outputs=current) | |
| with gr.Row(): | |
| gr.Button("Set A").click(lambda x: float(current_input), inputs=[], outputs=a) | |
| gr.Button("Set B").click(lambda x: float(current_input), inputs=[], outputs=b) | |
| with gr.Row(): | |
| gr.Button("Add").click(lambda a, b: gradio_calculate(a, b, "add"), inputs=[a, b], outputs=result) | |
| gr.Button("Subtract").click(lambda a, b: gradio_calculate(a, b, "subtract"), inputs=[a, b], outputs=result) | |
| gr.Button("Multiply").click(lambda a, b: gradio_calculate(a, b, "multiply"), inputs=[a, b], outputs=result) | |
| gr.Button("Divide").click(lambda a, b: gradio_calculate(a, b, "divide"), inputs=[a, b], outputs=result) | |
| with gr.Row(): | |
| gr.Button("Test FastAPI Add").click(lambda a, b: test_fastapi(a, b, "add"), inputs=[a, b], outputs=result) | |
| gr.Button("Test FastAPI Subtract").click(lambda a, b: test_fastapi(a, b, "subtract"), inputs=[a, b], outputs=result) | |
| gr.Button("Test FastAPI Multiply").click(lambda a, b: test_fastapi(a, b, "multiply"), inputs=[a, b], outputs=result) | |
| gr.Button("Test FastAPI Divide").click(lambda a, b: test_fastapi(a, b, "divide"), inputs=[a, b], outputs=result) | |
| return demo | |
| def run_gradio(): | |
| demo = create_interface() | |
| demo.launch(share=True) # Ensure share=True to make the Gradio interface publicly accessible | |
| def run_fastapi(): | |
| uvicorn.run(fastapi_app, host="0.0.0.0", port=8000) | |
| if __name__ == "__main__": | |
| p1 = Process(target=run_gradio) | |
| p2 = Process(target=run_fastapi) | |
| p1.start() | |
| p2.start() | |
| p1.join() | |
| p2.join() |