Spaces:
Sleeping
Sleeping
Update main.py
Browse files
main.py
CHANGED
|
@@ -169,60 +169,96 @@ def send_text_request(model_name, prompt, image):
|
|
| 169 |
return None
|
| 170 |
|
| 171 |
|
| 172 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 173 |
# OPEN IMAGE PROXY ENDPOINT (NO AUTHENTICATION)
|
| 174 |
# =============================================================================
|
| 175 |
@app.route('/api/image-proxy', methods=['GET'])
|
| 176 |
def image_proxy():
|
| 177 |
image_url = request.args.get('url')
|
|
|
|
|
|
|
| 178 |
if not image_url:
|
|
|
|
| 179 |
return jsonify({'error': 'URL parameter is missing.'}), 400
|
| 180 |
|
| 181 |
try:
|
| 182 |
# Parse Firebase Storage URL
|
| 183 |
# Expected format: https://storage.googleapis.com/bucket-name/path/to/file.ext
|
| 184 |
if 'storage.googleapis.com' not in image_url:
|
|
|
|
| 185 |
return jsonify({'error': 'Invalid Firebase Storage URL.'}), 400
|
| 186 |
|
|
|
|
|
|
|
| 187 |
# Extract bucket name and blob path from the URL
|
| 188 |
url_parts = image_url.split('storage.googleapis.com/')[1]
|
|
|
|
|
|
|
| 189 |
# Remove query parameters if present
|
| 190 |
url_parts = url_parts.split('?')[0]
|
|
|
|
| 191 |
|
| 192 |
# Split into bucket name and blob path
|
| 193 |
path_components = url_parts.split('/', 1)
|
|
|
|
|
|
|
| 194 |
if len(path_components) < 2:
|
|
|
|
| 195 |
return jsonify({'error': 'Invalid URL format.'}), 400
|
| 196 |
|
| 197 |
url_bucket_name = path_components[0]
|
| 198 |
blob_path = path_components[1]
|
| 199 |
|
|
|
|
|
|
|
|
|
|
| 200 |
# Verify bucket name matches (optional security check)
|
| 201 |
expected_bucket_name = bucket.name
|
|
|
|
|
|
|
| 202 |
if url_bucket_name != expected_bucket_name:
|
|
|
|
| 203 |
return jsonify({'error': 'Bucket name mismatch.'}), 403
|
| 204 |
|
|
|
|
|
|
|
| 205 |
# Get the blob
|
| 206 |
blob = bucket.blob(blob_path)
|
| 207 |
|
|
|
|
| 208 |
if not blob.exists():
|
|
|
|
| 209 |
return jsonify({'error': 'Image not found.'}), 404
|
| 210 |
|
|
|
|
| 211 |
# Download and return the image
|
| 212 |
image_bytes = blob.download_as_bytes()
|
| 213 |
content_type = blob.content_type or 'application/octet-stream'
|
| 214 |
|
|
|
|
|
|
|
| 215 |
# Add cache headers for better performance
|
| 216 |
response = Response(image_bytes, content_type=content_type)
|
| 217 |
response.headers['Cache-Control'] = 'public, max-age=3600' # Cache for 1 hour
|
| 218 |
return response
|
| 219 |
|
| 220 |
except IndexError as e:
|
| 221 |
-
|
|
|
|
| 222 |
return jsonify({'error': 'Invalid URL format.'}), 400
|
| 223 |
except Exception as e:
|
| 224 |
# This will catch parsing errors or other unexpected issues.
|
| 225 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 226 |
return jsonify({'error': 'Internal server error processing the image request.'}), 500
|
| 227 |
|
| 228 |
# -----------------------------------------------------------------------------
|
|
|
|
| 169 |
return None
|
| 170 |
|
| 171 |
|
| 172 |
+
import logging
|
| 173 |
+
|
| 174 |
+
# Configure logging at the top of your file if not already done
|
| 175 |
+
logging.basicConfig(level=logging.INFO)
|
| 176 |
+
logger = logging.getLogger(__name__)
|
| 177 |
+
|
| 178 |
+
# =============================================================================
|
| 179 |
# OPEN IMAGE PROXY ENDPOINT (NO AUTHENTICATION)
|
| 180 |
# =============================================================================
|
| 181 |
@app.route('/api/image-proxy', methods=['GET'])
|
| 182 |
def image_proxy():
|
| 183 |
image_url = request.args.get('url')
|
| 184 |
+
logger.info(f"[IMAGE PROXY] Received URL: {image_url}")
|
| 185 |
+
|
| 186 |
if not image_url:
|
| 187 |
+
logger.error("[IMAGE PROXY] ERROR: URL parameter is missing")
|
| 188 |
return jsonify({'error': 'URL parameter is missing.'}), 400
|
| 189 |
|
| 190 |
try:
|
| 191 |
# Parse Firebase Storage URL
|
| 192 |
# Expected format: https://storage.googleapis.com/bucket-name/path/to/file.ext
|
| 193 |
if 'storage.googleapis.com' not in image_url:
|
| 194 |
+
logger.error(f"[IMAGE PROXY] ERROR: Invalid Firebase Storage URL: {image_url}")
|
| 195 |
return jsonify({'error': 'Invalid Firebase Storage URL.'}), 400
|
| 196 |
|
| 197 |
+
logger.info(f"[IMAGE PROXY] Parsing URL: {image_url}")
|
| 198 |
+
|
| 199 |
# Extract bucket name and blob path from the URL
|
| 200 |
url_parts = image_url.split('storage.googleapis.com/')[1]
|
| 201 |
+
logger.info(f"[IMAGE PROXY] URL parts after split: {url_parts}")
|
| 202 |
+
|
| 203 |
# Remove query parameters if present
|
| 204 |
url_parts = url_parts.split('?')[0]
|
| 205 |
+
logger.info(f"[IMAGE PROXY] URL parts after removing query params: {url_parts}")
|
| 206 |
|
| 207 |
# Split into bucket name and blob path
|
| 208 |
path_components = url_parts.split('/', 1)
|
| 209 |
+
logger.info(f"[IMAGE PROXY] Path components: {path_components}")
|
| 210 |
+
|
| 211 |
if len(path_components) < 2:
|
| 212 |
+
logger.error(f"[IMAGE PROXY] ERROR: Invalid URL format - path_components: {path_components}")
|
| 213 |
return jsonify({'error': 'Invalid URL format.'}), 400
|
| 214 |
|
| 215 |
url_bucket_name = path_components[0]
|
| 216 |
blob_path = path_components[1]
|
| 217 |
|
| 218 |
+
logger.info(f"[IMAGE PROXY] Extracted bucket name: {url_bucket_name}")
|
| 219 |
+
logger.info(f"[IMAGE PROXY] Extracted blob path: {blob_path}")
|
| 220 |
+
|
| 221 |
# Verify bucket name matches (optional security check)
|
| 222 |
expected_bucket_name = bucket.name
|
| 223 |
+
logger.info(f"[IMAGE PROXY] Expected bucket name: {expected_bucket_name}")
|
| 224 |
+
|
| 225 |
if url_bucket_name != expected_bucket_name:
|
| 226 |
+
logger.error(f"[IMAGE PROXY] ERROR: Bucket name mismatch - URL: {url_bucket_name}, Expected: {expected_bucket_name}")
|
| 227 |
return jsonify({'error': 'Bucket name mismatch.'}), 403
|
| 228 |
|
| 229 |
+
logger.info(f"[IMAGE PROXY] Creating blob object for path: {blob_path}")
|
| 230 |
+
|
| 231 |
# Get the blob
|
| 232 |
blob = bucket.blob(blob_path)
|
| 233 |
|
| 234 |
+
logger.info(f"[IMAGE PROXY] Checking if blob exists...")
|
| 235 |
if not blob.exists():
|
| 236 |
+
logger.error(f"[IMAGE PROXY] ERROR: Image not found at path: {blob_path}")
|
| 237 |
return jsonify({'error': 'Image not found.'}), 404
|
| 238 |
|
| 239 |
+
logger.info(f"[IMAGE PROXY] Downloading blob...")
|
| 240 |
# Download and return the image
|
| 241 |
image_bytes = blob.download_as_bytes()
|
| 242 |
content_type = blob.content_type or 'application/octet-stream'
|
| 243 |
|
| 244 |
+
logger.info(f"[IMAGE PROXY] Successfully downloaded {len(image_bytes)} bytes, content-type: {content_type}")
|
| 245 |
+
|
| 246 |
# Add cache headers for better performance
|
| 247 |
response = Response(image_bytes, content_type=content_type)
|
| 248 |
response.headers['Cache-Control'] = 'public, max-age=3600' # Cache for 1 hour
|
| 249 |
return response
|
| 250 |
|
| 251 |
except IndexError as e:
|
| 252 |
+
logger.error(f"[IMAGE PROXY] URL parsing IndexError: {e}")
|
| 253 |
+
logger.error(f"[IMAGE PROXY] URL was: {image_url}")
|
| 254 |
return jsonify({'error': 'Invalid URL format.'}), 400
|
| 255 |
except Exception as e:
|
| 256 |
# This will catch parsing errors or other unexpected issues.
|
| 257 |
+
logger.error(f"[IMAGE PROXY] Unexpected error: {e}")
|
| 258 |
+
logger.error(f"[IMAGE PROXY] Error type: {type(e).__name__}")
|
| 259 |
+
logger.error(f"[IMAGE PROXY] URL was: {image_url}")
|
| 260 |
+
import traceback
|
| 261 |
+
logger.error(f"[IMAGE PROXY] Full traceback: {traceback.format_exc()}")
|
| 262 |
return jsonify({'error': 'Internal server error processing the image request.'}), 500
|
| 263 |
|
| 264 |
# -----------------------------------------------------------------------------
|