| import gradio as gr |
| from fastapi import FastAPI, HTTPException |
| from pydantic import BaseModel |
|
|
| app = FastAPI() |
|
|
| def add(a, b): |
| return a + b |
|
|
| def subtract(a, b): |
| return a - b |
|
|
| def multiply(a, b): |
| return a * b |
|
|
| def divide(a, b): |
| if b == 0: |
| return "Error: Division by zero" |
| return a / b |
|
|
| def calculator(input_text): |
| try: |
| |
| input_text = input_text.replace(" ", "") |
| for op in ['+', '-', '*', '/']: |
| input_text = input_text.replace(op, f" {op} ") |
| parts = input_text.split() |
| if len(parts) != 3: |
| return "Error: Invalid input" |
| a = float(parts[0]) |
| op = parts[1] |
| b = float(parts[2]) |
| if op == '+': |
| return str(add(a, b)) |
| elif op == '-': |
| return str(subtract(a, b)) |
| elif op == '*': |
| return str(multiply(a, b)) |
| elif op == '/': |
| return str(divide(a, b)) |
| else: |
| return "Error: Invalid operator" |
| except ValueError: |
| return "Error: Invalid number" |
| except Exception as e: |
| return f"Error: {str(e)}" |
|
|
| def append_to_input(current, value): |
| if current is None: |
| current = "" |
| current = str(current) |
| value = str(value) |
| if value in ['+', '-', '*', '/']: |
| return f"{current} {value} " |
| return current + value |
|
|
| def clear_input(): |
| return "" |
|
|
| def backspace(current): |
| if current is None or current == "": |
| return "" |
| return current[:-1] |
|
|
| class CalculationInput(BaseModel): |
| a: float |
| b: float |
|
|
| @app.post("/add") |
| async def api_add(input: CalculationInput): |
| return {"result": add(input.a, input.b)} |
|
|
| @app.post("/subtract") |
| async def api_subtract(input: CalculationInput): |
| return {"result": subtract(input.a, input.b)} |
|
|
| @app.post("/multiply") |
| async def api_multiply(input: CalculationInput): |
| return {"result": multiply(input.a, input.b)} |
|
|
| @app.post("/divide") |
| async def api_divide(input: CalculationInput): |
| if input.b == 0: |
| raise HTTPException(status_code=400, detail="Division by zero") |
| return {"result": divide(input.a, input.b)} |
|
|
| @app.post("/calculate") |
| async def api_calculate(input: CalculationInput, operation: str): |
| if operation == "+": |
| return {"result": add(input.a, input.b)} |
| elif operation == "-": |
| return {"result": subtract(input.a, input.b)} |
| elif operation == "*": |
| return {"result": multiply(input.a, input.b)} |
| elif operation == "/": |
| if input.b == 0: |
| raise HTTPException(status_code=400, detail="Division by zero") |
| return {"result": divide(input.a, input.b)} |
| else: |
| raise HTTPException(status_code=400, detail="Invalid operation") |
|
|
| @app.get("/test") |
| async def test_route(): |
| return {"message": "FastAPI is working!"} |
|
|
| with gr.Blocks() as demo: |
| input_text = gr.Textbox(label="Input", value="") |
| output = gr.Textbox(label="Result") |
| buttons = [ |
| ["7", "8", "9", "/"], |
| ["4", "5", "6", "*"], |
| ["1", "2", "3", "-"], |
| ["0", ".", "=", "+"] |
| ] |
| for row in buttons: |
| with gr.Row(): |
| for button in row: |
| if button == "=": |
| gr.Button(button).click(calculator, inputs=input_text, outputs=output) |
| else: |
| gr.Button(button).click( |
| lambda x, btn=button: append_to_input(x, btn), |
| inputs=[input_text], |
| outputs=input_text |
| ) |
| with gr.Row(): |
| clear_btn = gr.Button("Clear") |
| backspace_btn = gr.Button("⌫") |
| clear_btn.click(clear_input, inputs=[], outputs=input_text) |
| backspace_btn.click(backspace, inputs=[input_text], outputs=input_text) |
|
|
| app = gr.mount_gradio_app(app, demo, path="/") |
|
|
| if __name__ == "__main__": |
| import uvicorn |
| uvicorn.run(app, host="0.0.0.0", port=7860) |