Spaces:
Running
Running
Update api.py
Browse files
api.py
CHANGED
|
@@ -14,18 +14,13 @@ logger = logging.getLogger(__name__)
|
|
| 14 |
|
| 15 |
app = FastAPI()
|
| 16 |
|
| 17 |
-
# --- ADD THIS NEW TEST ENDPOINT ---
|
| 18 |
@app.get("/")
|
| 19 |
def health_check():
|
| 20 |
return {"status": "ok", "message": "API is running!"}
|
| 21 |
-
# --- END OF NEW TEST ENDPOINT ---
|
| 22 |
|
| 23 |
-
# This block allows your React frontend to make requests to your Python backend.
|
| 24 |
-
# IMPORTANT: When you deploy your React app to a live URL, you must add that URL to this list.
|
| 25 |
origins = [
|
| 26 |
"http://localhost:3000",
|
| 27 |
"http://localhost:3001",
|
| 28 |
-
# e.g., "https://your-react-app.vercel.app"
|
| 29 |
]
|
| 30 |
|
| 31 |
app.add_middleware(
|
|
@@ -38,4 +33,21 @@ app.add_middleware(
|
|
| 38 |
|
| 39 |
counselor = UltraAdvancedHybridCounselor()
|
| 40 |
|
| 41 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
|
| 15 |
app = FastAPI()
|
| 16 |
|
|
|
|
| 17 |
@app.get("/")
|
| 18 |
def health_check():
|
| 19 |
return {"status": "ok", "message": "API is running!"}
|
|
|
|
| 20 |
|
|
|
|
|
|
|
| 21 |
origins = [
|
| 22 |
"http://localhost:3000",
|
| 23 |
"http://localhost:3001",
|
|
|
|
| 24 |
]
|
| 25 |
|
| 26 |
app.add_middleware(
|
|
|
|
| 33 |
|
| 34 |
counselor = UltraAdvancedHybridCounselor()
|
| 35 |
|
| 36 |
+
# --- THIS BLOCK WAS CAUSING THE ERROR ---
|
| 37 |
+
# The two lines below must be indented to be inside the class.
|
| 38 |
+
class CounselRequest(BaseModel):
|
| 39 |
+
query: str
|
| 40 |
+
session_id: str
|
| 41 |
+
# --- END OF FIX ---
|
| 42 |
+
|
| 43 |
+
@app.post("/counsel")
|
| 44 |
+
async def counsel(request: CounselRequest):
|
| 45 |
+
async def stream_gen():
|
| 46 |
+
try:
|
| 47 |
+
async for chunk in counselor.get_comprehensive_answer(request.query, request.session_id):
|
| 48 |
+
yield chunk.encode('utf-8')
|
| 49 |
+
except Exception as e:
|
| 50 |
+
logger.error(f"Streaming error: {e}")
|
| 51 |
+
yield "An error occurred during streaming.".encode('utf-8')
|
| 52 |
+
|
| 53 |
+
return StreamingResponse(stream_gen(), media_type="text/plain")
|