Update app.py
Browse files
app.py
CHANGED
|
@@ -1,7 +1,7 @@
|
|
| 1 |
# --- FastAPI imports ---
|
| 2 |
-
from fastapi import FastAPI, Request, Query
|
| 3 |
from fastapi.responses import JSONResponse
|
| 4 |
-
|
| 5 |
# Add interactive loop for user input with Ctrl+C to break
|
| 6 |
app = FastAPI()
|
| 7 |
|
|
@@ -247,3 +247,23 @@ async def ask(user_input: str = Query(...)):
|
|
| 247 |
if json_obj:
|
| 248 |
return JSONResponse(content=json_obj)
|
| 249 |
return JSONResponse(content={"error": "No valid JSON found", "raw": response}, status_code=422)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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
|
| 6 |
app = FastAPI()
|
| 7 |
|
|
|
|
| 247 |
if json_obj:
|
| 248 |
return JSONResponse(content=json_obj)
|
| 249 |
return JSONResponse(content={"error": "No valid JSON found", "raw": response}, status_code=422)
|
| 250 |
+
|
| 251 |
+
|
| 252 |
+
@app.post("/ask_image")
|
| 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)
|