Spaces:
Sleeping
Sleeping
Roei Zavida
commited on
Enhance error handling in generate_image function to provide more specific feedback for API authentication and network errors.
Browse files
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 |
-
|
| 68 |
-
|
| 69 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 70 |
except Exception as e:
|
| 71 |
-
|
|
|
|
|
|
|
| 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 = """
|