54 lines
2.1 KiB
Python
54 lines
2.1 KiB
Python
from fastapi import UploadFile, Form
|
|
from fastapi.responses import JSONResponse
|
|
import io
|
|
from PIL import Image
|
|
from pipeline_setup import pipe, IMAGE_TOKEN
|
|
from utils.image_processing import encode_image_base64
|
|
|
|
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."})
|
|
|
|
image_data = await file.read()
|
|
image = Image.open(io.BytesIO(image_data)).convert("RGB").resize((512, 512))
|
|
encoded_image_base64 = encode_image_base64(image)
|
|
|
|
question_with_image_token = f"{question}\n{IMAGE_TOKEN}"
|
|
response = await asyncio.to_thread(pipe, (question, image))
|
|
return JSONResponse({"query": question, "response": response.text})
|
|
except Exception as e:
|
|
return JSONResponse({"query": question, "error": str(e)})
|
|
|
|
|
|
# import mimetypes
|
|
# async def image_query(file: UploadFile, question: str = Form(...)):
|
|
# """
|
|
# API endpoint to process an image with the user's query.
|
|
# """
|
|
# try:
|
|
# # Get the file path from the UploadFile object
|
|
# file_path = file.filename
|
|
|
|
# # Determine the file type using the file extension
|
|
# file_type, _ = mimetypes.guess_type(file_path)
|
|
# if file_type not in ["image/jpeg", "image/png"]:
|
|
# return {"query": question, "error": "Unsupported file type."}
|
|
|
|
# # Read the image file
|
|
# image_data = await file.read()
|
|
# image = Image.open(io.BytesIO(image_data)).convert("RGB").resize((512, 512))
|
|
# encoded_image_base64 = encode_image_base64(image)
|
|
|
|
# # Prepare the query with the image token
|
|
# question_with_image_token = f"{question}\n{IMAGE_TOKEN}"
|
|
|
|
# # 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)}
|