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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +81 -59
app.py CHANGED
@@ -1,59 +1,81 @@
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}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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()