Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -136,32 +136,6 @@ class ContentGenerator:
|
|
| 136 |
log_message(f"β Image generation failed: {str(e)}", "error")
|
| 137 |
raise
|
| 138 |
|
| 139 |
-
def generate_audio(self, tradition, theme):
|
| 140 |
-
try:
|
| 141 |
-
log_message("π΅ Starting audio generation")
|
| 142 |
-
music_prompts = {
|
| 143 |
-
"Buddhism": "Tibetan singing bowls and gentle bells with peaceful ambient drones",
|
| 144 |
-
"Hinduism": "Indian bansuri flute with gentle tabla rhythms",
|
| 145 |
-
"Modern Spirituality": "Ambient synthesizer with crystal bowls",
|
| 146 |
-
"default": "Peaceful ambient music with gentle bells"
|
| 147 |
-
}
|
| 148 |
-
prompt = music_prompts.get(tradition, music_prompts["default"])
|
| 149 |
-
|
| 150 |
-
output = self.replicate_client.run(
|
| 151 |
-
"meta/musicgen:671ac645ce5e552cc63a54a2bbff63fcf798043055d2dac5fc9e36a837eedcfb",
|
| 152 |
-
input={
|
| 153 |
-
"prompt": prompt,
|
| 154 |
-
"model_version": "stereo-large",
|
| 155 |
-
"output_format": "mp3",
|
| 156 |
-
"duration": 15
|
| 157 |
-
}
|
| 158 |
-
)
|
| 159 |
-
log_message("β¨ Audio generated successfully")
|
| 160 |
-
return output if isinstance(output, str) else str(output)
|
| 161 |
-
except Exception as e:
|
| 162 |
-
log_message(f"β Audio generation failed: {str(e)}", "error")
|
| 163 |
-
raise
|
| 164 |
-
|
| 165 |
def create_image_with_quote(self, image_url, quote_text, author):
|
| 166 |
try:
|
| 167 |
response = requests.get(image_url)
|
|
@@ -188,7 +162,11 @@ class ContentGenerator:
|
|
| 188 |
# Draw each line of the wrapped text
|
| 189 |
y_offset = height / 2 - 100 # Starting Y position for the quote
|
| 190 |
for line in lines:
|
| 191 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 192 |
x_position = (width - line_width) / 2 # Center the line horizontally
|
| 193 |
d.text((x_position, y_offset), line, font=quote_font, fill=(255, 255, 255, 255))
|
| 194 |
y_offset += line_height + 5 # Move to the next line, with a small line gap
|
|
@@ -196,7 +174,8 @@ class ContentGenerator:
|
|
| 196 |
# Draw the author text below the quote
|
| 197 |
author_y_position = y_offset + 20 # Leave some space below the quote for the author
|
| 198 |
author_text = f"- {author}"
|
| 199 |
-
|
|
|
|
| 200 |
author_x_position = (width - author_width) / 2
|
| 201 |
d.text((author_x_position, author_y_position), author_text, font=author_font, fill=(255, 255, 255, 255))
|
| 202 |
|
|
@@ -212,29 +191,6 @@ class ContentGenerator:
|
|
| 212 |
log_message(f"Error creating overlay: {str(e)}")
|
| 213 |
return None
|
| 214 |
|
| 215 |
-
def display_debug_logs(self):
|
| 216 |
-
st.markdown("### π Process Logs")
|
| 217 |
-
log_tabs = st.tabs(["All Logs", "Errors Only", "Timeline"])
|
| 218 |
-
|
| 219 |
-
with log_tabs[0]:
|
| 220 |
-
for log in st.session_state.process_logs:
|
| 221 |
-
st.info(f"{log['timestamp']} - {log['message']}" if log["type"] != "error"
|
| 222 |
-
else st.error(f"{log['timestamp']} - {log['message']}"))
|
| 223 |
-
|
| 224 |
-
with log_tabs[1]:
|
| 225 |
-
errors = [log for log in st.session_state.process_logs if log["type"] == "error"]
|
| 226 |
-
if errors:
|
| 227 |
-
for error in errors:
|
| 228 |
-
st.error(f"{error['timestamp']} - {error['message']}")
|
| 229 |
-
else:
|
| 230 |
-
st.success("No errors found! π")
|
| 231 |
-
|
| 232 |
-
with log_tabs[2]:
|
| 233 |
-
st.markdown("#### Generation Timeline")
|
| 234 |
-
for log in st.session_state.process_logs:
|
| 235 |
-
with st.expander(f"{log['timestamp']} - {log['message'][:50]}...", expanded=False):
|
| 236 |
-
st.text(log['message'])
|
| 237 |
-
|
| 238 |
def generate_hashtags(tradition, theme):
|
| 239 |
base_tags = ["#spirituality", "#mindfulness", "#wisdom", "#inspiration"]
|
| 240 |
tradition_tags = {
|
|
@@ -271,10 +227,9 @@ def main():
|
|
| 271 |
with col1:
|
| 272 |
st.title("ποΈ Spiritual Content Generator")
|
| 273 |
|
| 274 |
-
generator = ContentGenerator(st.secrets["OPENAI_API_KEY"],
|
| 275 |
-
st.secrets["REPLICATE_API_TOKEN"])
|
| 276 |
|
| 277 |
-
#
|
| 278 |
if st.button("Generate Quote"):
|
| 279 |
try:
|
| 280 |
quote_data = generator.generate_quote()
|
|
@@ -284,6 +239,7 @@ def main():
|
|
| 284 |
except Exception as e:
|
| 285 |
st.error(f"Error generating quote: {str(e)}")
|
| 286 |
|
|
|
|
| 287 |
if st.session_state.generated_content and st.button("Create Image"):
|
| 288 |
try:
|
| 289 |
image_url = generator.generate_image(
|
|
@@ -297,53 +253,48 @@ def main():
|
|
| 297 |
except Exception as e:
|
| 298 |
st.error(f"Error generating image: {str(e)}")
|
| 299 |
|
| 300 |
-
|
| 301 |
-
|
| 302 |
-
audio_url = generator.generate_audio(
|
| 303 |
-
st.session_state.generated_content["tradition"],
|
| 304 |
-
st.session_state.generated_content["theme"]
|
| 305 |
-
)
|
| 306 |
-
st.session_state.generated_audio = audio_url
|
| 307 |
-
st.success("β¨ Music generated!")
|
| 308 |
-
audio_response = requests.get(audio_url)
|
| 309 |
-
if audio_response.status_code == 200:
|
| 310 |
-
st.audio(audio_response.content, format='audio/mp3')
|
| 311 |
-
except Exception as e:
|
| 312 |
-
st.error(f"Error generating audio: {str(e)}")
|
| 313 |
-
|
| 314 |
-
# Preview tab content
|
| 315 |
-
if all([st.session_state.generated_content,
|
| 316 |
-
st.session_state.generated_image,
|
| 317 |
-
st.session_state.generated_audio]):
|
| 318 |
st.subheader("Final Preview")
|
| 319 |
-
|
| 320 |
final_image = generator.create_image_with_quote(
|
| 321 |
st.session_state.generated_image,
|
| 322 |
st.session_state.generated_content['text'],
|
| 323 |
st.session_state.generated_content['author']
|
| 324 |
)
|
| 325 |
-
|
| 326 |
if final_image:
|
| 327 |
st.image(final_image)
|
| 328 |
st.download_button("π₯ Download Image", final_image,
|
| 329 |
-
|
| 330 |
|
| 331 |
-
|
| 332 |
-
|
| 333 |
-
st.
|
| 334 |
-
|
| 335 |
-
|
| 336 |
-
|
| 337 |
-
st.session_state.generated_content['tradition'],
|
| 338 |
-
st.session_state.generated_content.get('theme', 'Wisdom')
|
| 339 |
-
)
|
| 340 |
-
caption = f"""β¨ Wisdom from {st.session_state.generated_content['tradition']} β¨
|
| 341 |
|
| 342 |
"{st.session_state.generated_content['text']}"
|
| 343 |
- {st.session_state.generated_content['author']}
|
| 344 |
|
| 345 |
{' '.join(hashtags)}"""
|
| 346 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 347 |
|
| 348 |
# Live debug log
|
| 349 |
with col2:
|
|
@@ -361,4 +312,4 @@ def main():
|
|
| 361 |
update_logs()
|
| 362 |
|
| 363 |
if __name__ == "__main__":
|
| 364 |
-
main()
|
|
|
|
| 136 |
log_message(f"β Image generation failed: {str(e)}", "error")
|
| 137 |
raise
|
| 138 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 139 |
def create_image_with_quote(self, image_url, quote_text, author):
|
| 140 |
try:
|
| 141 |
response = requests.get(image_url)
|
|
|
|
| 162 |
# Draw each line of the wrapped text
|
| 163 |
y_offset = height / 2 - 100 # Starting Y position for the quote
|
| 164 |
for line in lines:
|
| 165 |
+
# Calculate the text size using textbbox
|
| 166 |
+
bbox = d.textbbox((0, 0), line, font=quote_font)
|
| 167 |
+
line_width = bbox[2] - bbox[0]
|
| 168 |
+
line_height = bbox[3] - bbox[1]
|
| 169 |
+
|
| 170 |
x_position = (width - line_width) / 2 # Center the line horizontally
|
| 171 |
d.text((x_position, y_offset), line, font=quote_font, fill=(255, 255, 255, 255))
|
| 172 |
y_offset += line_height + 5 # Move to the next line, with a small line gap
|
|
|
|
| 174 |
# Draw the author text below the quote
|
| 175 |
author_y_position = y_offset + 20 # Leave some space below the quote for the author
|
| 176 |
author_text = f"- {author}"
|
| 177 |
+
author_bbox = d.textbbox((0, 0), author_text, font=author_font)
|
| 178 |
+
author_width = author_bbox[2] - author_bbox[0]
|
| 179 |
author_x_position = (width - author_width) / 2
|
| 180 |
d.text((author_x_position, author_y_position), author_text, font=author_font, fill=(255, 255, 255, 255))
|
| 181 |
|
|
|
|
| 191 |
log_message(f"Error creating overlay: {str(e)}")
|
| 192 |
return None
|
| 193 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 194 |
def generate_hashtags(tradition, theme):
|
| 195 |
base_tags = ["#spirituality", "#mindfulness", "#wisdom", "#inspiration"]
|
| 196 |
tradition_tags = {
|
|
|
|
| 227 |
with col1:
|
| 228 |
st.title("ποΈ Spiritual Content Generator")
|
| 229 |
|
| 230 |
+
generator = ContentGenerator(st.secrets["OPENAI_API_KEY"], st.secrets["REPLICATE_API_TOKEN"])
|
|
|
|
| 231 |
|
| 232 |
+
# Step 1: Generate Quote
|
| 233 |
if st.button("Generate Quote"):
|
| 234 |
try:
|
| 235 |
quote_data = generator.generate_quote()
|
|
|
|
| 239 |
except Exception as e:
|
| 240 |
st.error(f"Error generating quote: {str(e)}")
|
| 241 |
|
| 242 |
+
# Step 2: Create Image with Quote
|
| 243 |
if st.session_state.generated_content and st.button("Create Image"):
|
| 244 |
try:
|
| 245 |
image_url = generator.generate_image(
|
|
|
|
| 253 |
except Exception as e:
|
| 254 |
st.error(f"Error generating image: {str(e)}")
|
| 255 |
|
| 256 |
+
# Step 3: Preview Quote on Image
|
| 257 |
+
if st.session_state.generated_content and st.session_state.generated_image:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 258 |
st.subheader("Final Preview")
|
|
|
|
| 259 |
final_image = generator.create_image_with_quote(
|
| 260 |
st.session_state.generated_image,
|
| 261 |
st.session_state.generated_content['text'],
|
| 262 |
st.session_state.generated_content['author']
|
| 263 |
)
|
|
|
|
| 264 |
if final_image:
|
| 265 |
st.image(final_image)
|
| 266 |
st.download_button("π₯ Download Image", final_image,
|
| 267 |
+
"spiritual_quote.jpg", "image/jpeg")
|
| 268 |
|
| 269 |
+
# Generate Instagram Caption
|
| 270 |
+
hashtags = generate_hashtags(
|
| 271 |
+
st.session_state.generated_content['tradition'],
|
| 272 |
+
st.session_state.generated_content.get('theme', 'Wisdom')
|
| 273 |
+
)
|
| 274 |
+
caption = f"""β¨ Wisdom from {st.session_state.generated_content['tradition']} β¨
|
|
|
|
|
|
|
|
|
|
|
|
|
| 275 |
|
| 276 |
"{st.session_state.generated_content['text']}"
|
| 277 |
- {st.session_state.generated_content['author']}
|
| 278 |
|
| 279 |
{' '.join(hashtags)}"""
|
| 280 |
+
st.text_area("Instagram Caption", caption, height=200)
|
| 281 |
+
|
| 282 |
+
# Optional Step: Generate Music
|
| 283 |
+
if st.session_state.generated_image and st.button("Generate Music"):
|
| 284 |
+
try:
|
| 285 |
+
audio_url = generator.generate_audio(
|
| 286 |
+
st.session_state.generated_content["tradition"],
|
| 287 |
+
st.session_state.generated_content["theme"]
|
| 288 |
+
)
|
| 289 |
+
st.session_state.generated_audio = audio_url
|
| 290 |
+
st.success("β¨ Music generated!")
|
| 291 |
+
audio_response = requests.get(audio_url)
|
| 292 |
+
if audio_response.status_code == 200:
|
| 293 |
+
st.audio(audio_response.content, format='audio/mp3')
|
| 294 |
+
st.download_button("π₯ Download Audio", audio_response.content,
|
| 295 |
+
"background_music.mp3", "audio/mp3")
|
| 296 |
+
except Exception as e:
|
| 297 |
+
st.error(f"Error generating audio: {str(e)}")
|
| 298 |
|
| 299 |
# Live debug log
|
| 300 |
with col2:
|
|
|
|
| 312 |
update_logs()
|
| 313 |
|
| 314 |
if __name__ == "__main__":
|
| 315 |
+
main()
|