Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,93 +1,59 @@
|
|
| 1 |
-
|
| 2 |
-
import
|
| 3 |
-
from
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
return
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 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}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|