2025-01-23 21:50:55 +08:00
|
|
|
from fastapi import UploadFile, Form
|
|
|
|
from fastapi.responses import JSONResponse
|
|
|
|
import io
|
2025-01-23 21:57:08 +08:00
|
|
|
import asyncio
|
2025-01-24 14:11:46 +08:00
|
|
|
import numpy as np
|
2025-01-23 21:50:55 +08:00
|
|
|
from PIL import Image
|
2025-02-07 19:18:35 +08:00
|
|
|
from pipeline_setup import pipe, IMAGE_TOKEN
|
2025-01-23 21:50:55 +08:00
|
|
|
from utils.image_processing import encode_image_base64
|
|
|
|
|
2025-01-26 20:42:56 +08:00
|
|
|
async def image_query(file: UploadFile, question: str = Form(...)):
|
|
|
|
"""
|
|
|
|
API endpoint to process an image with the user's query.
|
|
|
|
"""
|
|
|
|
try:
|
|
|
|
if file.content_type not in ["image/jpeg", "image/png"]:
|
|
|
|
return JSONResponse({"query": question, "error": "Unsupported file type."})
|
2025-01-23 21:50:55 +08:00
|
|
|
|
2025-01-26 20:42:56 +08:00
|
|
|
image_data = await file.read()
|
|
|
|
image = Image.open(io.BytesIO(image_data)).convert("RGB").resize((512, 512))
|
|
|
|
encoded_image_base64 = encode_image_base64(image)
|
2025-01-24 14:11:46 +08:00
|
|
|
|
2025-01-26 20:42:56 +08:00
|
|
|
question_with_image_token = f"{question}\n{IMAGE_TOKEN}"
|
2025-02-07 19:18:35 +08:00
|
|
|
response = await asyncio.to_thread(pipe, (question, image))
|
2025-01-26 20:42:56 +08:00
|
|
|
return JSONResponse({"query": question, "response": response.text})
|
|
|
|
except Exception as e:
|
|
|
|
return JSONResponse({"query": question, "error": str(e)})
|
2025-01-24 14:11:46 +08:00
|
|
|
|
|
|
|
|
2025-01-26 20:42:56 +08:00
|
|
|
# async def image_query(image: np.ndarray, question: str):
|
|
|
|
# """
|
|
|
|
# API endpoint to process an image (as numpy array) with the user's query.
|
|
|
|
# """
|
|
|
|
# try:
|
|
|
|
# # Convert the numpy array to a PIL Image
|
|
|
|
# image = Image.fromarray(image).convert("RGB").resize((512, 512))
|
2025-01-24 14:11:46 +08:00
|
|
|
|
2025-01-26 20:42:56 +08:00
|
|
|
# # Encode the image to base64 (optional, if needed by your pipeline)
|
|
|
|
# buffered = io.BytesIO()
|
|
|
|
# image.save(buffered, format="JPEG")
|
|
|
|
# encoded_image_base64 = base64.b64encode(buffered.getvalue()).decode("utf-8")
|
2025-01-24 14:11:46 +08:00
|
|
|
|
2025-01-26 20:42:56 +08:00
|
|
|
# # Prepare the query with the image token
|
|
|
|
# question_with_image_token = f"{question}\n{IMAGE_TOKEN}"
|
2025-01-24 14:11:46 +08:00
|
|
|
|
2025-01-26 20:42:56 +08:00
|
|
|
# # Query the model
|
|
|
|
# response = await asyncio.to_thread(pipe, (question, image))
|
|
|
|
# return {"query": question, "response": response.text}
|
|
|
|
# except Exception as e:
|
|
|
|
# return {"query": question, "error": str(e)}
|