Roei Zavida commited on
Commit
8bf40f3
·
unverified ·
1 Parent(s): 338be10

Enhance error handling in generate_image function to provide more specific feedback for API authentication and network errors.

Browse files
Files changed (1) hide show
  1. app.py +12 -4
app.py CHANGED
@@ -63,12 +63,20 @@ def generate_image(
63
  else:
64
  raise gr.Error("No image data received from the API")
65
 
 
 
66
  except openai.APIError as e:
67
- raise gr.Error(f"OpenAI API error: {str(e)}")
68
- except requests.RequestException as e:
69
- raise gr.Error(f"Network error: {str(e)}")
 
 
 
 
70
  except Exception as e:
71
- raise gr.Error(f"An unexpected error occurred: {str(e)}")
 
 
72
 
73
 
74
  css = """
 
63
  else:
64
  raise gr.Error("No image data received from the API")
65
 
66
+ except openai.AuthenticationError:
67
+ raise gr.Error("Invalid API key. Please check your OpenAI API key and try again.")
68
  except openai.APIError as e:
69
+ error_msg = str(e)
70
+ if "api_key" in error_msg.lower():
71
+ raise gr.Error("API authentication error. Please check your credentials.")
72
+ else:
73
+ raise gr.Error("OpenAI API error occurred. Please try again.")
74
+ except requests.RequestException:
75
+ raise gr.Error("Network error occurred while downloading the image. Please try again.")
76
  except Exception as e:
77
+ # Log the full error internally but show a generic message to the user
78
+ print(f"Internal error: {type(e).__name__}") # Log error type without sensitive data
79
+ raise gr.Error("An unexpected error occurred. Please try again.")
80
 
81
 
82
  css = """