Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,59 +1,81 @@
|
|
| 1 |
-
|
| 2 |
-
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
operation =
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
result =
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import requests
|
| 3 |
+
import os
|
| 4 |
+
from dotenv import load_dotenv
|
| 5 |
+
|
| 6 |
+
# Load environment variables from .env file
|
| 7 |
+
load_dotenv()
|
| 8 |
+
MATH_ENGINE_URL = os.getenv("MATH_ENGINE_URL")
|
| 9 |
+
|
| 10 |
+
# Gradio Interface
|
| 11 |
+
history = []
|
| 12 |
+
current_input = ""
|
| 13 |
+
|
| 14 |
+
def gradio_calculate(a, b, operation):
|
| 15 |
+
url = MATH_ENGINE_URL
|
| 16 |
+
payload = {"a": a, "b": b, "operation": operation}
|
| 17 |
+
headers = {"Content-Type": "application/json"}
|
| 18 |
+
response = requests.post(url, json=payload, headers=headers)
|
| 19 |
+
result = response.json()["result"]
|
| 20 |
+
history.append(f"{a} {operation} {b} = {result}")
|
| 21 |
+
return "\n".join(history)
|
| 22 |
+
|
| 23 |
+
def update_input(digit):
|
| 24 |
+
global current_input
|
| 25 |
+
current_input += str(digit)
|
| 26 |
+
return current_input
|
| 27 |
+
|
| 28 |
+
def clear_input():
|
| 29 |
+
global current_input
|
| 30 |
+
current_input = ""
|
| 31 |
+
return current_input
|
| 32 |
+
|
| 33 |
+
def backspace():
|
| 34 |
+
global current_input
|
| 35 |
+
current_input = current_input[:-1]
|
| 36 |
+
return current_input
|
| 37 |
+
|
| 38 |
+
def test_fastapi(a, b, operation):
|
| 39 |
+
url = MATH_ENGINE_URL
|
| 40 |
+
payload = {"a": a, "b": b, "operation": operation}
|
| 41 |
+
headers = {"Content-Type": "application/json"}
|
| 42 |
+
response = requests.post(url, json=payload, headers=headers)
|
| 43 |
+
return response.json()
|
| 44 |
+
|
| 45 |
+
def create_interface():
|
| 46 |
+
with gr.Blocks() as demo:
|
| 47 |
+
a = gr.Number(label="A", interactive=True)
|
| 48 |
+
b = gr.Number(label="B", interactive=True)
|
| 49 |
+
result = gr.Textbox(label="Result", lines=10, interactive=False)
|
| 50 |
+
current = gr.Textbox(label="Current Input", interactive=False)
|
| 51 |
+
|
| 52 |
+
with gr.Row():
|
| 53 |
+
for i in range(10):
|
| 54 |
+
gr.Button(str(i)).click(lambda x=i: update_input(x), inputs=[], outputs=current)
|
| 55 |
+
gr.Button("Back").click(backspace, inputs=[], outputs=current)
|
| 56 |
+
gr.Button("Clear").click(clear_input, inputs=[], outputs=current)
|
| 57 |
+
|
| 58 |
+
with gr.Row():
|
| 59 |
+
gr.Button("Set A").click(lambda x: float(current_input), inputs=[], outputs=a)
|
| 60 |
+
gr.Button("Set B").click(lambda x: float(current_input), inputs=[], outputs=b)
|
| 61 |
+
|
| 62 |
+
with gr.Row():
|
| 63 |
+
gr.Button("Add").click(lambda a, b: gradio_calculate(a, b, "add"), inputs=[a, b], outputs=result)
|
| 64 |
+
gr.Button("Subtract").click(lambda a, b: gradio_calculate(a, b, "subtract"), inputs=[a, b], outputs=result)
|
| 65 |
+
gr.Button("Multiply").click(lambda a, b: gradio_calculate(a, b, "multiply"), inputs=[a, b], outputs=result)
|
| 66 |
+
gr.Button("Divide").click(lambda a, b: gradio_calculate(a, b, "divide"), inputs=[a, b], outputs=result)
|
| 67 |
+
|
| 68 |
+
with gr.Row():
|
| 69 |
+
gr.Button("Test FastAPI Add").click(lambda a, b: test_fastapi(a, b, "add"), inputs=[a, b], outputs=result)
|
| 70 |
+
gr.Button("Test FastAPI Subtract").click(lambda a, b: test_fastapi(a, b, "subtract"), inputs=[a, b], outputs=result)
|
| 71 |
+
gr.Button("Test FastAPI Multiply").click(lambda a, b: test_fastapi(a, b, "multiply"), inputs=[a, b], outputs=result)
|
| 72 |
+
gr.Button("Test FastAPI Divide").click(lambda a, b: test_fastapi(a, b, "divide"), inputs=[a, b], outputs=result)
|
| 73 |
+
|
| 74 |
+
return demo
|
| 75 |
+
|
| 76 |
+
def run_gradio():
|
| 77 |
+
demo = create_interface()
|
| 78 |
+
demo.launch()
|
| 79 |
+
|
| 80 |
+
if __name__ == "__main__":
|
| 81 |
+
run_gradio()
|