Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -36,7 +36,7 @@ import typing_extensions as typing
|
|
| 36 |
import nest_asyncio
|
| 37 |
nest_asyncio.apply()
|
| 38 |
|
| 39 |
-
# Import Vertex AI SDK and Google
|
| 40 |
import vertexai
|
| 41 |
from vertexai.preview.vision_models import ImageGenerationModel
|
| 42 |
from google.oauth2 import service_account
|
|
@@ -51,7 +51,7 @@ logging.basicConfig(
|
|
| 51 |
)
|
| 52 |
logger = logging.getLogger(__name__)
|
| 53 |
|
| 54 |
-
# --- Configuration ---
|
| 55 |
st.set_page_config(page_title="ChronoWeave", layout="wide", initial_sidebar_state="expanded")
|
| 56 |
st.title("π ChronoWeave: Advanced Branching Narrative Generator")
|
| 57 |
st.markdown("""
|
|
@@ -63,8 +63,7 @@ Generate multiple, branching story timelines from a single theme using AI, compl
|
|
| 63 |
TEXT_MODEL_ID = "models/gemini-1.5-flash"
|
| 64 |
AUDIO_MODEL_ID = "models/gemini-1.5-flash"
|
| 65 |
AUDIO_SAMPLING_RATE = 24000
|
| 66 |
-
#
|
| 67 |
-
IMAGE_MODEL_ID = "imagen-3.0-generate-002"
|
| 68 |
DEFAULT_ASPECT_RATIO = "1:1"
|
| 69 |
VIDEO_FPS = 24
|
| 70 |
VIDEO_CODEC = "libx264"
|
|
@@ -72,7 +71,7 @@ AUDIO_CODEC = "aac"
|
|
| 72 |
TEMP_DIR_BASE = ".chrono_temp"
|
| 73 |
|
| 74 |
# --- Secrets and Environment Variables ---
|
| 75 |
-
|
| 76 |
try:
|
| 77 |
GOOGLE_API_KEY = st.secrets["GOOGLE_API_KEY"]
|
| 78 |
logger.info("Google API Key loaded from Streamlit secrets.")
|
|
@@ -84,25 +83,30 @@ except KeyError:
|
|
| 84 |
st.error("π¨ **Google API Key Not Found!** Please configure it.", icon="π¨")
|
| 85 |
st.stop()
|
| 86 |
|
|
|
|
| 87 |
PROJECT_ID = st.secrets.get("PROJECT_ID") or os.environ.get("PROJECT_ID")
|
| 88 |
LOCATION = st.secrets.get("LOCATION") or os.environ.get("LOCATION", "us-central1")
|
| 89 |
if not PROJECT_ID:
|
| 90 |
st.error("π¨ **PROJECT_ID not set!** Please add PROJECT_ID to your secrets.", icon="π¨")
|
| 91 |
st.stop()
|
| 92 |
|
| 93 |
-
# Load
|
|
|
|
|
|
|
|
|
|
|
|
|
| 94 |
try:
|
| 95 |
-
service_account_info = json.loads(
|
| 96 |
credentials = service_account.Credentials.from_service_account_info(service_account_info)
|
| 97 |
logger.info("Service account credentials loaded successfully.")
|
| 98 |
except Exception as e:
|
| 99 |
st.error(f"π¨ Failed to load service account JSON: {e}", icon="π¨")
|
| 100 |
st.stop()
|
| 101 |
|
| 102 |
-
# Initialize Vertex AI with credentials
|
| 103 |
vertexai.init(project=PROJECT_ID, location=LOCATION, credentials=credentials)
|
| 104 |
|
| 105 |
-
# --- Initialize Google Clients for
|
| 106 |
try:
|
| 107 |
genai.configure(api_key=GOOGLE_API_KEY)
|
| 108 |
logger.info("Configured google-generativeai with API key.")
|
|
@@ -153,6 +157,7 @@ class ChronoWeaveResponse(BaseModel):
|
|
| 153 |
return self
|
| 154 |
|
| 155 |
# --- Helper Functions ---
|
|
|
|
| 156 |
@contextlib.contextmanager
|
| 157 |
def wave_file_writer(filename: str, channels: int = 1, rate: int = AUDIO_SAMPLING_RATE, sample_width: int = 2):
|
| 158 |
"""Context manager to safely write WAV files."""
|
|
@@ -259,7 +264,7 @@ def generate_image_imagen(prompt: str, aspect_ratio: str = "1:1", task_id: str =
|
|
| 259 |
Generates an image using Vertex AI's Imagen model via the Vertex AI preview API.
|
| 260 |
|
| 261 |
Loads the pretrained Imagen model and attempts to generate an image.
|
| 262 |
-
If a quota exceeded error occurs, it
|
| 263 |
"""
|
| 264 |
logger.info(f"πΌοΈ [{task_id}] Requesting image: '{prompt[:70]}...' (Aspect: {aspect_ratio})")
|
| 265 |
try:
|
|
|
|
| 36 |
import nest_asyncio
|
| 37 |
nest_asyncio.apply()
|
| 38 |
|
| 39 |
+
# Import Vertex AI SDK and Google credentials support
|
| 40 |
import vertexai
|
| 41 |
from vertexai.preview.vision_models import ImageGenerationModel
|
| 42 |
from google.oauth2 import service_account
|
|
|
|
| 51 |
)
|
| 52 |
logger = logging.getLogger(__name__)
|
| 53 |
|
| 54 |
+
# --- App Configuration ---
|
| 55 |
st.set_page_config(page_title="ChronoWeave", layout="wide", initial_sidebar_state="expanded")
|
| 56 |
st.title("π ChronoWeave: Advanced Branching Narrative Generator")
|
| 57 |
st.markdown("""
|
|
|
|
| 63 |
TEXT_MODEL_ID = "models/gemini-1.5-flash"
|
| 64 |
AUDIO_MODEL_ID = "models/gemini-1.5-flash"
|
| 65 |
AUDIO_SAMPLING_RATE = 24000
|
| 66 |
+
IMAGE_MODEL_ID = "imagen-3.0-generate-002" # Vertex AI Imagen model identifier
|
|
|
|
| 67 |
DEFAULT_ASPECT_RATIO = "1:1"
|
| 68 |
VIDEO_FPS = 24
|
| 69 |
VIDEO_CODEC = "libx264"
|
|
|
|
| 71 |
TEMP_DIR_BASE = ".chrono_temp"
|
| 72 |
|
| 73 |
# --- Secrets and Environment Variables ---
|
| 74 |
+
# Load GOOGLE_API_KEY
|
| 75 |
try:
|
| 76 |
GOOGLE_API_KEY = st.secrets["GOOGLE_API_KEY"]
|
| 77 |
logger.info("Google API Key loaded from Streamlit secrets.")
|
|
|
|
| 83 |
st.error("π¨ **Google API Key Not Found!** Please configure it.", icon="π¨")
|
| 84 |
st.stop()
|
| 85 |
|
| 86 |
+
# Load PROJECT_ID and LOCATION
|
| 87 |
PROJECT_ID = st.secrets.get("PROJECT_ID") or os.environ.get("PROJECT_ID")
|
| 88 |
LOCATION = st.secrets.get("LOCATION") or os.environ.get("LOCATION", "us-central1")
|
| 89 |
if not PROJECT_ID:
|
| 90 |
st.error("π¨ **PROJECT_ID not set!** Please add PROJECT_ID to your secrets.", icon="π¨")
|
| 91 |
st.stop()
|
| 92 |
|
| 93 |
+
# Load and verify SERVICE_ACCOUNT_JSON
|
| 94 |
+
service_account_json = os.environ.get("SERVICE_ACCOUNT_JSON", "").strip()
|
| 95 |
+
if not service_account_json:
|
| 96 |
+
st.error("π¨ **SERVICE_ACCOUNT_JSON is missing or empty!** Please add your service account JSON to your secrets.", icon="π¨")
|
| 97 |
+
st.stop()
|
| 98 |
try:
|
| 99 |
+
service_account_info = json.loads(service_account_json)
|
| 100 |
credentials = service_account.Credentials.from_service_account_info(service_account_info)
|
| 101 |
logger.info("Service account credentials loaded successfully.")
|
| 102 |
except Exception as e:
|
| 103 |
st.error(f"π¨ Failed to load service account JSON: {e}", icon="π¨")
|
| 104 |
st.stop()
|
| 105 |
|
| 106 |
+
# Initialize Vertex AI with service account credentials
|
| 107 |
vertexai.init(project=PROJECT_ID, location=LOCATION, credentials=credentials)
|
| 108 |
|
| 109 |
+
# --- Initialize Google Clients for Text and Audio ---
|
| 110 |
try:
|
| 111 |
genai.configure(api_key=GOOGLE_API_KEY)
|
| 112 |
logger.info("Configured google-generativeai with API key.")
|
|
|
|
| 157 |
return self
|
| 158 |
|
| 159 |
# --- Helper Functions ---
|
| 160 |
+
|
| 161 |
@contextlib.contextmanager
|
| 162 |
def wave_file_writer(filename: str, channels: int = 1, rate: int = AUDIO_SAMPLING_RATE, sample_width: int = 2):
|
| 163 |
"""Context manager to safely write WAV files."""
|
|
|
|
| 264 |
Generates an image using Vertex AI's Imagen model via the Vertex AI preview API.
|
| 265 |
|
| 266 |
Loads the pretrained Imagen model and attempts to generate an image.
|
| 267 |
+
If a quota exceeded error occurs, it advises you to request a quota increase.
|
| 268 |
"""
|
| 269 |
logger.info(f"πΌοΈ [{task_id}] Requesting image: '{prompt[:70]}...' (Aspect: {aspect_ratio})")
|
| 270 |
try:
|