Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -126,56 +126,56 @@ def askimage():
|
|
| 126 |
return jsonify({"error": "No image provided"}), 400
|
| 127 |
|
| 128 |
try:
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
# Convert RGBA to RGB if needed
|
| 134 |
-
if img.mode in ('RGBA', 'LA', 'P'):
|
| 135 |
-
background = Image.new('RGB', img.size, (255, 255, 255))
|
| 136 |
-
if img.mode == 'P':
|
| 137 |
-
img = img.convert('RGBA')
|
| 138 |
-
background.paste(img, mask=img.split()[-1] if img.mode == 'RGBA' else None)
|
| 139 |
-
img = background
|
| 140 |
-
|
| 141 |
-
# Resize if too large (max 33 megapixels)
|
| 142 |
-
max_pixels = 33 * 1000000
|
| 143 |
-
if img.width * img.height > max_pixels:
|
| 144 |
-
ratio = (max_pixels / (img.width * img.height)) ** 0.5
|
| 145 |
-
new_size = (int(img.width * ratio), int(img.height * ratio))
|
| 146 |
-
img = img.resize(new_size, Image.Resampling.LANCZOS)
|
| 147 |
-
|
| 148 |
-
# Compress to JPEG with quality adjustment to stay under 4MB
|
| 149 |
-
output = BytesIO()
|
| 150 |
-
quality = 85
|
| 151 |
-
while quality > 20:
|
| 152 |
-
output.seek(0)
|
| 153 |
-
output.truncate()
|
| 154 |
-
img.convert('RGB').save(output, format='JPEG', quality=quality, optimize=True)
|
| 155 |
-
size_mb = output.tell() / (1024 * 1024)
|
| 156 |
|
| 157 |
-
#
|
| 158 |
-
if
|
| 159 |
-
|
| 160 |
-
|
| 161 |
-
|
| 162 |
-
|
| 163 |
-
|
| 164 |
-
|
| 165 |
-
# Create data URL
|
| 166 |
-
image_data_url = f"data:image/jpeg;base64,{base64_image}"
|
| 167 |
-
# Build prompt in your existing format
|
| 168 |
-
prompt = "Image" + "%" + image_data_url + "%" + mode + "%" + lang
|
| 169 |
-
print(prompt)
|
| 170 |
|
| 171 |
-
|
| 172 |
-
|
| 173 |
-
|
| 174 |
-
|
| 175 |
-
|
| 176 |
-
|
| 177 |
-
|
| 178 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 179 |
except Exception as e:
|
| 180 |
return jsonify({"error": str(e)}), 500
|
| 181 |
|
|
|
|
| 126 |
return jsonify({"error": "No image provided"}), 400
|
| 127 |
|
| 128 |
try:
|
| 129 |
+
# Read and compress image
|
| 130 |
+
image_bytes = image_file.read()
|
| 131 |
+
img = Image.open(BytesIO(image_bytes))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 132 |
|
| 133 |
+
# Convert RGBA to RGB if needed
|
| 134 |
+
if img.mode in ('RGBA', 'LA', 'P'):
|
| 135 |
+
background = Image.new('RGB', img.size, (255, 255, 255))
|
| 136 |
+
if img.mode == 'P':
|
| 137 |
+
img = img.convert('RGBA')
|
| 138 |
+
background.paste(img, mask=img.split()[-1] if img.mode == 'RGBA' else None)
|
| 139 |
+
img = background
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 140 |
|
| 141 |
+
# Resize if too large (max 33 megapixels)
|
| 142 |
+
max_pixels = 33 * 1000000
|
| 143 |
+
if img.width * img.height > max_pixels:
|
| 144 |
+
ratio = (max_pixels / (img.width * img.height)) ** 0.5
|
| 145 |
+
new_size = (int(img.width * ratio), int(img.height * ratio))
|
| 146 |
+
img = img.resize(new_size, Image.Resampling.LANCZOS)
|
| 147 |
+
|
| 148 |
+
# Compress to JPEG with quality adjustment to stay under 4MB
|
| 149 |
+
output = BytesIO()
|
| 150 |
+
quality = 85
|
| 151 |
+
while quality > 20:
|
| 152 |
+
output.seek(0)
|
| 153 |
+
output.truncate()
|
| 154 |
+
img.convert('RGB').save(output, format='JPEG', quality=quality, optimize=True)
|
| 155 |
+
size_mb = output.tell() / (1024 * 1024)
|
| 156 |
+
|
| 157 |
+
# Base64 increases size by ~33%, so aim for 3MB max
|
| 158 |
+
if size_mb < 3.0:
|
| 159 |
+
break
|
| 160 |
+
quality -= 10
|
| 161 |
+
|
| 162 |
+
compressed_bytes = output.getvalue()
|
| 163 |
+
base64_image = base64.b64encode(compressed_bytes).decode('utf-8')
|
| 164 |
+
|
| 165 |
+
# Create data URL
|
| 166 |
+
image_data_url = f"data:image/jpeg;base64,{base64_image}"
|
| 167 |
+
# Build prompt in your existing format
|
| 168 |
+
prompt = "Image" + "%" + image_data_url + "%" + mode + "%" + lang
|
| 169 |
+
print(prompt)
|
| 170 |
+
|
| 171 |
+
return Response(
|
| 172 |
+
stream_with_context(stream_openrouter(prompt)),
|
| 173 |
+
mimetype="text/event-stream",
|
| 174 |
+
headers={
|
| 175 |
+
"Cache-Control": "no-cache",
|
| 176 |
+
"X-Accel-Buffering": "no"
|
| 177 |
+
}
|
| 178 |
+
)
|
| 179 |
except Exception as e:
|
| 180 |
return jsonify({"error": str(e)}), 500
|
| 181 |
|