Update app.py
Browse files12345263 unmodified lines269270271272273274# --- FastAPI imports ---from fastapi import FastAPI, Request, Query, File, UploadFile, Formfrom fastapi.responses import JSONResponseimport shutil# Add interactive loop for user input with Ctrl+C to break263 unmodified linesexcept asyncio.CancelledError:return JSONResponse(content={"error": "Request was cancelled"}, status_code=499)json_obj = extract_json_from_response(response)if json_obj:return JSONResponse(content=json_obj)123456263 unmodified lines270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317# --- FastAPI imports ---from fastapi import FastAPI, Request, Query, File, UploadFile, Formfrom fastapi.responses import JSONResponseimport shutil# Add interactive loop for user input with Ctrl+C to break263 unmodified linesexcept asyncio.CancelledError:return JSONResponse(content={"error": "Request was cancelled"}, status_code=499)json_obj = extract_json_from_response(response)if json_obj:return JSONResponse(content=json_obj)return JSONResponse(content={"error": "No valid JSON found", "raw": response}, status_code=422)@app.post("/query")async def query(user_input: str = Form(...), image: UploadFile = File(None)):"""General endpoint:- If only text is provided -> behaves like /ask- If text + image is provided -> behaves like /ask_image"""if not user_input.strip():return JSONResponse(content={"error": "user_input is required"}, status_code=400)loop = asyncio.get_event_loop()# Case 1: text only -> call ask logicif image is None:try:response = await loop.run_in_executor(None, query_agent_with_planning, user_input)except asyncio.CancelledError:return JSONResponse(content={"error": "Request was cancelled"}, status_code=499)json_obj = extract_json_from_response(response)if json_obj:return JSONResponse(content=json_obj)return JSONResponse(content={"error": "No valid JSON found", "raw": response}, status_code=422)# Case 2: text + image -> call ask_image logictmp_dir = tempfile.gettempdir()image_path = os.path.join(tmp_dir, "Taken_image.jpg")with open(image_path, "wb") as buffer:shutil.copyfileobj(image.file, buffer)try:response = await loop.run_in_executor(None, query_agent_with_planning, user_input)except asyncio.CancelledError:return JSONResponse(content={"error": "Request was cancelled"}, status_code=499)json_obj = extract_json_from_response(response)if json_obj:return JSONResponse(content=json_obj)
app.py+43