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

Update main.py

Browse files
Files changed (1) hide show
  1. main.py +25 -19
main.py CHANGED
@@ -176,11 +176,8 @@ def send_text_request(model_name, prompt, image):
176
  def image_proxy():
177
  """
178
  [NO AUTH - OPEN PROXY] Fetches an image from a given URL and returns it.
179
- This acts as a proxy to get around client-side CORS issues for PDF generation.
180
- The image URL is passed as a query parameter.
181
-
182
- WARNING: This endpoint is an open proxy and is not secure. Anyone can use it
183
- to route traffic through your server. Use with caution.
184
  """
185
  image_url = request.args.get('url')
186
  if not image_url:
@@ -188,29 +185,38 @@ def image_proxy():
188
 
189
  try:
190
  # Use a session object for better performance and connection management.
191
- # Set a timeout to prevent requests from hanging indefinitely.
192
  with requests.Session() as s:
193
- response = s.get(image_url, stream=True, timeout=15)
194
- # Raise an exception for bad status codes (e.g., 404 Not Found, 500 Server Error).
 
 
 
 
195
  response.raise_for_status()
196
 
197
- # Get the content type from the original image response to pass it along to the client.
198
  content_type = response.headers.get('Content-Type', 'application/octet-stream')
199
 
200
- # Stream the response back to the client. This is memory-efficient as it doesn't
201
- # load the entire image into memory at once.
202
  return Response(response.iter_content(chunk_size=1024), content_type=content_type)
203
 
204
- except requests.exceptions.Timeout:
205
- return jsonify({'error': 'The request to the external image URL timed out.'}), 504 # 504 Gateway Timeout
206
  except requests.exceptions.RequestException as e:
207
- # This catches other network-related errors (e.g., DNS failure, connection error).
208
- print(f"Error fetching image from URL '{image_url}': {e}")
209
- 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 # 502 Bad Gateway
 
 
 
 
 
210
  except Exception as e:
211
- # Catch any other unexpected errors.
212
- print(f"An unexpected error occurred while proxying the image '{image_url}': {e}")
213
- return jsonify({'error': 'An internal server error occurred while trying to fetch the image.'}), 500
 
 
 
 
214
 
215
  # -----------------------------------------------------------------------------
216
  # 3. AUTHENTICATION & USER MANAGEMENT
 
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:
 
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