1MR commited on
Commit
8b11159
·
verified ·
1 Parent(s): 829a513

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +8 -3
app.py CHANGED
@@ -253,17 +253,22 @@ async def ask(user_input: str = Query(...)):
253
  async def ask_image(user_input: str = Form(...), image: UploadFile = File(...)):
254
  if not user_input.strip():
255
  return JSONResponse(content={"error": "user_input is required"}, status_code=400)
256
- # Save the uploaded image to a file
257
- image_path = "Taken_image.jpg"
 
 
 
258
  with open(image_path, "wb") as buffer:
259
  shutil.copyfileobj(image.file, buffer)
 
260
  # Now call the agent as usual
261
  loop = asyncio.get_event_loop()
262
  try:
263
  response = await loop.run_in_executor(None, query_agent_with_planning, user_input)
264
  except asyncio.CancelledError:
265
  return JSONResponse(content={"error": "Request was cancelled"}, status_code=499)
 
266
  json_obj = extract_json_from_response(response)
267
  if json_obj:
268
  return JSONResponse(content=json_obj)
269
- return JSONResponse(content={"error": "No valid JSON found", "raw": response}, status_code=422)
 
253
  async def ask_image(user_input: str = Form(...), image: UploadFile = File(...)):
254
  if not user_input.strip():
255
  return JSONResponse(content={"error": "user_input is required"}, status_code=400)
256
+
257
+ # Save uploaded image in a safe temporary directory
258
+ tmp_dir = tempfile.gettempdir()
259
+ image_path = os.path.join(tmp_dir, "Taken_image.jpg")
260
+
261
  with open(image_path, "wb") as buffer:
262
  shutil.copyfileobj(image.file, buffer)
263
+
264
  # Now call the agent as usual
265
  loop = asyncio.get_event_loop()
266
  try:
267
  response = await loop.run_in_executor(None, query_agent_with_planning, user_input)
268
  except asyncio.CancelledError:
269
  return JSONResponse(content={"error": "Request was cancelled"}, status_code=499)
270
+
271
  json_obj = extract_json_from_response(response)
272
  if json_obj:
273
  return JSONResponse(content=json_obj)
274
+ return JSONResponse(content={"error": "No valid JSON found", "raw": response}, status_code=422)