Update app.py
Browse files
app.py
CHANGED
|
@@ -1,5 +1,6 @@
|
|
| 1 |
# --- FastAPI imports ---
|
| 2 |
from fastapi import FastAPI, Request, Query, File, UploadFile, Form
|
|
|
|
| 3 |
from fastapi.responses import JSONResponse
|
| 4 |
import shutil
|
| 5 |
# Add interactive loop for user input with Ctrl+C to break
|
|
@@ -269,6 +270,48 @@ async def ask_image(user_input: str = Form(...), image: UploadFile = File(...)):
|
|
| 269 |
except asyncio.CancelledError:
|
| 270 |
return JSONResponse(content={"error": "Request was cancelled"}, status_code=499)
|
| 271 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 272 |
json_obj = extract_json_from_response(response)
|
| 273 |
if json_obj:
|
| 274 |
return JSONResponse(content=json_obj)
|
|
|
|
| 1 |
# --- FastAPI imports ---
|
| 2 |
from fastapi import FastAPI, Request, Query, File, UploadFile, Form
|
| 3 |
+
|
| 4 |
from fastapi.responses import JSONResponse
|
| 5 |
import shutil
|
| 6 |
# Add interactive loop for user input with Ctrl+C to break
|
|
|
|
| 270 |
except asyncio.CancelledError:
|
| 271 |
return JSONResponse(content={"error": "Request was cancelled"}, status_code=499)
|
| 272 |
|
| 273 |
+
json_obj = extract_json_from_response(response)
|
| 274 |
+
if json_obj:
|
| 275 |
+
return JSONResponse(content=json_obj)
|
| 276 |
+
return JSONResponse(content={"error": "No valid JSON found", "raw": response}, status_code=422)
|
| 277 |
+
|
| 278 |
+
|
| 279 |
+
@app.post("/query")
|
| 280 |
+
async def query(user_input: str = Form(...), image: UploadFile = File(None)):
|
| 281 |
+
"""
|
| 282 |
+
General endpoint:
|
| 283 |
+
- If only text is provided -> behaves like /ask
|
| 284 |
+
- If text + image is provided -> behaves like /ask_image
|
| 285 |
+
"""
|
| 286 |
+
if not user_input.strip():
|
| 287 |
+
return JSONResponse(content={"error": "user_input is required"}, status_code=400)
|
| 288 |
+
|
| 289 |
+
loop = asyncio.get_event_loop()
|
| 290 |
+
|
| 291 |
+
# Case 1: text only -> call ask logic
|
| 292 |
+
if image is None:
|
| 293 |
+
try:
|
| 294 |
+
response = await loop.run_in_executor(None, query_agent_with_planning, user_input)
|
| 295 |
+
except asyncio.CancelledError:
|
| 296 |
+
return JSONResponse(content={"error": "Request was cancelled"}, status_code=499)
|
| 297 |
+
|
| 298 |
+
json_obj = extract_json_from_response(response)
|
| 299 |
+
if json_obj:
|
| 300 |
+
return JSONResponse(content=json_obj)
|
| 301 |
+
return JSONResponse(content={"error": "No valid JSON found", "raw": response}, status_code=422)
|
| 302 |
+
|
| 303 |
+
# Case 2: text + image -> call ask_image logic
|
| 304 |
+
tmp_dir = tempfile.gettempdir()
|
| 305 |
+
image_path = os.path.join(tmp_dir, "Taken_image.jpg")
|
| 306 |
+
|
| 307 |
+
with open(image_path, "wb") as buffer:
|
| 308 |
+
shutil.copyfileobj(image.file, buffer)
|
| 309 |
+
|
| 310 |
+
try:
|
| 311 |
+
response = await loop.run_in_executor(None, query_agent_with_planning, user_input)
|
| 312 |
+
except asyncio.CancelledError:
|
| 313 |
+
return JSONResponse(content={"error": "Request was cancelled"}, status_code=499)
|
| 314 |
+
|
| 315 |
json_obj = extract_json_from_response(response)
|
| 316 |
if json_obj:
|
| 317 |
return JSONResponse(content=json_obj)
|