Refactor: Direct SDK call for image generation and cleanup
Browse files- src/api_clients.py +0 -71
- src/automation.py +1 -1
src/api_clients.py
CHANGED
|
@@ -27,8 +27,6 @@ from file_downloader import FileDownloader
|
|
| 27 |
|
| 28 |
|
| 29 |
# --- NEW IMPORTS ---
|
| 30 |
-
from google.oauth2 import service_account
|
| 31 |
-
import vertexai
|
| 32 |
from google_src.gcs_utils import get_gcs_client, get_gcs_credentials, upload_file_to_gcs, list_gcs_files, create_bucket_if_not_exists
|
| 33 |
from google_src.setup_gcs_permissions import setup_bucket_permissions
|
| 34 |
|
|
@@ -101,75 +99,6 @@ class APIClients:
|
|
| 101 |
await self.store_in_cache(file_path, f"{method_type}_{duration}", ".txt")
|
| 102 |
except: pass
|
| 103 |
|
| 104 |
-
async def generate_image(self, prompt: str) -> Optional[str]:
|
| 105 |
-
"""
|
| 106 |
-
Generate image using Vertex AI Imagen 4 Ultra
|
| 107 |
-
|
| 108 |
-
Args:
|
| 109 |
-
prompt: Image generation prompt
|
| 110 |
-
|
| 111 |
-
Returns:
|
| 112 |
-
Local path to generated image or None
|
| 113 |
-
"""
|
| 114 |
-
try:
|
| 115 |
-
if os.getenv("TEST_AUTOMATION", "").lower() == "true":
|
| 116 |
-
return f"{os.getenv('TEST_DATA_DIRECTORY')}/image.png"
|
| 117 |
-
|
| 118 |
-
url = await self.get_cache_url("generate_image", ".jpg")
|
| 119 |
-
if url:
|
| 120 |
-
return url
|
| 121 |
-
|
| 122 |
-
logger.info(f"🎨 Generating image with Imagen 4 Ultra: {prompt[:200]}...")
|
| 123 |
-
image_path = ai_studio_sdk.generate_image(prompt)
|
| 124 |
-
if image_path:
|
| 125 |
-
await self.store_in_cache(image_path, "generate_image", ".jpg")
|
| 126 |
-
return image_path
|
| 127 |
-
raise ValueError("Image not Generated.")
|
| 128 |
-
from vertexai.preview.vision_models import ImageGenerationModel
|
| 129 |
-
|
| 130 |
-
vertex_ai_credentials = None
|
| 131 |
-
vertex_ai_project_id = self.config.get("gcp_project_id") # Fallback to main config
|
| 132 |
-
|
| 133 |
-
# Check for the specific Vertex AI credentials in the environment variable
|
| 134 |
-
vertex_creds_json_str = os.getenv("VERTEX_AI_CREDENTIALS_JSON")
|
| 135 |
-
if vertex_creds_json_str:
|
| 136 |
-
logger.info("Found specific VERTEX_AI_CREDENTIALS_JSON. Initializing Vertex AI with these credentials.")
|
| 137 |
-
try:
|
| 138 |
-
# Parse the JSON string from the environment variable
|
| 139 |
-
creds_info = json.loads(vertex_creds_json_str)
|
| 140 |
-
vertex_ai_credentials = service_account.Credentials.from_service_account_info(creds_info)
|
| 141 |
-
# Get the project ID directly from the credentials, which is more reliable
|
| 142 |
-
vertex_ai_project_id = creds_info.get("project_id")
|
| 143 |
-
except Exception as e:
|
| 144 |
-
logger.error(f"❌ Failed to parse VERTEX_AI_CREDENTIALS_JSON: {e}. Falling back to default credentials.")
|
| 145 |
-
else:
|
| 146 |
-
logger.info("VERTEX_AI_CREDENTIALS_JSON not found. Initializing Vertex AI with default (WIF) credentials.")
|
| 147 |
-
|
| 148 |
-
# Initialize Vertex AI with either the specific credentials or default (None)
|
| 149 |
-
vertexai.init(project=vertex_ai_project_id, location="us-central1", credentials=vertex_ai_credentials)
|
| 150 |
-
|
| 151 |
-
model = ImageGenerationModel.from_pretrained("imagen-4.0-ultra-generate-001")
|
| 152 |
-
images = model.generate_images(
|
| 153 |
-
prompt=prompt,
|
| 154 |
-
number_of_images=1,
|
| 155 |
-
aspect_ratio="9:16",
|
| 156 |
-
safety_filter_level="block_some",
|
| 157 |
-
person_generation="allow_adult",
|
| 158 |
-
)
|
| 159 |
-
if not images:
|
| 160 |
-
logger.error("❌ Imagen API returned no images.")
|
| 161 |
-
return None
|
| 162 |
-
|
| 163 |
-
# Save to temp file
|
| 164 |
-
import tempfile
|
| 165 |
-
output_path = f"/tmp/hook_image_{hash(prompt)}.png"
|
| 166 |
-
images[0].save(location=output_path, include_generation_parameters=False)
|
| 167 |
-
logger.info(f"✓ Image generated with Imagen 4 Ultra (9:16): {output_path}")
|
| 168 |
-
return output_path
|
| 169 |
-
|
| 170 |
-
except Exception as e:
|
| 171 |
-
logger.error(f"❌ Imagen 4 Ultra generation failed: {e}")
|
| 172 |
-
raise
|
| 173 |
|
| 174 |
async def generate_video(self, prompt: str, duration: int, image_input: str = None) -> Dict:
|
| 175 |
"""
|
|
|
|
| 27 |
|
| 28 |
|
| 29 |
# --- NEW IMPORTS ---
|
|
|
|
|
|
|
| 30 |
from google_src.gcs_utils import get_gcs_client, get_gcs_credentials, upload_file_to_gcs, list_gcs_files, create_bucket_if_not_exists
|
| 31 |
from google_src.setup_gcs_permissions import setup_bucket_permissions
|
| 32 |
|
|
|
|
| 99 |
await self.store_in_cache(file_path, f"{method_type}_{duration}", ".txt")
|
| 100 |
except: pass
|
| 101 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 102 |
|
| 103 |
async def generate_video(self, prompt: str, duration: int, image_input: str = None) -> Dict:
|
| 104 |
"""
|
src/automation.py
CHANGED
|
@@ -469,7 +469,7 @@ class ContentAutomation:
|
|
| 469 |
logger.info(f"Using provided captions: {captions[:50]}...")
|
| 470 |
|
| 471 |
# Step 1: Generate image using Imagen 4 Ultra
|
| 472 |
-
image_path =
|
| 473 |
if not image_path:
|
| 474 |
raise Exception("Image generation failed")
|
| 475 |
|
|
|
|
| 469 |
logger.info(f"Using provided captions: {captions[:50]}...")
|
| 470 |
|
| 471 |
# Step 1: Generate image using Imagen 4 Ultra
|
| 472 |
+
image_path = ai_studio_sdk.generate_image(strategy["gemini_prompt"])
|
| 473 |
if not image_path:
|
| 474 |
raise Exception("Image generation failed")
|
| 475 |
|