Deadmon commited on
Commit
bc7d519
·
verified ·
1 Parent(s): df8e9d1

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +31 -21
app.py CHANGED
@@ -13,7 +13,7 @@ from huggingface_hub import login
13
  # --- 1. Configuration and Authentication ---
14
 
15
  # IMPORTANT: Replace with your Google Cloud Project ID
16
- GCP_PROJECT_ID = "gen-lang-client-0193353123"
17
  GCP_LOCATION = "us-central1"
18
  MODEL_ID = "veo-3.0-generate-preview"
19
  API_ENDPOINT = f"{GCP_LOCATION}-aiplatform.googleapis.com"
@@ -23,8 +23,7 @@ FETCH_URL = f"https://{API_ENDPOINT}/v1/projects/{GCP_PROJECT_ID}/locations/{GCP
23
 
24
  # --- Authentication Block ---
25
 
26
- # Part A: Hugging Face Hub Authentication (NEW)
27
- # This section looks for a secret named 'HF_TOKEN' to log into the Hub.
28
  hf_token = os.environ.get("HF_TOKEN")
29
  if hf_token:
30
  print("Hugging Face token found. Logging in.")
@@ -32,13 +31,10 @@ if hf_token:
32
  else:
33
  print("WARNING: Hugging Face token ('HF_TOKEN') not found. Hub-related features may be disabled.")
34
 
35
-
36
- # Part B: Google Cloud Authentication (Unchanged)
37
- # This section expects a secret named 'GOOGLE_APPLICATION_CREDENTIALS_JSON'
38
  creds_json_str = os.environ.get("GOOGLE_APPLICATION_CREDENTIALS_JSON")
39
  if not creds_json_str:
40
  print("FATAL: 'GOOGLE_APPLICATION_CREDENTIALS_JSON' secret not found. App cannot authenticate with Google Cloud.")
41
- # Define a dummy function to show an error in the UI
42
  def generate_video(prompt):
43
  raise gr.Error("Authentication failed. Server is missing Google Cloud credentials. Please check the Hugging Face Space secrets.")
44
  else:
@@ -51,17 +47,13 @@ else:
51
 
52
 
53
  def get_access_token():
54
- """Generates a fresh short-lived access token for Google Cloud."""
55
  auth_req = google.auth.transport.requests.Request()
56
  credentials.refresh(auth_req)
57
  return credentials.token
58
 
59
- # --- 2. Core Video Generation Logic (Unchanged) ---
60
 
61
  def generate_video(prompt: str):
62
- """
63
- The main function to generate a video. It submits, polls, and returns the result.
64
- """
65
  if not prompt:
66
  raise gr.Error("Prompt cannot be empty.")
67
 
@@ -111,15 +103,32 @@ else:
111
 
112
  if poll_result.get("done"):
113
  print("Job finished successfully.")
114
- video_base64 = poll_result["response"]["predictions"][0]["bytesBase64Encoded"]
115
- video_bytes = base64.b64decode(video_base64)
116
 
117
- temp_video_path = "generated_video.mp4"
118
- with open(temp_video_path, "wb") as f:
119
- f.write(video_bytes)
 
120
 
121
- yield "Status: Done!", temp_video_path
122
- return
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
123
 
124
  time.sleep(10)
125
 
@@ -129,8 +138,9 @@ else:
129
  print(f"HTTP Error: {e.response.text}")
130
  raise gr.Error(f"API Error: {e.response.status_code}. Details: {e.response.text}")
131
  except Exception as e:
132
- print(f"An unexpected error occurred: {e}")
133
- raise gr.Error(f"An unexpected error occurred: {str(e)}")
 
134
 
135
 
136
  # --- 3. Gradio User Interface (Unchanged) ---
 
13
  # --- 1. Configuration and Authentication ---
14
 
15
  # IMPORTANT: Replace with your Google Cloud Project ID
16
+ GCP_PROJECT_ID = "YOUR_GCP_PROJECT_ID" # Make sure this is replaced!
17
  GCP_LOCATION = "us-central1"
18
  MODEL_ID = "veo-3.0-generate-preview"
19
  API_ENDPOINT = f"{GCP_LOCATION}-aiplatform.googleapis.com"
 
23
 
24
  # --- Authentication Block ---
25
 
26
+ # Part A: Hugging Face Hub Authentication
 
27
  hf_token = os.environ.get("HF_TOKEN")
28
  if hf_token:
29
  print("Hugging Face token found. Logging in.")
 
31
  else:
32
  print("WARNING: Hugging Face token ('HF_TOKEN') not found. Hub-related features may be disabled.")
33
 
34
+ # Part B: Google Cloud Authentication
 
 
35
  creds_json_str = os.environ.get("GOOGLE_APPLICATION_CREDENTIALS_JSON")
36
  if not creds_json_str:
37
  print("FATAL: 'GOOGLE_APPLICATION_CREDENTIALS_JSON' secret not found. App cannot authenticate with Google Cloud.")
 
38
  def generate_video(prompt):
39
  raise gr.Error("Authentication failed. Server is missing Google Cloud credentials. Please check the Hugging Face Space secrets.")
40
  else:
 
47
 
48
 
49
  def get_access_token():
 
50
  auth_req = google.auth.transport.requests.Request()
51
  credentials.refresh(auth_req)
52
  return credentials.token
53
 
54
+ # --- 2. Core Video Generation Logic (WITH FIX) ---
55
 
56
  def generate_video(prompt: str):
 
 
 
57
  if not prompt:
58
  raise gr.Error("Prompt cannot be empty.")
59
 
 
103
 
104
  if poll_result.get("done"):
105
  print("Job finished successfully.")
106
+ # --- START: ROBUST RESPONSE HANDLING FIX ---
 
107
 
108
+ # For debugging, print the entire final response from Google
109
+ print(f"Full successful response payload: {json.dumps(poll_result, indent=2)}")
110
+
111
+ response_data = poll_result.get("response", {})
112
 
113
+ # Check if the 'predictions' key exists and is not empty
114
+ if "predictions" in response_data and response_data["predictions"]:
115
+ video_base64 = response_data["predictions"][0]["bytesBase64Encoded"]
116
+ video_bytes = base64.b64decode(video_base64)
117
+
118
+ temp_video_path = "generated_video.mp4"
119
+ with open(temp_video_path, "wb") as f:
120
+ f.write(video_bytes)
121
+
122
+ yield "Status: Done! Video generated successfully.", temp_video_path
123
+ return
124
+ else:
125
+ # This branch handles cases where the job is "done" but no video was returned,
126
+ # most likely due to safety filters.
127
+ error_message = "Video generation finished, but the content was blocked by safety filters or another issue prevented video creation."
128
+ print(f"ERROR: {error_message}")
129
+ raise gr.Error(error_message)
130
+
131
+ # --- END: ROBUST RESPONSE HANDLING FIX ---
132
 
133
  time.sleep(10)
134
 
 
138
  print(f"HTTP Error: {e.response.text}")
139
  raise gr.Error(f"API Error: {e.response.status_code}. Details: {e.response.text}")
140
  except Exception as e:
141
+ # Catch the specific Gradio error we raised, and any other unexpected errors.
142
+ print(f"An error occurred in the generation process: {e}")
143
+ raise gr.Error(str(e))
144
 
145
 
146
  # --- 3. Gradio User Interface (Unchanged) ---