Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,6 +1,6 @@
|
|
| 1 |
import streamlit as st
|
| 2 |
import os
|
| 3 |
-
import json
|
| 4 |
from datetime import datetime
|
| 5 |
from crewai import Agent, Task, Crew
|
| 6 |
from langchain_openai import OpenAI
|
|
@@ -22,11 +22,17 @@ BASE_DIR = "data"
|
|
| 22 |
QUOTES_DIR = os.path.join(BASE_DIR, "quotes")
|
| 23 |
IMAGES_DIR = os.path.join(BASE_DIR, "images")
|
| 24 |
FINAL_IMAGES_DIR = os.path.join(BASE_DIR, "final_images")
|
|
|
|
| 25 |
|
| 26 |
# Ensure the directories exist
|
| 27 |
-
|
| 28 |
-
os.makedirs(
|
| 29 |
-
os.makedirs(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 30 |
|
| 31 |
def log_message(message, type="info"):
|
| 32 |
if 'process_logs' not in st.session_state:
|
|
@@ -115,10 +121,15 @@ class ContentGenerator:
|
|
| 115 |
# Save the quote to a file
|
| 116 |
filename = f"quote_{datetime.now().strftime('%Y%m%d_%H%M%S')}.json"
|
| 117 |
file_path = os.path.join(QUOTES_DIR, filename)
|
| 118 |
-
|
| 119 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 120 |
|
| 121 |
-
log_message(f"β¨ Generated quote saved at {file_path}")
|
| 122 |
return result
|
| 123 |
|
| 124 |
except Exception as e:
|
|
@@ -154,15 +165,65 @@ class ContentGenerator:
|
|
| 154 |
if image_response.status_code == 200:
|
| 155 |
filename = f"image_{datetime.now().strftime('%Y%m%d_%H%M%S')}.jpg"
|
| 156 |
file_path = os.path.join(IMAGES_DIR, filename)
|
| 157 |
-
|
| 158 |
-
|
| 159 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 160 |
else:
|
| 161 |
raise Exception("Failed to download image")
|
| 162 |
|
| 163 |
return image_url
|
| 164 |
except Exception as e:
|
| 165 |
log_message(f"β Image generation failed: {str(e)}", "error")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 166 |
raise
|
| 167 |
|
| 168 |
def create_image_with_quote(self, image_url, quote_text, author):
|
|
@@ -218,14 +279,20 @@ class ContentGenerator:
|
|
| 218 |
|
| 219 |
filename = f"final_image_{datetime.now().strftime('%Y%m%d_%H%M%S')}.jpg"
|
| 220 |
file_path = os.path.join(FINAL_IMAGES_DIR, filename)
|
| 221 |
-
|
| 222 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 223 |
|
| 224 |
-
log_message(f"β¨ Final image saved at {file_path}")
|
| 225 |
return final_image_bytes
|
| 226 |
|
| 227 |
except Exception as e:
|
| 228 |
-
log_message(f"Error creating overlay: {str(e)}")
|
|
|
|
| 229 |
return None
|
| 230 |
|
| 231 |
def generate_hashtags(tradition, theme):
|
|
@@ -290,7 +357,22 @@ def main():
|
|
| 290 |
except Exception as e:
|
| 291 |
st.error(f"Error generating image: {str(e)}")
|
| 292 |
|
| 293 |
-
# Step 3:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 294 |
if st.session_state.generated_content and st.session_state.generated_image:
|
| 295 |
st.subheader("Final Preview")
|
| 296 |
final_image = generator.create_image_with_quote(
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
import os
|
| 3 |
+
import json
|
| 4 |
from datetime import datetime
|
| 5 |
from crewai import Agent, Task, Crew
|
| 6 |
from langchain_openai import OpenAI
|
|
|
|
| 22 |
QUOTES_DIR = os.path.join(BASE_DIR, "quotes")
|
| 23 |
IMAGES_DIR = os.path.join(BASE_DIR, "images")
|
| 24 |
FINAL_IMAGES_DIR = os.path.join(BASE_DIR, "final_images")
|
| 25 |
+
MUSIC_DIR = os.path.join(BASE_DIR, "music") # New directory for storing music files
|
| 26 |
|
| 27 |
# Ensure the directories exist
|
| 28 |
+
try:
|
| 29 |
+
os.makedirs(QUOTES_DIR, exist_ok=True)
|
| 30 |
+
os.makedirs(IMAGES_DIR, exist_ok=True)
|
| 31 |
+
os.makedirs(FINAL_IMAGES_DIR, exist_ok=True)
|
| 32 |
+
os.makedirs(MUSIC_DIR, exist_ok=True) # Create music directory
|
| 33 |
+
logger.info(f"Directories successfully created or verified: {BASE_DIR}, {QUOTES_DIR}, {IMAGES_DIR}, {FINAL_IMAGES_DIR}, {MUSIC_DIR}")
|
| 34 |
+
except Exception as e:
|
| 35 |
+
logger.error(f"Error creating directories: {str(e)}")
|
| 36 |
|
| 37 |
def log_message(message, type="info"):
|
| 38 |
if 'process_logs' not in st.session_state:
|
|
|
|
| 121 |
# Save the quote to a file
|
| 122 |
filename = f"quote_{datetime.now().strftime('%Y%m%d_%H%M%S')}.json"
|
| 123 |
file_path = os.path.join(QUOTES_DIR, filename)
|
| 124 |
+
try:
|
| 125 |
+
with open(file_path, 'w') as f:
|
| 126 |
+
json.dump(result, f, indent=4)
|
| 127 |
+
log_message(f"β¨ Generated quote saved at {file_path}")
|
| 128 |
+
logger.info(f"Quote saved successfully at: {file_path}")
|
| 129 |
+
except Exception as e:
|
| 130 |
+
log_message(f"β Failed to save quote to file: {str(e)}", "error")
|
| 131 |
+
logger.error(f"Error saving quote: {str(e)}")
|
| 132 |
|
|
|
|
| 133 |
return result
|
| 134 |
|
| 135 |
except Exception as e:
|
|
|
|
| 165 |
if image_response.status_code == 200:
|
| 166 |
filename = f"image_{datetime.now().strftime('%Y%m%d_%H%M%S')}.jpg"
|
| 167 |
file_path = os.path.join(IMAGES_DIR, filename)
|
| 168 |
+
try:
|
| 169 |
+
with open(file_path, 'wb') as f:
|
| 170 |
+
f.write(image_response.content)
|
| 171 |
+
log_message(f"β¨ Image generated and saved at {file_path}")
|
| 172 |
+
logger.info(f"Image saved successfully at: {file_path}")
|
| 173 |
+
except Exception as e:
|
| 174 |
+
log_message(f"β Failed to save image to file: {str(e)}", "error")
|
| 175 |
+
logger.error(f"Error saving image: {str(e)}")
|
| 176 |
else:
|
| 177 |
raise Exception("Failed to download image")
|
| 178 |
|
| 179 |
return image_url
|
| 180 |
except Exception as e:
|
| 181 |
log_message(f"β Image generation failed: {str(e)}", "error")
|
| 182 |
+
logger.error(f"Image generation failed: {str(e)}")
|
| 183 |
+
raise
|
| 184 |
+
|
| 185 |
+
def generate_audio(self, tradition, theme):
|
| 186 |
+
try:
|
| 187 |
+
log_message("π΅ Starting audio generation")
|
| 188 |
+
music_prompts = {
|
| 189 |
+
"Buddhism": "Tibetan singing bowls and gentle bells with peaceful ambient drones",
|
| 190 |
+
"Hinduism": "Indian bansuri flute with gentle tabla rhythms",
|
| 191 |
+
"Modern Spirituality": "Ambient synthesizer with crystal bowls",
|
| 192 |
+
"default": "Peaceful ambient music with gentle bells"
|
| 193 |
+
}
|
| 194 |
+
prompt = music_prompts.get(tradition, music_prompts["default"])
|
| 195 |
+
|
| 196 |
+
output = self.replicate_client.run(
|
| 197 |
+
"meta/musicgen:671ac645ce5e552cc63a54a2bbff63fcf798043055d2dac5fc9e36a837eedcfb",
|
| 198 |
+
input={
|
| 199 |
+
"prompt": prompt,
|
| 200 |
+
"model_version": "stereo-large",
|
| 201 |
+
"output_format": "mp3",
|
| 202 |
+
"duration": 15
|
| 203 |
+
}
|
| 204 |
+
)
|
| 205 |
+
audio_url = output if isinstance(output, str) else str(output)
|
| 206 |
+
|
| 207 |
+
# Download and save the generated music
|
| 208 |
+
music_response = requests.get(audio_url)
|
| 209 |
+
if music_response.status_code == 200:
|
| 210 |
+
filename = f"music_{datetime.now().strftime('%Y%m%d_%H%M%S')}.mp3"
|
| 211 |
+
file_path = os.path.join(MUSIC_DIR, filename)
|
| 212 |
+
try:
|
| 213 |
+
with open(file_path, 'wb') as f:
|
| 214 |
+
f.write(music_response.content)
|
| 215 |
+
log_message(f"β¨ Music generated and saved at {file_path}")
|
| 216 |
+
logger.info(f"Music saved successfully at: {file_path}")
|
| 217 |
+
except Exception as e:
|
| 218 |
+
log_message(f"β Failed to save music to file: {str(e)}", "error")
|
| 219 |
+
logger.error(f"Error saving music: {str(e)}")
|
| 220 |
+
else:
|
| 221 |
+
raise Exception("Failed to download audio")
|
| 222 |
+
|
| 223 |
+
return audio_url
|
| 224 |
+
except Exception as e:
|
| 225 |
+
log_message(f"β Music generation failed: {str(e)}", "error")
|
| 226 |
+
logger.error(f"Music generation failed: {str(e)}")
|
| 227 |
raise
|
| 228 |
|
| 229 |
def create_image_with_quote(self, image_url, quote_text, author):
|
|
|
|
| 279 |
|
| 280 |
filename = f"final_image_{datetime.now().strftime('%Y%m%d_%H%M%S')}.jpg"
|
| 281 |
file_path = os.path.join(FINAL_IMAGES_DIR, filename)
|
| 282 |
+
try:
|
| 283 |
+
with open(file_path, 'wb') as f:
|
| 284 |
+
f.write(final_image_bytes)
|
| 285 |
+
log_message(f"β¨ Final image saved at {file_path}")
|
| 286 |
+
logger.info(f"Final image saved successfully at: {file_path}")
|
| 287 |
+
except Exception as e:
|
| 288 |
+
log_message(f"β Failed to save final image to file: {str(e)}", "error")
|
| 289 |
+
logger.error(f"Error saving final image: {str(e)}")
|
| 290 |
|
|
|
|
| 291 |
return final_image_bytes
|
| 292 |
|
| 293 |
except Exception as e:
|
| 294 |
+
log_message(f"Error creating overlay: {str(e)}", "error")
|
| 295 |
+
logger.error(f"Error creating image with quote: {str(e)}")
|
| 296 |
return None
|
| 297 |
|
| 298 |
def generate_hashtags(tradition, theme):
|
|
|
|
| 357 |
except Exception as e:
|
| 358 |
st.error(f"Error generating image: {str(e)}")
|
| 359 |
|
| 360 |
+
# Step 3: Generate Music
|
| 361 |
+
if st.session_state.generated_content and st.button("Generate Music"):
|
| 362 |
+
try:
|
| 363 |
+
audio_url = generator.generate_audio(
|
| 364 |
+
st.session_state.generated_content["tradition"],
|
| 365 |
+
st.session_state.generated_content["theme"]
|
| 366 |
+
)
|
| 367 |
+
st.session_state.generated_audio = audio_url
|
| 368 |
+
st.success("β¨ Music generated!")
|
| 369 |
+
audio_response = requests.get(audio_url)
|
| 370 |
+
if audio_response.status_code == 200:
|
| 371 |
+
st.audio(audio_response.content, format='audio/mp3')
|
| 372 |
+
except Exception as e:
|
| 373 |
+
st.error(f"Error generating audio: {str(e)}")
|
| 374 |
+
|
| 375 |
+
# Step 4: Preview Quote on Image
|
| 376 |
if st.session_state.generated_content and st.session_state.generated_image:
|
| 377 |
st.subheader("Final Preview")
|
| 378 |
final_image = generator.create_image_with_quote(
|