Tiktok-Talent-Info/endpoints/image.py

52 lines
1.9 KiB
Python
Raw Normal View History

2025-01-23 21:50:55 +08:00
from fastapi import UploadFile, Form
from fastapi.responses import JSONResponse
import io
2025-01-24 14:11:46 +08:00
import base64
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
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:
2025-01-24 14:11:46 +08:00
# 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
# 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-23 21:50:55 +08:00
# question_with_image_token = f"{question}\n{IMAGE_TOKEN}"
# response = await asyncio.to_thread(pipe, (question, image))
2025-01-24 14:11:46 +08:00
# return JSONResponse({"query": question, "response": response.text})
2025-01-23 21:50:55 +08:00
# except Exception as e:
2025-01-24 14:11:46 +08:00
# return JSONResponse({"query": question, "error": str(e)})
# import mimetypes
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))
# 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")
# 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)}