Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -3,14 +3,12 @@ import os
|
|
| 3 |
from datetime import datetime
|
| 4 |
import json
|
| 5 |
from crewai import Agent, Task, Crew
|
| 6 |
-
from langchain_community.tools import DuckDuckGoSearchRun
|
| 7 |
-
from langchain_community.utilities import DuckDuckGoSearchAPIWrapper
|
| 8 |
from langchain_openai import OpenAI
|
| 9 |
from openai import OpenAI as OpenAIClient
|
| 10 |
import logging
|
| 11 |
import requests
|
| 12 |
import replicate
|
| 13 |
-
import
|
| 14 |
|
| 15 |
# Set up logging
|
| 16 |
logging.basicConfig(level=logging.INFO)
|
|
@@ -27,58 +25,99 @@ def log_message(message, type="info"):
|
|
| 27 |
"type": type
|
| 28 |
})
|
| 29 |
|
| 30 |
-
class
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 46 |
|
| 47 |
class ContentGenerator:
|
| 48 |
def __init__(self, openai_api_key, replicate_api_key):
|
| 49 |
self.llm = OpenAI(api_key=openai_api_key, temperature=0.7)
|
| 50 |
self.openai_client = OpenAIClient(api_key=openai_api_key)
|
| 51 |
self.replicate_client = replicate.Client(api_token=replicate_api_key)
|
| 52 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 53 |
|
| 54 |
def generate_quote(self):
|
| 55 |
-
"""Generate a new spiritual quote
|
| 56 |
try:
|
| 57 |
log_message("π― Starting quote generation process")
|
| 58 |
|
|
|
|
|
|
|
| 59 |
quote_curator = Agent(
|
| 60 |
role='Quote Curator',
|
| 61 |
-
goal='Generate and
|
| 62 |
-
backstory="You are
|
| 63 |
-
|
|
|
|
|
|
|
| 64 |
llm=self.llm,
|
| 65 |
-
verbose=True
|
| 66 |
-
max_iterations=3
|
| 67 |
)
|
| 68 |
|
| 69 |
task = Task(
|
| 70 |
-
description="""Generate a
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 78 |
{
|
| 79 |
-
"quote": "The
|
| 80 |
-
"author": "
|
| 81 |
-
"tradition": "
|
|
|
|
| 82 |
}""",
|
| 83 |
agent=quote_curator
|
| 84 |
)
|
|
@@ -89,45 +128,60 @@ class ContentGenerator:
|
|
| 89 |
verbose=True
|
| 90 |
)
|
| 91 |
|
| 92 |
-
log_message("π€ Agent starting task...")
|
| 93 |
result = crew.kickoff()
|
| 94 |
log_message("β¨ Quote generation completed successfully")
|
| 95 |
-
|
| 96 |
return self._parse_quote_result(result)
|
| 97 |
|
| 98 |
except Exception as e:
|
| 99 |
log_message(f"β Quote generation failed: {str(e)}", "error")
|
| 100 |
-
|
| 101 |
|
| 102 |
def _parse_quote_result(self, result):
|
| 103 |
"""Parse the generated quote result"""
|
| 104 |
try:
|
| 105 |
if isinstance(result, str):
|
| 106 |
result = json.loads(result)
|
| 107 |
-
|
| 108 |
"text": result.get('quote', ''),
|
| 109 |
"author": result.get('author', ''),
|
| 110 |
"tradition": result.get('tradition', ''),
|
|
|
|
| 111 |
"generated_at": datetime.now().strftime("%Y-%m-%d %H:%M:%S")
|
| 112 |
}
|
| 113 |
-
log_message(f"π Generated quote: '{parsed_result['text']}' - {parsed_result['author']}")
|
| 114 |
-
return parsed_result
|
| 115 |
except Exception as e:
|
| 116 |
log_message(f"β Error parsing quote result: {str(e)}", "error")
|
| 117 |
raise
|
| 118 |
|
| 119 |
-
def generate_image(self, quote, tradition):
|
| 120 |
-
"""Generate an image using OpenAI DALL-E"""
|
| 121 |
try:
|
| 122 |
log_message("π¨ Starting image generation")
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 129 |
|
| 130 |
-
log_message(f"π€ Sending prompt to DALL-E")
|
| 131 |
response = self.openai_client.images.generate(
|
| 132 |
model="dall-e-3",
|
| 133 |
prompt=prompt,
|
|
@@ -141,21 +195,36 @@ class ContentGenerator:
|
|
| 141 |
log_message(f"β Image generation failed: {str(e)}", "error")
|
| 142 |
raise
|
| 143 |
|
| 144 |
-
def generate_audio(self, tradition):
|
| 145 |
-
"""Generate background music using MusicGen"""
|
| 146 |
try:
|
| 147 |
log_message("π΅ Starting audio generation")
|
| 148 |
-
|
| 149 |
-
|
| 150 |
-
|
| 151 |
-
"
|
| 152 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 153 |
}
|
| 154 |
|
| 155 |
-
|
| 156 |
-
|
| 157 |
|
| 158 |
-
log_message("π€ Sending prompt to MusicGen")
|
| 159 |
output = self.replicate_client.run(
|
| 160 |
"meta/musicgen:7be0f12c54a8d0d0d720305c1e6ea10c48d5f0a3afce48478341a0fe682a8787",
|
| 161 |
input={
|
|
@@ -166,15 +235,13 @@ class ContentGenerator:
|
|
| 166 |
}
|
| 167 |
)
|
| 168 |
|
| 169 |
-
|
| 170 |
-
|
| 171 |
-
return output
|
| 172 |
-
else:
|
| 173 |
-
raise Exception("No output received from MusicGen")
|
| 174 |
except Exception as e:
|
| 175 |
log_message(f"β Audio generation failed: {str(e)}", "error")
|
| 176 |
raise
|
| 177 |
|
|
|
|
| 178 |
def generate_hashtags(tradition):
|
| 179 |
"""Generate relevant hashtags"""
|
| 180 |
base_tags = ["#spirituality", "#mindfulness", "#wisdom", "#inspiration", "#meditation"]
|
|
|
|
| 3 |
from datetime import datetime
|
| 4 |
import json
|
| 5 |
from crewai import Agent, Task, Crew
|
|
|
|
|
|
|
| 6 |
from langchain_openai import OpenAI
|
| 7 |
from openai import OpenAI as OpenAIClient
|
| 8 |
import logging
|
| 9 |
import requests
|
| 10 |
import replicate
|
| 11 |
+
import random
|
| 12 |
|
| 13 |
# Set up logging
|
| 14 |
logging.basicConfig(level=logging.INFO)
|
|
|
|
| 25 |
"type": type
|
| 26 |
})
|
| 27 |
|
| 28 |
+
class SpiritualThemes:
|
| 29 |
+
"""Diverse spiritual themes for content generation"""
|
| 30 |
+
|
| 31 |
+
TRADITIONS = {
|
| 32 |
+
"Eastern": [
|
| 33 |
+
"Buddhism", "Hinduism", "Taoism", "Zen", "Vedanta",
|
| 34 |
+
"Yoga Philosophy", "Jainism", "Sikhism", "Confucianism"
|
| 35 |
+
],
|
| 36 |
+
"Western": [
|
| 37 |
+
"Christian Mysticism", "Sufi Wisdom", "Kabbalah",
|
| 38 |
+
"Greek Philosophy", "Modern Spirituality", "Stoicism"
|
| 39 |
+
],
|
| 40 |
+
"Indigenous": [
|
| 41 |
+
"Native American Wisdom", "Aboriginal Spirituality",
|
| 42 |
+
"African Traditional", "Maori Wisdom"
|
| 43 |
+
],
|
| 44 |
+
"Modern": [
|
| 45 |
+
"Mindfulness", "Contemporary Philosophy",
|
| 46 |
+
"Integrative Spirituality", "Secular Wisdom"
|
| 47 |
+
]
|
| 48 |
+
}
|
| 49 |
+
|
| 50 |
+
THEMES = [
|
| 51 |
+
"Inner Peace", "Mindfulness", "Compassion", "Wisdom",
|
| 52 |
+
"Self-Discovery", "Unity", "Balance", "Transformation",
|
| 53 |
+
"Enlightenment", "Purpose", "Gratitude", "Presence",
|
| 54 |
+
"Love", "Harmony", "Truth", "Liberation", "Service",
|
| 55 |
+
"Connection", "Growth", "Awareness"
|
| 56 |
+
]
|
| 57 |
+
|
| 58 |
+
QUOTE_STYLES = [
|
| 59 |
+
"contemplative", "inspirational", "philosophical",
|
| 60 |
+
"practical wisdom", "mystical insight", "ancient teaching",
|
| 61 |
+
"modern interpretation", "timeless truth", "daily practice",
|
| 62 |
+
"meditation guidance"
|
| 63 |
+
]
|
| 64 |
|
| 65 |
class ContentGenerator:
|
| 66 |
def __init__(self, openai_api_key, replicate_api_key):
|
| 67 |
self.llm = OpenAI(api_key=openai_api_key, temperature=0.7)
|
| 68 |
self.openai_client = OpenAIClient(api_key=openai_api_key)
|
| 69 |
self.replicate_client = replicate.Client(api_token=replicate_api_key)
|
| 70 |
+
|
| 71 |
+
def _generate_prompt_variation(self):
|
| 72 |
+
"""Generate varied prompts for quote generation"""
|
| 73 |
+
tradition_category = random.choice(list(SpiritualThemes.TRADITIONS.keys()))
|
| 74 |
+
traditions = SpiritualThemes.TRADITIONS[tradition_category]
|
| 75 |
+
themes = random.sample(SpiritualThemes.THEMES, 2)
|
| 76 |
+
style = random.choice(SpiritualThemes.QUOTE_STYLES)
|
| 77 |
+
|
| 78 |
+
return f"""Focus on {tradition_category} wisdom, particularly from {', '.join(traditions)}.
|
| 79 |
+
Explore themes of {themes[0]} and {themes[1]} in a {style} style.
|
| 80 |
+
Ensure the quote is profound yet accessible for social media."""
|
| 81 |
|
| 82 |
def generate_quote(self):
|
| 83 |
+
"""Generate a new spiritual quote with enhanced variety"""
|
| 84 |
try:
|
| 85 |
log_message("π― Starting quote generation process")
|
| 86 |
|
| 87 |
+
prompt_variation = self._generate_prompt_variation()
|
| 88 |
+
|
| 89 |
quote_curator = Agent(
|
| 90 |
role='Quote Curator',
|
| 91 |
+
goal='Generate diverse and profound spiritual quotes',
|
| 92 |
+
backstory="""You are a wise curator of spiritual wisdom with extensive knowledge
|
| 93 |
+
of both ancient traditions and modern spiritual thought. You understand the nuances
|
| 94 |
+
of different spiritual and philosophical traditions, and can communicate their wisdom
|
| 95 |
+
in an engaging, social media-friendly way.""",
|
| 96 |
llm=self.llm,
|
| 97 |
+
verbose=True
|
|
|
|
| 98 |
)
|
| 99 |
|
| 100 |
task = Task(
|
| 101 |
+
description=f"""Generate a spiritual quote following these criteria:
|
| 102 |
+
{prompt_variation}
|
| 103 |
+
|
| 104 |
+
Requirements:
|
| 105 |
+
1. Ensure quote authenticity and proper attribution
|
| 106 |
+
2. Balance depth with accessibility
|
| 107 |
+
3. Keep length appropriate for Instagram
|
| 108 |
+
4. Include cultural context if relevant
|
| 109 |
+
|
| 110 |
+
Return in JSON format with keys:
|
| 111 |
+
- quote: the spiritual quote text
|
| 112 |
+
- author: name of the source
|
| 113 |
+
- tradition: specific tradition or philosophy
|
| 114 |
+
- theme: primary theme of the quote""",
|
| 115 |
+
expected_output="""A JSON object containing the quote details:
|
| 116 |
{
|
| 117 |
+
"quote": "The quote text",
|
| 118 |
+
"author": "Author name",
|
| 119 |
+
"tradition": "Spiritual tradition",
|
| 120 |
+
"theme": "Primary theme"
|
| 121 |
}""",
|
| 122 |
agent=quote_curator
|
| 123 |
)
|
|
|
|
| 128 |
verbose=True
|
| 129 |
)
|
| 130 |
|
|
|
|
| 131 |
result = crew.kickoff()
|
| 132 |
log_message("β¨ Quote generation completed successfully")
|
|
|
|
| 133 |
return self._parse_quote_result(result)
|
| 134 |
|
| 135 |
except Exception as e:
|
| 136 |
log_message(f"β Quote generation failed: {str(e)}", "error")
|
| 137 |
+
return self._get_fallback_quote()
|
| 138 |
|
| 139 |
def _parse_quote_result(self, result):
|
| 140 |
"""Parse the generated quote result"""
|
| 141 |
try:
|
| 142 |
if isinstance(result, str):
|
| 143 |
result = json.loads(result)
|
| 144 |
+
return {
|
| 145 |
"text": result.get('quote', ''),
|
| 146 |
"author": result.get('author', ''),
|
| 147 |
"tradition": result.get('tradition', ''),
|
| 148 |
+
"theme": result.get('theme', ''),
|
| 149 |
"generated_at": datetime.now().strftime("%Y-%m-%d %H:%M:%S")
|
| 150 |
}
|
|
|
|
|
|
|
| 151 |
except Exception as e:
|
| 152 |
log_message(f"β Error parsing quote result: {str(e)}", "error")
|
| 153 |
raise
|
| 154 |
|
| 155 |
+
def generate_image(self, quote, tradition, theme):
|
| 156 |
+
"""Generate an image using OpenAI DALL-E with enhanced prompts"""
|
| 157 |
try:
|
| 158 |
log_message("π¨ Starting image generation")
|
| 159 |
+
|
| 160 |
+
# Generate varied image styles based on tradition and theme
|
| 161 |
+
style_prompts = {
|
| 162 |
+
"Buddhism": "zen minimalism, misty mountains, lotus flowers",
|
| 163 |
+
"Hinduism": "mandala patterns, sacred geometry, vibrant cosmos",
|
| 164 |
+
"Taoism": "flowing water, yin-yang harmony, natural elements",
|
| 165 |
+
"Modern Spirituality": "abstract sacred geometry, cosmic patterns",
|
| 166 |
+
"Christian Mysticism": "ethereal light, cathedral aesthetics",
|
| 167 |
+
"Sufi Wisdom": "whirling patterns, desert wisdom, geometric art",
|
| 168 |
+
"Indigenous": "natural elements, earth tones, tribal patterns",
|
| 169 |
+
"default": "serene minimalism, sacred geometry, natural elements"
|
| 170 |
+
}
|
| 171 |
+
|
| 172 |
+
style = style_prompts.get(tradition, style_prompts["default"])
|
| 173 |
+
|
| 174 |
+
prompt = f"""Create a spiritual and contemplative image inspired by this theme: '{theme}'
|
| 175 |
+
Style inspiration: {style}
|
| 176 |
+
Mood: serene, inspiring, contemplative
|
| 177 |
+
Requirements:
|
| 178 |
+
- Suitable for Instagram
|
| 179 |
+
- Leave space for text overlay
|
| 180 |
+
- Balanced composition
|
| 181 |
+
- No text or symbols
|
| 182 |
+
- Peaceful and harmonious colors
|
| 183 |
+
Make it visually striking yet subtle enough for quote overlay."""
|
| 184 |
|
|
|
|
| 185 |
response = self.openai_client.images.generate(
|
| 186 |
model="dall-e-3",
|
| 187 |
prompt=prompt,
|
|
|
|
| 195 |
log_message(f"β Image generation failed: {str(e)}", "error")
|
| 196 |
raise
|
| 197 |
|
| 198 |
+
def generate_audio(self, tradition, theme):
|
| 199 |
+
"""Generate background music using MusicGen with enhanced variety"""
|
| 200 |
try:
|
| 201 |
log_message("π΅ Starting audio generation")
|
| 202 |
+
|
| 203 |
+
# Enhanced music prompts based on tradition and theme
|
| 204 |
+
music_prompts = {
|
| 205 |
+
"Buddhism": {
|
| 206 |
+
"Inner Peace": "Tibetan singing bowls with gentle bells, very peaceful",
|
| 207 |
+
"Mindfulness": "Zen flute with soft water sounds, mindful atmosphere",
|
| 208 |
+
"default": "Meditation bells with nature sounds"
|
| 209 |
+
},
|
| 210 |
+
"Hinduism": {
|
| 211 |
+
"Devotion": "Indian flute with soft tabla, devotional mood",
|
| 212 |
+
"Wisdom": "Gentle sitar with peaceful drone",
|
| 213 |
+
"default": "Sanskrit chants with soft instrumentation"
|
| 214 |
+
},
|
| 215 |
+
"Modern Spirituality": {
|
| 216 |
+
"Awareness": "Ambient synthesizer with crystal bowls",
|
| 217 |
+
"Growth": "Uplifting piano with gentle pads",
|
| 218 |
+
"default": "Modern meditation music with nature sounds"
|
| 219 |
+
},
|
| 220 |
+
"default": {
|
| 221 |
+
"default": "Peaceful ambient music with gentle bells"
|
| 222 |
+
}
|
| 223 |
}
|
| 224 |
|
| 225 |
+
tradition_prompts = music_prompts.get(tradition, music_prompts["default"])
|
| 226 |
+
prompt = tradition_prompts.get(theme, tradition_prompts["default"])
|
| 227 |
|
|
|
|
| 228 |
output = self.replicate_client.run(
|
| 229 |
"meta/musicgen:7be0f12c54a8d0d0d720305c1e6ea10c48d5f0a3afce48478341a0fe682a8787",
|
| 230 |
input={
|
|
|
|
| 235 |
}
|
| 236 |
)
|
| 237 |
|
| 238 |
+
log_message("β¨ Audio generated successfully")
|
| 239 |
+
return output
|
|
|
|
|
|
|
|
|
|
| 240 |
except Exception as e:
|
| 241 |
log_message(f"β Audio generation failed: {str(e)}", "error")
|
| 242 |
raise
|
| 243 |
|
| 244 |
+
# ... [Rest of the code including main() function remains the same] ...
|
| 245 |
def generate_hashtags(tradition):
|
| 246 |
"""Generate relevant hashtags"""
|
| 247 |
base_tags = ["#spirituality", "#mindfulness", "#wisdom", "#inspiration", "#meditation"]
|