mgbam commited on
Commit
97c7cc1
Β·
verified Β·
1 Parent(s): 9728f29

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +23 -11
app.py CHANGED
@@ -41,7 +41,7 @@ from vertexai.preview.vision_models import ImageGenerationModel
41
 
42
  # --- Logging Setup ---
43
  logging.basicConfig(
44
- level=logging.INFO,
45
  format='%(asctime)s - %(levelname)s - %(message)s'
46
  )
47
  logger = logging.getLogger(__name__)
@@ -58,7 +58,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
- # IMAGE_MODEL_ID is now used with the preview API
62
  IMAGE_MODEL_ID = "imagen-3.0-generate-002"
63
  DEFAULT_ASPECT_RATIO = "1:1"
64
  VIDEO_FPS = 24
@@ -79,14 +79,14 @@ except KeyError:
79
  st.error("🚨 **Google API Key Not Found!** Please configure it.", icon="🚨")
80
  st.stop()
81
 
82
- # For Vertex AI, we also need PROJECT_ID and LOCATION.
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
- # Initialize Vertex AI (used for image generation)
90
  vertexai.init(project=PROJECT_ID, location=LOCATION)
91
 
92
  # --- Initialize Google Clients for text/audio ---
@@ -117,7 +117,7 @@ class StorySegment(BaseModel):
117
  @field_validator('image_prompt')
118
  @classmethod
119
  def image_prompt_no_humans(cls, v: str) -> str:
120
- if any(word in v.lower() for word in ["person", "people", "human", "man", "woman", "boy", "girl", "child"]):
121
  logger.warning(f"Prompt '{v[:50]}...' may contain humans.")
122
  return v
123
 
@@ -265,13 +265,13 @@ 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
- It calls the ImageGenerationModel from vertexai.preview.vision_models with the pretrained model "imagen-3.0-generate-002" and returns a PIL Image.
 
269
  """
270
  logger.info(f"πŸ–ΌοΈ [{task_id}] Requesting image: '{prompt[:70]}...' (Aspect: {aspect_ratio})")
271
  try:
272
- # Load the pretrained Imagen model
273
  generation_model = ImageGenerationModel.from_pretrained(IMAGE_MODEL_ID)
274
- # Generate the image (here we generate one image)
275
  images = generation_model.generate_images(
276
  prompt=prompt,
277
  number_of_images=1,
@@ -281,13 +281,25 @@ def generate_image_imagen(prompt: str, aspect_ratio: str = "1:1", task_id: str =
281
  safety_filter_level="",
282
  add_watermark=True,
283
  )
284
- # Return the generated PIL image (using the internal _pil_image attribute)
285
  image = images[0]._pil_image
286
  logger.info(f"βœ… [{task_id}] Image generated successfully.")
287
  return image
288
  except Exception as e:
289
- logger.exception(f"❌ [{task_id}] Image generation failed: {e}")
290
- st.error(f"Image generation for {task_id} failed: {e}", icon="πŸ–ΌοΈ")
 
 
 
 
 
 
 
 
 
 
 
 
 
291
  return None
292
 
293
  # --- Streamlit UI Elements ---
 
41
 
42
  # --- Logging Setup ---
43
  logging.basicConfig(
44
+ level=logging.INFO,
45
  format='%(asctime)s - %(levelname)s - %(message)s'
46
  )
47
  logger = logging.getLogger(__name__)
 
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 used with Vertex AI preview
62
  IMAGE_MODEL_ID = "imagen-3.0-generate-002"
63
  DEFAULT_ASPECT_RATIO = "1:1"
64
  VIDEO_FPS = 24
 
79
  st.error("🚨 **Google API Key Not Found!** Please configure it.", icon="🚨")
80
  st.stop()
81
 
82
+ # Vertex AI configuration: Get project and location
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
+ # Initialize Vertex AI
90
  vertexai.init(project=PROJECT_ID, location=LOCATION)
91
 
92
  # --- Initialize Google Clients for text/audio ---
 
117
  @field_validator('image_prompt')
118
  @classmethod
119
  def image_prompt_no_humans(cls, v: str) -> str:
120
+ if any(w in v.lower() for w in ["person", "people", "human", "man", "woman", "boy", "girl", "child"]):
121
  logger.warning(f"Prompt '{v[:50]}...' may contain humans.")
122
  return v
123
 
 
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,
277
  number_of_images=1,
 
281
  safety_filter_level="",
282
  add_watermark=True,
283
  )
 
284
  image = images[0]._pil_image
285
  logger.info(f"βœ… [{task_id}] Image generated successfully.")
286
  return image
287
  except Exception as e:
288
+ error_str = str(e)
289
+ if "Unable to authenticate" in error_str:
290
+ error_msg = (
291
+ "Authentication error: Unable to authenticate your request. "
292
+ "If running locally, please run `!gcloud auth login`. "
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}"
301
+ logger.exception(f"❌ [{task_id}] {error_msg}")
302
+ st.error(error_msg, icon="πŸ–ΌοΈ")
303
  return None
304
 
305
  # --- Streamlit UI Elements ---