Abhlash commited on
Commit
e6dcafa
·
verified ·
1 Parent(s): ef3dec0

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +59 -93
app.py CHANGED
@@ -1,93 +1,59 @@
1
- import gradio as gr
2
- import requests
3
- from MathEngine import calculate, CalculationRequest, app as fastapi_app
4
- import uvicorn
5
- from multiprocessing import Process
6
- from pyngrok import ngrok
7
-
8
- # Gradio Interface
9
- history = []
10
- current_input = ""
11
-
12
- def gradio_calculate(a, b, operation):
13
- request = CalculationRequest(a=a, b=b, operation=operation)
14
- response = calculate(request)
15
- result = response["result"]
16
- history.append(f"{a} {operation} {b} = {result}")
17
- return "\n".join(history)
18
-
19
- def update_input(digit):
20
- global current_input
21
- current_input += str(digit)
22
- return current_input
23
-
24
- def clear_input():
25
- global current_input
26
- current_input = ""
27
- return current_input
28
-
29
- def backspace():
30
- global current_input
31
- current_input = current_input[:-1]
32
- return current_input
33
-
34
- def test_fastapi(a, b, operation):
35
- url = f"{ngrok_tunnel}/calculate"
36
- payload = {"a": a, "b": b, "operation": operation}
37
- headers = {"Content-Type": "application/json"}
38
- response = requests.post(url, json=payload, headers=headers)
39
- return response.json()
40
-
41
- def create_interface():
42
- with gr.Blocks() as demo:
43
- a = gr.Number(label="A", interactive=True)
44
- b = gr.Number(label="B", interactive=True)
45
- result = gr.Textbox(label="Result", lines=10, interactive=False)
46
- current = gr.Textbox(label="Current Input", interactive=False)
47
-
48
- with gr.Row():
49
- for i in range(10):
50
- gr.Button(str(i)).click(lambda x=i: update_input(x), inputs=[], outputs=current)
51
- gr.Button("Back").click(backspace, inputs=[], outputs=current)
52
- gr.Button("Clear").click(clear_input, inputs=[], outputs=current)
53
-
54
- with gr.Row():
55
- gr.Button("Set A").click(lambda x: float(current_input), inputs=[], outputs=a)
56
- gr.Button("Set B").click(lambda x: float(current_input), inputs=[], outputs=b)
57
-
58
- with gr.Row():
59
- gr.Button("Add").click(lambda a, b: gradio_calculate(a, b, "add"), inputs=[a, b], outputs=result)
60
- gr.Button("Subtract").click(lambda a, b: gradio_calculate(a, b, "subtract"), inputs=[a, b], outputs=result)
61
- gr.Button("Multiply").click(lambda a, b: gradio_calculate(a, b, "multiply"), inputs=[a, b], outputs=result)
62
- gr.Button("Divide").click(lambda a, b: gradio_calculate(a, b, "divide"), inputs=[a, b], outputs=result)
63
-
64
- with gr.Row():
65
- gr.Button("Test FastAPI Add").click(lambda a, b: test_fastapi(a, b, "add"), inputs=[a, b], outputs=result)
66
- gr.Button("Test FastAPI Subtract").click(lambda a, b: test_fastapi(a, b, "subtract"), inputs=[a, b], outputs=result)
67
- gr.Button("Test FastAPI Multiply").click(lambda a, b: test_fastapi(a, b, "multiply"), inputs=[a, b], outputs=result)
68
- gr.Button("Test FastAPI Divide").click(lambda a, b: test_fastapi(a, b, "divide"), inputs=[a, b], outputs=result)
69
-
70
- return demo
71
-
72
- def run_gradio():
73
- demo = create_interface()
74
- demo.launch()
75
-
76
- def run_fastapi():
77
- uvicorn.run(fastapi_app, host="0.0.0.0", port=8000)
78
-
79
- def run_ngrok():
80
- global ngrok_tunnel
81
- ngrok_tunnel = ngrok.connect(8000)
82
- print(f" * Ngrok tunnel available at: {ngrok_tunnel.public_url}")
83
-
84
- if __name__ == "__main__":
85
- p1 = Process(target=run_gradio)
86
- p2 = Process(target=run_fastapi)
87
- p3 = Process(target=run_ngrok)
88
- p1.start()
89
- p2.start()
90
- p3.start()
91
- p1.join()
92
- p2.join()
93
- p3.join()
 
1
+ from fastapi import FastAPI
2
+ from pydantic import BaseModel
3
+ from transformers import pipeline
4
+
5
+ app = FastAPI()
6
+
7
+ # Define request model for math operations
8
+ class CalculationRequest(BaseModel):
9
+ a: float
10
+ b: float
11
+ operation: str
12
+
13
+ # Load Hugging Face model (example: a simple text generation model)
14
+ model = pipeline('text-generation', model='gpt2')
15
+
16
+ @app.post("/calculate")
17
+ def calculate(request: CalculationRequest):
18
+ a = request.a
19
+ b = request.b
20
+ operation = request.operation
21
+
22
+ if operation == "add":
23
+ result = a + b
24
+ elif operation == "subtract":
25
+ result = a - b
26
+ elif operation == "multiply":
27
+ result = a * b
28
+ elif operation == "divide":
29
+ result = a / b
30
+ else:
31
+ return {"error": "Invalid operation"}
32
+
33
+ return {"result": result}
34
+
35
+ # Example endpoint using Hugging Face model
36
+ @app.post("/generate")
37
+ def generate_text(prompt: str):
38
+ generated = model(prompt, max_length=50)
39
+ return {"generated_text": generated[0]['generated_text']}
40
+
41
+ # New endpoint for testing math operations
42
+ @app.post("/test_math")
43
+ def test_math(request: CalculationRequest):
44
+ a = request.a
45
+ b = request.b
46
+ operation = request.operation
47
+
48
+ if operation == "add":
49
+ result = a + b
50
+ elif operation == "subtract":
51
+ result = a - b
52
+ elif operation == "multiply":
53
+ result = a * b
54
+ elif operation == "divide":
55
+ result = a / b
56
+ else:
57
+ return {"error": "Invalid operation"}
58
+
59
+ return {"result": result}