Spaces:
Running
Running
Commit
·
47f5422
1
Parent(s):
077ebc4
new endpoint
Browse files
main.py
CHANGED
@@ -4,6 +4,7 @@ from io import BytesIO
|
|
4 |
import torch
|
5 |
from fastapi import FastAPI, Query
|
6 |
from PIL import Image
|
|
|
7 |
from qwen_vl_utils import process_vision_info
|
8 |
from transformers import AutoProcessor, Qwen2_5_VLForConditionalGeneration, Qwen2VLForConditionalGeneration
|
9 |
|
@@ -28,35 +29,61 @@ def read_root():
|
|
28 |
return {"message": "API is live. Use the /predict endpoint."}
|
29 |
|
30 |
|
31 |
-
def encode_image(image_path, max_size=(800, 800), quality=85):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
32 |
"""
|
33 |
-
Converts an image from
|
34 |
-
|
35 |
-
Args:
|
36 |
-
image_path (str): The path to the image file.
|
37 |
-
max_size (tuple): The maximum width and height of the resized image.
|
38 |
-
quality (int): The compression quality (1-100, higher means better quality but bigger size).
|
39 |
-
|
40 |
-
Returns:
|
41 |
-
str: Base64-encoded representation of the optimized image.
|
42 |
"""
|
43 |
try:
|
44 |
-
with Image.open(
|
45 |
-
# Convert to RGB (avoid issues with PNG transparency)
|
46 |
img = img.convert("RGB")
|
47 |
-
|
48 |
-
# Resize while maintaining aspect ratio
|
49 |
img.thumbnail(max_size, Image.LANCZOS)
|
50 |
-
|
51 |
-
# Save to buffer with compression
|
52 |
buffer = BytesIO()
|
53 |
-
img.save(
|
54 |
-
buffer, format="JPEG", quality=quality
|
55 |
-
) # Save as JPEG to reduce size
|
56 |
return base64.b64encode(buffer.getvalue()).decode("utf-8")
|
57 |
except Exception as e:
|
58 |
-
|
59 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
60 |
|
61 |
@app.get("/predict")
|
62 |
def describe_image_with_qwen2_vl(image_url: str = Query(...), prompt: str = Query(...)):
|
|
|
4 |
import torch
|
5 |
from fastapi import FastAPI, Query
|
6 |
from PIL import Image
|
7 |
+
from fastapi import FastAPI, File, UploadFile, HTTPException
|
8 |
from qwen_vl_utils import process_vision_info
|
9 |
from transformers import AutoProcessor, Qwen2_5_VLForConditionalGeneration, Qwen2VLForConditionalGeneration
|
10 |
|
|
|
29 |
return {"message": "API is live. Use the /predict endpoint."}
|
30 |
|
31 |
|
32 |
+
# def encode_image(image_path, max_size=(800, 800), quality=85):
|
33 |
+
# """
|
34 |
+
# Converts an image from a local file path to a Base64-encoded string with optimized size.
|
35 |
+
|
36 |
+
# Args:
|
37 |
+
# image_path (str): The path to the image file.
|
38 |
+
# max_size (tuple): The maximum width and height of the resized image.
|
39 |
+
# quality (int): The compression quality (1-100, higher means better quality but bigger size).
|
40 |
+
|
41 |
+
# Returns:
|
42 |
+
# str: Base64-encoded representation of the optimized image.
|
43 |
+
# """
|
44 |
+
# try:
|
45 |
+
# with Image.open(image_path) as img:
|
46 |
+
# # Convert to RGB (avoid issues with PNG transparency)
|
47 |
+
# img = img.convert("RGB")
|
48 |
+
|
49 |
+
# # Resize while maintaining aspect ratio
|
50 |
+
# img.thumbnail(max_size, Image.LANCZOS)
|
51 |
+
|
52 |
+
# # Save to buffer with compression
|
53 |
+
# buffer = BytesIO()
|
54 |
+
# img.save(
|
55 |
+
# buffer, format="JPEG", quality=quality
|
56 |
+
# ) # Save as JPEG to reduce size
|
57 |
+
# return base64.b64encode(buffer.getvalue()).decode("utf-8")
|
58 |
+
# except Exception as e:
|
59 |
+
# print(f"❌ Error encoding image {image_path}: {e}")
|
60 |
+
# return None
|
61 |
+
|
62 |
+
def encode_image(image_data: BytesIO, max_size=(800, 800), quality=85):
|
63 |
"""
|
64 |
+
Converts an image from file data to a Base64-encoded string with optimized size.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
65 |
"""
|
66 |
try:
|
67 |
+
with Image.open(image_data) as img:
|
|
|
68 |
img = img.convert("RGB")
|
|
|
|
|
69 |
img.thumbnail(max_size, Image.LANCZOS)
|
|
|
|
|
70 |
buffer = BytesIO()
|
71 |
+
img.save(buffer, format="JPEG", quality=quality)
|
|
|
|
|
72 |
return base64.b64encode(buffer.getvalue()).decode("utf-8")
|
73 |
except Exception as e:
|
74 |
+
raise HTTPException(status_code=500, detail=f"Error encoding image: {e}")
|
75 |
+
|
76 |
+
@app.post("/encode-image/")
|
77 |
+
async def upload_and_encode_image(file: UploadFile = File(...)):
|
78 |
+
"""
|
79 |
+
Endpoint to upload an image file and return its Base64-encoded representation.
|
80 |
+
"""
|
81 |
+
try:
|
82 |
+
image_data = BytesIO(await file.read())
|
83 |
+
encoded_string = encode_image(image_data)
|
84 |
+
return {"filename": file.filename, "encoded_image": encoded_string}
|
85 |
+
except Exception as e:
|
86 |
+
raise HTTPException(status_code=400, detail=f"Invalid file: {e}")
|
87 |
|
88 |
@app.get("/predict")
|
89 |
def describe_image_with_qwen2_vl(image_url: str = Query(...), prompt: str = Query(...)):
|