Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,35 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import io
|
2 |
+
from PIL import Image
|
3 |
+
import torch
|
4 |
+
from transformers import AutoProcessor, AutoModelForVisionEncoderDecoder
|
5 |
+
|
6 |
+
# Load the model and processor
|
7 |
+
model_name = "colt12/maxcushion"
|
8 |
+
processor = AutoProcessor.from_pretrained(model_name)
|
9 |
+
model = AutoModelForVisionEncoderDecoder.from_pretrained(model_name)
|
10 |
+
|
11 |
+
def predict(image_bytes):
|
12 |
+
# Open the image using PIL
|
13 |
+
image = Image.open(io.BytesIO(image_bytes))
|
14 |
+
|
15 |
+
# Preprocess the image
|
16 |
+
pixel_values = processor(images=image, return_tensors="pt").pixel_values
|
17 |
+
|
18 |
+
# Generate the caption
|
19 |
+
generated_ids = model.generate(pixel_values, max_length=50)
|
20 |
+
generated_caption = processor.batch_decode(generated_ids, skip_special_tokens=True)[0]
|
21 |
+
|
22 |
+
return generated_caption
|
23 |
+
|
24 |
+
def run(raw_image):
|
25 |
+
# Input validation
|
26 |
+
if not raw_image:
|
27 |
+
raise ValueError("No image provided")
|
28 |
+
|
29 |
+
try:
|
30 |
+
# Process the image and generate the caption
|
31 |
+
result = predict(raw_image)
|
32 |
+
return {"caption": result}
|
33 |
+
except Exception as e:
|
34 |
+
# Error handling
|
35 |
+
return {"error": str(e)}
|