Tiktok-Talent-Info/endpoints/image.py

70 lines
2.6 KiB
Python
Raw Normal View History

2025-01-23 21:50:55 +08:00
from fastapi import UploadFile, Form
from fastapi.responses import JSONResponse
2025-02-08 18:52:07 +08:00
import base64
2025-01-23 21:50:55 +08:00
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-05-12 11:22:46 +08:00
# api
2025-03-22 20:54:10 +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."})
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)})
2025-05-12 11:22:46 +08:00
# gradio
2025-03-22 20:54:10 +08:00
# async def image_query(image: np.ndarray, question: str):
2025-02-08 18:52:07 +08:00
# try:
2025-03-22 20:54:10 +08:00
# # 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-03-22 20:54:10 +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")
# # Prepare the query with the image token
2025-02-08 18:52:07 +08:00
# question_with_image_token = f"{question}\n{IMAGE_TOKEN}"
2025-03-22 20:54:10 +08:00
# # Query the model
# response = await asyncio.to_thread(pipe, (question, image))
# return {"query": question, "response": response.text}
2025-02-08 18:52:07 +08:00
# except Exception as e:
2025-03-22 20:54:10 +08:00
# return {"query": question, "error": str(e)}
2025-01-24 14:11:46 +08:00
2025-05-12 11:22:46 +08:00
# celery
2025-03-22 20:54:10 +08:00
# def image_query(image_path: str, question: str):
# try:
# print("image_path in image_query...")
# with open(image_path, "rb") as file:
# image_data = file.read()
2025-01-24 14:11:46 +08:00
2025-03-22 20:54:10 +08:00
# 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-03-22 20:54:10 +08:00
# # Prepare the question with the image token
# question_with_image_token = f"{question}\n{IMAGE_TOKEN}"
# # Run model inference (blocking call, but can be async)
# response = pipe((question_with_image_token, image))
# return {"query": question, "response": response.text}
# except Exception as e:
# return {"query": question, "error": str(e)}