Update app.py
Browse files
app.py
CHANGED
@@ -1,4 +1,4 @@
|
|
1 |
-
# app.py
|
2 |
|
3 |
import os
|
4 |
import gradio as gr
|
@@ -11,10 +11,12 @@ import google.auth.transport.requests
|
|
11 |
from huggingface_hub import login
|
12 |
|
13 |
# --- 1. Configuration and Authentication ---
|
|
|
14 |
GCP_PROJECT_ID = os.environ.get("GCP_PROJECT_ID")
|
15 |
GCP_LOCATION = os.environ.get("GCP_LOCATION")
|
16 |
|
17 |
# --- Authentication and Sanity Checks Block ---
|
|
|
18 |
hf_token = os.environ.get("HF_TOKEN")
|
19 |
if hf_token:
|
20 |
print("Hugging Face token found. Logging in.")
|
@@ -25,7 +27,11 @@ else:
|
|
25 |
creds_json_str = os.environ.get("GOOGLE_APPLICATION_CREDENTIALS_JSON")
|
26 |
|
27 |
if not all([GCP_PROJECT_ID, GCP_LOCATION, creds_json_str]):
|
28 |
-
missing_secrets = [s for s, v in {
|
|
|
|
|
|
|
|
|
29 |
error_message = f"FATAL: Missing required secrets: {', '.join(missing_secrets)}."
|
30 |
print(error_message)
|
31 |
def generate_video(prompt):
|
@@ -68,6 +74,7 @@ else:
|
|
68 |
poll_result = poll_response.json()
|
69 |
if poll_result.get("done"):
|
70 |
print("Job finished.")
|
|
|
71 |
response_data = poll_result.get("response", {})
|
72 |
if "videos" in response_data and response_data["videos"]:
|
73 |
video_base64 = response_data["videos"][0]["bytesBase64Encoded"]
|
@@ -76,23 +83,21 @@ else:
|
|
76 |
yield "Status: Done!", "generated_video.mp4"
|
77 |
return
|
78 |
else:
|
|
|
79 |
error_message = "Video generation failed."
|
|
|
80 |
if "error" in poll_result:
|
81 |
error_details = poll_result["error"].get("message", "No details provided.")
|
82 |
-
|
83 |
-
|
84 |
-
error_message += "\nReason: The Google API reported a temporary internal error. Please wait and try again."
|
85 |
-
elif "Deadline exceeded" in error_details:
|
86 |
-
error_message += "\nReason: The job took too long on Google's servers and timed out. This can happen during peak load. Please try again."
|
87 |
-
else:
|
88 |
-
error_message += f"\nAPI Error: {error_details}"
|
89 |
-
# <<< END: FINAL UX IMPROVEMENT >>>
|
90 |
elif "raiResult" in response_data:
|
91 |
rai_reason = response_data.get("raiMediaFilteredReason", "Unknown reason.")
|
92 |
error_message += f"\nReason: Content was blocked by safety filters ({rai_reason})."
|
93 |
else:
|
94 |
error_message += "\nReason: The API did not return a video or a specific error."
|
|
|
95 |
raise gr.Error(error_message)
|
|
|
96 |
time.sleep(10)
|
97 |
raise gr.Error("Operation timed out.")
|
98 |
except Exception as e:
|
|
|
1 |
+
# app.py
|
2 |
|
3 |
import os
|
4 |
import gradio as gr
|
|
|
11 |
from huggingface_hub import login
|
12 |
|
13 |
# --- 1. Configuration and Authentication ---
|
14 |
+
|
15 |
GCP_PROJECT_ID = os.environ.get("GCP_PROJECT_ID")
|
16 |
GCP_LOCATION = os.environ.get("GCP_LOCATION")
|
17 |
|
18 |
# --- Authentication and Sanity Checks Block ---
|
19 |
+
|
20 |
hf_token = os.environ.get("HF_TOKEN")
|
21 |
if hf_token:
|
22 |
print("Hugging Face token found. Logging in.")
|
|
|
27 |
creds_json_str = os.environ.get("GOOGLE_APPLICATION_CREDENTIALS_JSON")
|
28 |
|
29 |
if not all([GCP_PROJECT_ID, GCP_LOCATION, creds_json_str]):
|
30 |
+
missing_secrets = [s for s, v in {
|
31 |
+
"GCP_PROJECT_ID": GCP_PROJECT_ID,
|
32 |
+
"GCP_LOCATION": GCP_LOCATION,
|
33 |
+
"GOOGLE_APPLICATION_CREDENTIALS_JSON": creds_json_str
|
34 |
+
}.items() if not v]
|
35 |
error_message = f"FATAL: Missing required secrets: {', '.join(missing_secrets)}."
|
36 |
print(error_message)
|
37 |
def generate_video(prompt):
|
|
|
74 |
poll_result = poll_response.json()
|
75 |
if poll_result.get("done"):
|
76 |
print("Job finished.")
|
77 |
+
print(f"Full response payload: {json.dumps(poll_result, indent=2)}") # For debugging
|
78 |
response_data = poll_result.get("response", {})
|
79 |
if "videos" in response_data and response_data["videos"]:
|
80 |
video_base64 = response_data["videos"][0]["bytesBase64Encoded"]
|
|
|
83 |
yield "Status: Done!", "generated_video.mp4"
|
84 |
return
|
85 |
else:
|
86 |
+
# <<< START: IMPROVED ERROR HANDLING >>>
|
87 |
error_message = "Video generation failed."
|
88 |
+
# Check for a specific error message in the operation response
|
89 |
if "error" in poll_result:
|
90 |
error_details = poll_result["error"].get("message", "No details provided.")
|
91 |
+
error_message += f"\nAPI Error: {error_details}"
|
92 |
+
# Check for a specific RAI reason
|
|
|
|
|
|
|
|
|
|
|
|
|
93 |
elif "raiResult" in response_data:
|
94 |
rai_reason = response_data.get("raiMediaFilteredReason", "Unknown reason.")
|
95 |
error_message += f"\nReason: Content was blocked by safety filters ({rai_reason})."
|
96 |
else:
|
97 |
error_message += "\nReason: The API did not return a video or a specific error."
|
98 |
+
|
99 |
raise gr.Error(error_message)
|
100 |
+
# <<< END: IMPROVED ERROR HANDLING >>>
|
101 |
time.sleep(10)
|
102 |
raise gr.Error("Operation timed out.")
|
103 |
except Exception as e:
|