Spaces:
Running
Running
Update main.py
Browse files
main.py
CHANGED
|
@@ -174,49 +174,30 @@ def send_text_request(model_name, prompt, image):
|
|
| 174 |
# =============================================================================
|
| 175 |
@app.route('/api/image-proxy', methods=['GET'])
|
| 176 |
def image_proxy():
|
| 177 |
-
"""
|
| 178 |
-
[NO AUTH - OPEN PROXY] Fetches an image from a given URL and returns it.
|
| 179 |
-
This version includes better error handling and a workaround for potential
|
| 180 |
-
SSL verification issues in sandboxed server environments.
|
| 181 |
-
"""
|
| 182 |
image_url = request.args.get('url')
|
| 183 |
if not image_url:
|
| 184 |
-
return jsonify({'error': '
|
| 185 |
|
| 186 |
try:
|
| 187 |
-
|
| 188 |
-
|
| 189 |
-
|
| 190 |
-
|
| 191 |
-
|
| 192 |
-
|
| 193 |
-
|
| 194 |
-
# Raise an exception for bad status codes (e.g., 404 Not Found).
|
| 195 |
-
response.raise_for_status()
|
| 196 |
|
| 197 |
-
|
| 198 |
-
|
| 199 |
|
| 200 |
-
|
| 201 |
-
|
|
|
|
|
|
|
| 202 |
|
| 203 |
-
except requests.exceptions.RequestException as e:
|
| 204 |
-
# This will catch network-related errors.
|
| 205 |
-
print("="*20, "REQUESTS ERROR", "="*20)
|
| 206 |
-
print(f"Failed to fetch image from URL: {image_url}")
|
| 207 |
-
# The following line prints the full error, which is crucial for debugging.
|
| 208 |
-
print(traceback.format_exc())
|
| 209 |
-
print("="*50)
|
| 210 |
-
return jsonify({'error': f'Failed to fetch the image from the provided URL. The server may be down or the URL may be incorrect.'}), 502
|
| 211 |
-
|
| 212 |
except Exception as e:
|
| 213 |
-
# This will catch
|
| 214 |
-
print("
|
| 215 |
-
|
| 216 |
-
# Print the full traceback to the logs for detailed debugging.
|
| 217 |
-
print(traceback.format_exc())
|
| 218 |
-
print("="*60)
|
| 219 |
-
return jsonify({'error': 'An internal server error occurred while trying to fetch the image.'}), 50
|
| 220 |
|
| 221 |
# -----------------------------------------------------------------------------
|
| 222 |
# 3. AUTHENTICATION & USER MANAGEMENT
|
|
|
|
| 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 |
+
bucket_name = bucket.name
|
| 183 |
+
# Extract the file path from the full URL
|
| 184 |
+
# Example: https://.../bucket-name/users/file.png -> users/file.png
|
| 185 |
+
blob_path = image_url.split(f"{bucket_name}/")[1].split("?")[0]
|
| 186 |
+
|
| 187 |
+
blob = bucket.blob(blob_path)
|
|
|
|
|
|
|
|
|
|
| 188 |
|
| 189 |
+
if not blob.exists():
|
| 190 |
+
return jsonify({'error': 'Image not found.'}), 404
|
| 191 |
|
| 192 |
+
image_bytes = blob.download_as_bytes()
|
| 193 |
+
content_type = blob.content_type or 'application/octet-stream'
|
| 194 |
+
|
| 195 |
+
return Response(image_bytes, content_type=content_type)
|
| 196 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 197 |
except Exception as e:
|
| 198 |
+
# This will catch parsing errors or other unexpected issues.
|
| 199 |
+
print(f"Error in image proxy: {e}")
|
| 200 |
+
return jsonify({'error': 'Internal server error processing the image request.'}), 500
|
|
|
|
|
|
|
|
|
|
|
|
|
| 201 |
|
| 202 |
# -----------------------------------------------------------------------------
|
| 203 |
# 3. AUTHENTICATION & USER MANAGEMENT
|