Spaces:
Running
Running
Update blaxel_main.py
Browse files- blaxel_main.py +13 -49
blaxel_main.py
CHANGED
|
@@ -1,62 +1,26 @@
|
|
| 1 |
-
|
| 2 |
-
import json
|
| 3 |
from fastapi import FastAPI
|
| 4 |
from pydantic import BaseModel
|
| 5 |
from sse_starlette.sse import EventSourceResponse
|
|
|
|
| 6 |
|
| 7 |
-
# --- API Models ---
|
| 8 |
-
# This defines the data structure we expect from our Gradio app
|
| 9 |
-
class ProblemRequest(BaseModel):
|
| 10 |
-
problem: str
|
| 11 |
-
run_count: int
|
| 12 |
-
do_calibrate: bool
|
| 13 |
-
|
| 14 |
-
# --- FastAPI App ---
|
| 15 |
-
# This creates our web application
|
| 16 |
app = FastAPI()
|
| 17 |
|
| 18 |
-
#
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
def read_root():
|
| 22 |
-
"""
|
| 23 |
-
A simple "health check" endpoint.
|
| 24 |
-
If we can see this in a browser, we know our Blaxel server is running.
|
| 25 |
-
"""
|
| 26 |
-
return {"status": "MudabbirAI Backend is running."}
|
| 27 |
-
|
| 28 |
|
| 29 |
@app.post("/solve_problem")
|
| 30 |
async def solve_problem(request: ProblemRequest):
|
| 31 |
"""
|
| 32 |
-
|
| 33 |
-
It streams back a simple "hello world" test to confirm the connection.
|
| 34 |
"""
|
| 35 |
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
# 2. Show the data we received
|
| 44 |
-
yield f"Received Problem: {request.problem}"
|
| 45 |
-
await asyncio.sleep(1)
|
| 46 |
-
yield f"Received Run Count: {request.run_count}"
|
| 47 |
-
await asyncio.sleep(1)
|
| 48 |
-
|
| 49 |
-
# 3. Send the final "package" (as a string)
|
| 50 |
-
# We use a special "FINAL:" prefix
|
| 51 |
-
final_package = {
|
| 52 |
-
"text": "## Test Successful!\n\nThe Blaxel backend is live. We are now ready to build the real agent logic.",
|
| 53 |
-
"audio": None
|
| 54 |
-
}
|
| 55 |
-
yield f"FINAL:{json.dumps(final_package)}"
|
| 56 |
-
|
| 57 |
-
except Exception as e:
|
| 58 |
-
# Send any errors as part of the stream
|
| 59 |
-
yield f"Backend Error: {str(e)}"
|
| 60 |
|
| 61 |
-
|
| 62 |
-
return EventSourceResponse(stream_solution())
|
|
|
|
| 1 |
+
# blaxel_main.py (Smoke Test)
|
|
|
|
| 2 |
from fastapi import FastAPI
|
| 3 |
from pydantic import BaseModel
|
| 4 |
from sse_starlette.sse import EventSourceResponse
|
| 5 |
+
import asyncio
|
| 6 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
app = FastAPI()
|
| 8 |
|
| 9 |
+
# Input model (matches Gradio's request)
|
| 10 |
+
class ProblemRequest(BaseModel):
|
| 11 |
+
problem: str
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
|
| 13 |
@app.post("/solve_problem")
|
| 14 |
async def solve_problem(request: ProblemRequest):
|
| 15 |
"""
|
| 16 |
+
A simple streaming endpoint to confirm the connection.
|
|
|
|
| 17 |
"""
|
| 18 |
|
| 19 |
+
async def stream_test():
|
| 20 |
+
yield f"Connection established for problem: '{request.problem}'"
|
| 21 |
+
await asyncio.sleep(0.5)
|
| 22 |
+
yield "MudabbirAI is thinking..."
|
| 23 |
+
await asyncio.sleep(0.5)
|
| 24 |
+
yield "Test complete. The connection is good!"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
|
| 26 |
+
return EventSourceResponse(stream_test())
|
|
|