rairo commited on
Commit
e353eec
·
verified ·
1 Parent(s): c3361ef

Update main.py

Browse files
Files changed (1) hide show
  1. main.py +16 -35
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': 'The "url" query parameter is required.'}), 400
185
 
186
  try:
187
- # Use a session object for better performance and connection management.
188
- with requests.Session() as s:
189
- # The key change is adding `verify=False`. This is often necessary
190
- # in environments like Hugging Face Spaces or behind corporate proxies
191
- # that can interfere with SSL certificate verification.
192
- response = s.get(image_url, stream=True, timeout=15, verify=False)
193
-
194
- # Raise an exception for bad status codes (e.g., 404 Not Found).
195
- response.raise_for_status()
196
 
197
- # Get the content type from the original image response to pass it along.
198
- content_type = response.headers.get('Content-Type', 'application/octet-stream')
199
 
200
- # Stream the response back to the client.
201
- return Response(response.iter_content(chunk_size=1024), content_type=content_type)
 
 
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 any other unexpected errors in the function.
214
- print("="*20, "UNEXPECTED PROXY ERROR", "="*20)
215
- print(f"An unexpected error occurred while proxying the image: {image_url}")
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