youssefleb commited on
Commit
6905aa5
·
verified ·
1 Parent(s): 8844797

Create blaxel_main.py

Browse files
Files changed (1) hide show
  1. blaxel_main.py +62 -0
blaxel_main.py ADDED
@@ -0,0 +1,62 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import asyncio
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
+ # --- API Endpoints ---
19
+
20
+ @app.get("/")
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
+ This is the main endpoint our Gradio app calls.
33
+ It streams back a simple "hello world" test to confirm the connection.
34
+ """
35
+
36
+ # This is the streaming function
37
+ async def stream_solution():
38
+ try:
39
+ # 1. Acknowledge the request
40
+ yield f"[1/6] 🧠 Agent activated. Connection to Blaxel backend successful."
41
+ await asyncio.sleep(1) # simulate work
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
+ # Return the streaming response
62
+ return EventSourceResponse(stream_solution())