Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -142,8 +142,6 @@ class ContentGenerator:
|
|
| 142 |
log_message(f"β Quote generation failed: {str(e)}")
|
| 143 |
return self._get_fallback_quote()
|
| 144 |
|
| 145 |
-
|
| 146 |
-
|
| 147 |
def generate_image(self, quote, tradition, theme):
|
| 148 |
"""Generate an image using OpenAI DALL-E with enhanced prompts"""
|
| 149 |
try:
|
|
@@ -224,44 +222,44 @@ class ContentGenerator:
|
|
| 224 |
log_message(f"β Audio generation failed: {str(e)}", "error")
|
| 225 |
raise
|
| 226 |
|
| 227 |
-
def create_image_with_quote(self, image_url, quote_text, author):
|
| 228 |
-
try:
|
| 229 |
-
response = requests.get(image_url)
|
| 230 |
-
img = Image.open(BytesIO(response.content))
|
| 231 |
-
width, height = img.size
|
| 232 |
-
|
| 233 |
-
# Create blank image with text
|
| 234 |
-
txt = Image.new('RGBA', (width, height), (255, 255, 255, 0))
|
| 235 |
-
d = ImageDraw.Draw(txt)
|
| 236 |
-
|
| 237 |
-
# Semi-transparent background for text
|
| 238 |
-
d.rectangle([0, height/2-100, width, height/2+100],
|
| 239 |
-
fill=(0, 0, 0, 127))
|
| 240 |
-
|
| 241 |
try:
|
| 242 |
-
|
| 243 |
-
|
| 244 |
-
|
| 245 |
-
quote_font = ImageFont.load_default()
|
| 246 |
-
author_font = ImageFont.load_default()
|
| 247 |
|
| 248 |
-
|
| 249 |
-
|
| 250 |
-
|
| 251 |
-
d.text((width/2, height/2+60), f"- {author}",
|
| 252 |
-
font=author_font, fill=(255,255,255,255), anchor="mm")
|
| 253 |
|
| 254 |
-
|
| 255 |
-
|
|
|
|
| 256 |
|
| 257 |
-
|
| 258 |
-
|
| 259 |
-
|
| 260 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 261 |
|
| 262 |
-
|
| 263 |
-
|
| 264 |
-
|
| 265 |
|
| 266 |
|
| 267 |
def display_debug_logs():
|
|
@@ -384,73 +382,73 @@ def main():
|
|
| 384 |
st.json(st.session_state.generated_content)
|
| 385 |
|
| 386 |
with tab2:
|
| 387 |
-
|
| 388 |
-
|
| 389 |
-
st.session_state.generated_image,
|
| 390 |
-
st.session_state.generated_audio]):
|
| 391 |
-
|
| 392 |
-
col1, col2 = st.columns([2, 1])
|
| 393 |
-
|
| 394 |
-
with col1:
|
| 395 |
-
# Image with quote overlay
|
| 396 |
-
quote_data = st.session_state.generated_content
|
| 397 |
-
generator = ContentGenerator(
|
| 398 |
-
st.secrets["OPENAI_API_KEY"],
|
| 399 |
-
st.secrets["REPLICATE_API_TOKEN"]
|
| 400 |
-
)
|
| 401 |
-
final_image = generator.create_image_with_quote(
|
| 402 |
st.session_state.generated_image,
|
| 403 |
-
|
| 404 |
-
quote_data['author']
|
| 405 |
-
)
|
| 406 |
-
if final_image:
|
| 407 |
-
st.image(final_image)
|
| 408 |
-
st.download_button(
|
| 409 |
-
"π₯ Download Image",
|
| 410 |
-
data=final_image,
|
| 411 |
-
file_name="spiritual_quote.jpg",
|
| 412 |
-
mime="image/jpeg"
|
| 413 |
-
)
|
| 414 |
|
| 415 |
-
|
| 416 |
-
# Audio preview
|
| 417 |
-
st.subheader("π΅ Background Music")
|
| 418 |
-
if st.session_state.generated_audio:
|
| 419 |
-
try:
|
| 420 |
-
audio_response = requests.get(st.session_state.generated_audio)
|
| 421 |
-
if audio_response.status_code == 200:
|
| 422 |
-
st.audio(audio_response.content, format='audio/mp3')
|
| 423 |
-
st.download_button(
|
| 424 |
-
"π₯ Download Audio",
|
| 425 |
-
data=audio_response.content,
|
| 426 |
-
file_name="background_music.mp3",
|
| 427 |
-
mime="audio/mp3"
|
| 428 |
-
)
|
| 429 |
|
| 430 |
-
|
| 431 |
-
|
| 432 |
-
|
| 433 |
-
|
| 434 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 435 |
)
|
| 436 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 437 |
|
| 438 |
"{quote_data['text']}"
|
| 439 |
- {quote_data['author']}
|
| 440 |
|
| 441 |
{' '.join(hashtags)}"""
|
| 442 |
-
|
| 443 |
|
| 444 |
-
|
| 445 |
-
|
| 446 |
-
|
| 447 |
-
|
| 448 |
-
|
| 449 |
-
|
|
|
|
|
|
|
|
|
|
| 450 |
|
| 451 |
-
|
| 452 |
-
with tab3: # Make sure this is at the same indentation level as 'with tab2:'
|
| 453 |
-
display_debug_logs()
|
| 454 |
|
| 455 |
if __name__ == "__main__":
|
| 456 |
-
main()
|
|
|
|
| 142 |
log_message(f"β Quote generation failed: {str(e)}")
|
| 143 |
return self._get_fallback_quote()
|
| 144 |
|
|
|
|
|
|
|
| 145 |
def generate_image(self, quote, tradition, theme):
|
| 146 |
"""Generate an image using OpenAI DALL-E with enhanced prompts"""
|
| 147 |
try:
|
|
|
|
| 222 |
log_message(f"β Audio generation failed: {str(e)}", "error")
|
| 223 |
raise
|
| 224 |
|
| 225 |
+
def create_image_with_quote(self, image_url, quote_text, author):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 226 |
try:
|
| 227 |
+
response = requests.get(image_url)
|
| 228 |
+
img = Image.open(BytesIO(response.content))
|
| 229 |
+
width, height = img.size
|
|
|
|
|
|
|
| 230 |
|
| 231 |
+
# Create blank image with text
|
| 232 |
+
txt = Image.new('RGBA', (width, height), (255, 255, 255, 0))
|
| 233 |
+
d = ImageDraw.Draw(txt)
|
|
|
|
|
|
|
| 234 |
|
| 235 |
+
# Semi-transparent background for text
|
| 236 |
+
d.rectangle([0, height / 2 - 100, width, height / 2 + 100],
|
| 237 |
+
fill=(0, 0, 0, 127))
|
| 238 |
|
| 239 |
+
try:
|
| 240 |
+
quote_font = ImageFont.truetype("DejaVuSans-Bold", 40)
|
| 241 |
+
author_font = ImageFont.truetype("DejaVuSans", 30)
|
| 242 |
+
except:
|
| 243 |
+
quote_font = ImageFont.load_default()
|
| 244 |
+
author_font = ImageFont.load_default()
|
| 245 |
+
|
| 246 |
+
# Add text
|
| 247 |
+
d.text((width / 2, height / 2), quote_text,
|
| 248 |
+
font=quote_font, fill=(255, 255, 255, 255), anchor="mm")
|
| 249 |
+
d.text((width / 2, height / 2 + 60), f"- {author}",
|
| 250 |
+
font=author_font, fill=(255, 255, 255, 255), anchor="mm")
|
| 251 |
+
|
| 252 |
+
# Combine images
|
| 253 |
+
out = Image.alpha_composite(img.convert('RGBA'), txt)
|
| 254 |
+
|
| 255 |
+
# Convert to bytes
|
| 256 |
+
buffer = BytesIO()
|
| 257 |
+
out.convert('RGB').save(buffer, format='JPEG')
|
| 258 |
+
return buffer.getvalue()
|
| 259 |
|
| 260 |
+
except Exception as e:
|
| 261 |
+
log_message(f"Error creating overlay: {str(e)}")
|
| 262 |
+
return None
|
| 263 |
|
| 264 |
|
| 265 |
def display_debug_logs():
|
|
|
|
| 382 |
st.json(st.session_state.generated_content)
|
| 383 |
|
| 384 |
with tab2:
|
| 385 |
+
st.title("Instagram Preview")
|
| 386 |
+
if all([st.session_state.generated_content,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 387 |
st.session_state.generated_image,
|
| 388 |
+
st.session_state.generated_audio]):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 389 |
|
| 390 |
+
col1, col2 = st.columns([2, 1])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 391 |
|
| 392 |
+
with col1:
|
| 393 |
+
# Image with quote overlay
|
| 394 |
+
quote_data = st.session_state.generated_content
|
| 395 |
+
generator = ContentGenerator(
|
| 396 |
+
st.secrets["OPENAI_API_KEY"],
|
| 397 |
+
st.secrets["REPLICATE_API_TOKEN"]
|
| 398 |
+
)
|
| 399 |
+
final_image = generator.create_image_with_quote(
|
| 400 |
+
st.session_state.generated_image,
|
| 401 |
+
quote_data['text'],
|
| 402 |
+
quote_data['author']
|
| 403 |
+
)
|
| 404 |
+
if final_image:
|
| 405 |
+
st.image(final_image)
|
| 406 |
+
st.download_button(
|
| 407 |
+
"π₯ Download Image",
|
| 408 |
+
data=final_image,
|
| 409 |
+
file_name="spiritual_quote.jpg",
|
| 410 |
+
mime="image/jpeg"
|
| 411 |
)
|
| 412 |
+
|
| 413 |
+
with col2:
|
| 414 |
+
# Audio preview
|
| 415 |
+
st.subheader("π΅ Background Music")
|
| 416 |
+
if st.session_state.generated_audio:
|
| 417 |
+
try:
|
| 418 |
+
audio_response = requests.get(st.session_state.generated_audio)
|
| 419 |
+
if audio_response.status_code == 200:
|
| 420 |
+
st.audio(audio_response.content, format='audio/mp3')
|
| 421 |
+
st.download_button(
|
| 422 |
+
"π₯ Download Audio",
|
| 423 |
+
data=audio_response.content,
|
| 424 |
+
file_name="background_music.mp3",
|
| 425 |
+
mime="audio/mp3"
|
| 426 |
+
)
|
| 427 |
+
|
| 428 |
+
# Add caption and hashtags
|
| 429 |
+
st.subheader("π Caption")
|
| 430 |
+
hashtags = generate_hashtags(
|
| 431 |
+
quote_data['tradition'],
|
| 432 |
+
quote_data.get('theme', 'Wisdom')
|
| 433 |
+
)
|
| 434 |
+
caption = f"""β¨ Wisdom from {quote_data['tradition']} β¨
|
| 435 |
|
| 436 |
"{quote_data['text']}"
|
| 437 |
- {quote_data['author']}
|
| 438 |
|
| 439 |
{' '.join(hashtags)}"""
|
| 440 |
+
st.text_area("Instagram Caption", caption, height=200)
|
| 441 |
|
| 442 |
+
else:
|
| 443 |
+
st.error("Failed to load audio preview")
|
| 444 |
+
except Exception as e:
|
| 445 |
+
st.error(f"Error playing audio: {str(e)}")
|
| 446 |
+
else:
|
| 447 |
+
st.info("Generate content first to see the Instagram preview")
|
| 448 |
+
|
| 449 |
+
with tab3:
|
| 450 |
+
display_debug_logs()
|
| 451 |
|
|
|
|
|
|
|
|
|
|
| 452 |
|
| 453 |
if __name__ == "__main__":
|
| 454 |
+
main()
|