Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -31,13 +31,14 @@ from moviepy.editor import ImageClip, AudioFileClip, concatenate_videoclips
|
|
31 |
# Type hints
|
32 |
import typing_extensions as typing
|
33 |
|
34 |
-
# Async support
|
35 |
import nest_asyncio
|
36 |
nest_asyncio.apply()
|
37 |
|
38 |
# Import Vertex AI SDK for image generation (Preview API)
|
39 |
import vertexai
|
40 |
from vertexai.preview.vision_models import ImageGenerationModel
|
|
|
41 |
|
42 |
# --- Logging Setup ---
|
43 |
logging.basicConfig(
|
@@ -58,7 +59,7 @@ Generate multiple, branching story timelines from a single theme using AI, compl
|
|
58 |
TEXT_MODEL_ID = "models/gemini-1.5-flash"
|
59 |
AUDIO_MODEL_ID = "models/gemini-1.5-flash"
|
60 |
AUDIO_SAMPLING_RATE = 24000
|
61 |
-
# Pretrained Imagen model
|
62 |
IMAGE_MODEL_ID = "imagen-3.0-generate-002"
|
63 |
DEFAULT_ASPECT_RATIO = "1:1"
|
64 |
VIDEO_FPS = 24
|
@@ -79,15 +80,24 @@ except KeyError:
|
|
79 |
st.error("π¨ **Google API Key Not Found!** Please configure it.", icon="π¨")
|
80 |
st.stop()
|
81 |
|
82 |
-
# Vertex AI configuration:
|
83 |
PROJECT_ID = st.secrets.get("PROJECT_ID") or os.environ.get("PROJECT_ID")
|
84 |
LOCATION = st.secrets.get("LOCATION") or os.environ.get("LOCATION", "us-central1")
|
85 |
if not PROJECT_ID:
|
86 |
st.error("π¨ **PROJECT_ID not set!** Please add PROJECT_ID to your secrets.", icon="π¨")
|
87 |
st.stop()
|
88 |
|
89 |
-
#
|
90 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
91 |
|
92 |
# --- Initialize Google Clients for text/audio ---
|
93 |
try:
|
@@ -106,7 +116,7 @@ except Exception as e:
|
|
106 |
st.error(f"π¨ Failed Init: {e}", icon="π¨")
|
107 |
st.stop()
|
108 |
|
109 |
-
# --- Define Pydantic Schemas
|
110 |
class StorySegment(BaseModel):
|
111 |
scene_id: int = Field(..., ge=0)
|
112 |
image_prompt: str = Field(..., min_length=10, max_length=250)
|
@@ -140,7 +150,6 @@ class ChronoWeaveResponse(BaseModel):
|
|
140 |
return self
|
141 |
|
142 |
# --- Helper Functions ---
|
143 |
-
|
144 |
@contextlib.contextmanager
|
145 |
def wave_file_writer(filename: str, channels: int = 1, rate: int = AUDIO_SAMPLING_RATE, sample_width: int = 2):
|
146 |
"""Context manager to safely write WAV files."""
|
@@ -265,12 +274,11 @@ def generate_image_imagen(prompt: str, aspect_ratio: str = "1:1", task_id: str =
|
|
265 |
"""
|
266 |
Generates an image using Vertex AI's Imagen model via the Vertex AI preview API.
|
267 |
|
268 |
-
This function loads the pretrained model "imagen-3.0-generate-002" and attempts to generate an image.
|
269 |
If authentication fails, it provides guidance on how to resolve the issue.
|
270 |
"""
|
271 |
logger.info(f"πΌοΈ [{task_id}] Requesting image: '{prompt[:70]}...' (Aspect: {aspect_ratio})")
|
272 |
try:
|
273 |
-
# Load the pretrained Imagen model from Vertex AI.
|
274 |
generation_model = ImageGenerationModel.from_pretrained(IMAGE_MODEL_ID)
|
275 |
images = generation_model.generate_images(
|
276 |
prompt=prompt,
|
@@ -293,8 +301,7 @@ def generate_image_imagen(prompt: str, aspect_ratio: str = "1:1", task_id: str =
|
|
293 |
"If running in Colab, try:\n"
|
294 |
" from google.colab import auth\n"
|
295 |
" auth.authenticate_user()\n"
|
296 |
-
"If using a service account or other environment, please refer to "
|
297 |
-
"https://cloud.google.com/docs/authentication for guidance."
|
298 |
)
|
299 |
else:
|
300 |
error_msg = f"Image generation for {task_id} failed: {e}"
|
@@ -560,7 +567,7 @@ if generate_button:
|
|
560 |
logger.info(f"β
Temp dir removed: {temp_dir}")
|
561 |
st.success("β
Temp files cleaned.")
|
562 |
except Exception as e:
|
563 |
-
logger.error(f"β οΈ Failed remove temp dir {temp_dir}: {e}")
|
564 |
st.warning(f"Could not remove temp files: {temp_dir}.", icon="β οΈ")
|
565 |
|
566 |
elif not chrono_response:
|
|
|
31 |
# Type hints
|
32 |
import typing_extensions as typing
|
33 |
|
34 |
+
# Async support
|
35 |
import nest_asyncio
|
36 |
nest_asyncio.apply()
|
37 |
|
38 |
# Import Vertex AI SDK for image generation (Preview API)
|
39 |
import vertexai
|
40 |
from vertexai.preview.vision_models import ImageGenerationModel
|
41 |
+
from google.oauth2 import service_account
|
42 |
|
43 |
# --- Logging Setup ---
|
44 |
logging.basicConfig(
|
|
|
59 |
TEXT_MODEL_ID = "models/gemini-1.5-flash"
|
60 |
AUDIO_MODEL_ID = "models/gemini-1.5-flash"
|
61 |
AUDIO_SAMPLING_RATE = 24000
|
62 |
+
# Pretrained Imagen model identifier for Vertex AI preview
|
63 |
IMAGE_MODEL_ID = "imagen-3.0-generate-002"
|
64 |
DEFAULT_ASPECT_RATIO = "1:1"
|
65 |
VIDEO_FPS = 24
|
|
|
80 |
st.error("π¨ **Google API Key Not Found!** Please configure it.", icon="π¨")
|
81 |
st.stop()
|
82 |
|
83 |
+
# Vertex AI configuration: load PROJECT_ID and LOCATION from secrets or environment.
|
84 |
PROJECT_ID = st.secrets.get("PROJECT_ID") or os.environ.get("PROJECT_ID")
|
85 |
LOCATION = st.secrets.get("LOCATION") or os.environ.get("LOCATION", "us-central1")
|
86 |
if not PROJECT_ID:
|
87 |
st.error("π¨ **PROJECT_ID not set!** Please add PROJECT_ID to your secrets.", icon="π¨")
|
88 |
st.stop()
|
89 |
|
90 |
+
# Load service account JSON from environment (secret name: SERVICE_ACCOUNT_JSON)
|
91 |
+
try:
|
92 |
+
service_account_info = json.loads(os.environ["SERVICE_ACCOUNT_JSON"])
|
93 |
+
credentials = service_account.Credentials.from_service_account_info(service_account_info)
|
94 |
+
logger.info("Service account credentials loaded successfully.")
|
95 |
+
except Exception as e:
|
96 |
+
st.error(f"π¨ Failed to load service account JSON: {e}", icon="π¨")
|
97 |
+
st.stop()
|
98 |
+
|
99 |
+
# Initialize Vertex AI with credentials
|
100 |
+
vertexai.init(project=PROJECT_ID, location=LOCATION, credentials=credentials)
|
101 |
|
102 |
# --- Initialize Google Clients for text/audio ---
|
103 |
try:
|
|
|
116 |
st.error(f"π¨ Failed Init: {e}", icon="π¨")
|
117 |
st.stop()
|
118 |
|
119 |
+
# --- Define Pydantic Schemas ---
|
120 |
class StorySegment(BaseModel):
|
121 |
scene_id: int = Field(..., ge=0)
|
122 |
image_prompt: str = Field(..., min_length=10, max_length=250)
|
|
|
150 |
return self
|
151 |
|
152 |
# --- Helper Functions ---
|
|
|
153 |
@contextlib.contextmanager
|
154 |
def wave_file_writer(filename: str, channels: int = 1, rate: int = AUDIO_SAMPLING_RATE, sample_width: int = 2):
|
155 |
"""Context manager to safely write WAV files."""
|
|
|
274 |
"""
|
275 |
Generates an image using Vertex AI's Imagen model via the Vertex AI preview API.
|
276 |
|
277 |
+
This function loads the pretrained Imagen model "imagen-3.0-generate-002" and attempts to generate an image.
|
278 |
If authentication fails, it provides guidance on how to resolve the issue.
|
279 |
"""
|
280 |
logger.info(f"πΌοΈ [{task_id}] Requesting image: '{prompt[:70]}...' (Aspect: {aspect_ratio})")
|
281 |
try:
|
|
|
282 |
generation_model = ImageGenerationModel.from_pretrained(IMAGE_MODEL_ID)
|
283 |
images = generation_model.generate_images(
|
284 |
prompt=prompt,
|
|
|
301 |
"If running in Colab, try:\n"
|
302 |
" from google.colab import auth\n"
|
303 |
" auth.authenticate_user()\n"
|
304 |
+
"If using a service account or other environment, please refer to https://cloud.google.com/docs/authentication for guidance."
|
|
|
305 |
)
|
306 |
else:
|
307 |
error_msg = f"Image generation for {task_id} failed: {e}"
|
|
|
567 |
logger.info(f"β
Temp dir removed: {temp_dir}")
|
568 |
st.success("β
Temp files cleaned.")
|
569 |
except Exception as e:
|
570 |
+
logger.error(f"β οΈ Failed to remove temp dir {temp_dir}: {e}")
|
571 |
st.warning(f"Could not remove temp files: {temp_dir}.", icon="β οΈ")
|
572 |
|
573 |
elif not chrono_response:
|