Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -3,34 +3,39 @@ import os
|
|
| 3 |
from datetime import datetime
|
| 4 |
import json
|
| 5 |
from crewai import Agent, Task, Crew
|
| 6 |
-
from langchain.tools import
|
| 7 |
from langchain_community.tools import DuckDuckGoSearchRun
|
| 8 |
from langchain_openai import OpenAI
|
| 9 |
from openai import OpenAI as OpenAIClient
|
|
|
|
|
|
|
| 10 |
import logging
|
| 11 |
import requests
|
| 12 |
import replicate
|
|
|
|
| 13 |
|
| 14 |
# Set up logging
|
| 15 |
logging.basicConfig(level=logging.INFO)
|
| 16 |
logger = logging.getLogger(__name__)
|
| 17 |
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
|
|
|
|
|
|
| 21 |
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
|
| 28 |
class ContentGenerator:
|
| 29 |
def __init__(self, openai_api_key, replicate_api_key):
|
| 30 |
self.llm = OpenAI(api_key=openai_api_key, temperature=0.7)
|
| 31 |
self.openai_client = OpenAIClient(api_key=openai_api_key)
|
| 32 |
self.replicate_client = replicate.Client(api_token=replicate_api_key)
|
| 33 |
-
self.search_tool =
|
| 34 |
|
| 35 |
def generate_quote(self):
|
| 36 |
"""Generate a new spiritual quote using CrewAI"""
|
|
@@ -165,8 +170,130 @@ def main():
|
|
| 165 |
st.error("Replicate API token not found in secrets!")
|
| 166 |
st.stop()
|
| 167 |
|
| 168 |
-
#
|
| 169 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 170 |
|
| 171 |
if __name__ == "__main__":
|
| 172 |
main()
|
|
|
|
| 3 |
from datetime import datetime
|
| 4 |
import json
|
| 5 |
from crewai import Agent, Task, Crew
|
| 6 |
+
from langchain.tools import BaseTool
|
| 7 |
from langchain_community.tools import DuckDuckGoSearchRun
|
| 8 |
from langchain_openai import OpenAI
|
| 9 |
from openai import OpenAI as OpenAIClient
|
| 10 |
+
from typing import Optional, Type
|
| 11 |
+
from pydantic import Field
|
| 12 |
import logging
|
| 13 |
import requests
|
| 14 |
import replicate
|
| 15 |
+
from io import BytesIO
|
| 16 |
|
| 17 |
# Set up logging
|
| 18 |
logging.basicConfig(level=logging.INFO)
|
| 19 |
logger = logging.getLogger(__name__)
|
| 20 |
|
| 21 |
+
class SearchTool(BaseTool):
|
| 22 |
+
name: str = "Search"
|
| 23 |
+
description: str = "Search the internet for information"
|
| 24 |
+
return_direct: bool = False
|
| 25 |
+
search: DuckDuckGoSearchRun = Field(default_factory=DuckDuckGoSearchRun)
|
| 26 |
|
| 27 |
+
def _run(self, query: str) -> str:
|
| 28 |
+
return self.search.run(query)
|
| 29 |
+
|
| 30 |
+
def _arun(self, query: str) -> str:
|
| 31 |
+
raise NotImplementedError("Async not implemented")
|
| 32 |
|
| 33 |
class ContentGenerator:
|
| 34 |
def __init__(self, openai_api_key, replicate_api_key):
|
| 35 |
self.llm = OpenAI(api_key=openai_api_key, temperature=0.7)
|
| 36 |
self.openai_client = OpenAIClient(api_key=openai_api_key)
|
| 37 |
self.replicate_client = replicate.Client(api_token=replicate_api_key)
|
| 38 |
+
self.search_tool = SearchTool()
|
| 39 |
|
| 40 |
def generate_quote(self):
|
| 41 |
"""Generate a new spiritual quote using CrewAI"""
|
|
|
|
| 170 |
st.error("Replicate API token not found in secrets!")
|
| 171 |
st.stop()
|
| 172 |
|
| 173 |
+
# Tabs
|
| 174 |
+
tab1, tab2 = st.tabs(["Generate Content", "Instagram Preview"])
|
| 175 |
+
|
| 176 |
+
with tab1:
|
| 177 |
+
st.title("ποΈ Spiritual Content Generator")
|
| 178 |
+
|
| 179 |
+
if st.button("Generate New Content", key="generate_button"):
|
| 180 |
+
try:
|
| 181 |
+
with st.spinner("Generating content..."):
|
| 182 |
+
generator = ContentGenerator(
|
| 183 |
+
st.secrets["OPENAI_API_KEY"],
|
| 184 |
+
st.secrets["REPLICATE_API_TOKEN"]
|
| 185 |
+
)
|
| 186 |
+
|
| 187 |
+
# Generate quote
|
| 188 |
+
with st.status("Generating quote...") as status:
|
| 189 |
+
quote_data = generator.generate_quote()
|
| 190 |
+
st.session_state.generated_content = quote_data
|
| 191 |
+
status.update(label="Quote generated!", state="complete")
|
| 192 |
+
|
| 193 |
+
# Generate image
|
| 194 |
+
with st.status("Generating image...") as status:
|
| 195 |
+
image_url = generator.generate_image(
|
| 196 |
+
quote_data["text"],
|
| 197 |
+
quote_data["tradition"]
|
| 198 |
+
)
|
| 199 |
+
st.session_state.generated_image = image_url
|
| 200 |
+
status.update(label="Image generated!", state="complete")
|
| 201 |
+
|
| 202 |
+
# Generate audio
|
| 203 |
+
with st.status("Generating audio...") as status:
|
| 204 |
+
audio_url = generator.generate_audio(quote_data["tradition"])
|
| 205 |
+
st.session_state.generated_audio = audio_url
|
| 206 |
+
status.update(label="Audio generated!", state="complete")
|
| 207 |
+
|
| 208 |
+
st.success("All content generated successfully!")
|
| 209 |
+
|
| 210 |
+
except Exception as e:
|
| 211 |
+
st.error(f"Error during generation: {str(e)}")
|
| 212 |
+
|
| 213 |
+
# Display generated content
|
| 214 |
+
if st.session_state.generated_content:
|
| 215 |
+
st.json(st.session_state.generated_content)
|
| 216 |
+
|
| 217 |
+
with tab2:
|
| 218 |
+
st.title("Instagram Preview")
|
| 219 |
+
|
| 220 |
+
if all([st.session_state.generated_content,
|
| 221 |
+
st.session_state.generated_image,
|
| 222 |
+
st.session_state.generated_audio]):
|
| 223 |
+
|
| 224 |
+
col1, col2 = st.columns([2, 1])
|
| 225 |
+
|
| 226 |
+
with col1:
|
| 227 |
+
# Display generated image with quote overlay
|
| 228 |
+
st.image(st.session_state.generated_image, use_column_width=True)
|
| 229 |
+
|
| 230 |
+
# Quote overlay
|
| 231 |
+
quote = st.session_state.generated_content
|
| 232 |
+
st.markdown(f"""
|
| 233 |
+
<div style='
|
| 234 |
+
background-color: rgba(255,255,255,0.8);
|
| 235 |
+
padding: 20px;
|
| 236 |
+
border-radius: 10px;
|
| 237 |
+
margin: 10px 0;
|
| 238 |
+
text-align: center;
|
| 239 |
+
'>
|
| 240 |
+
<h2>{quote['text']}</h2>
|
| 241 |
+
<p>- {quote['author']}</p>
|
| 242 |
+
</div>
|
| 243 |
+
""", unsafe_allow_html=True)
|
| 244 |
+
|
| 245 |
+
with col2:
|
| 246 |
+
# Audio preview
|
| 247 |
+
st.subheader("π΅ Background Music")
|
| 248 |
+
if st.session_state.generated_audio:
|
| 249 |
+
st.audio(st.session_state.generated_audio, format='audio/mp3')
|
| 250 |
+
|
| 251 |
+
# Caption
|
| 252 |
+
st.subheader("π Caption")
|
| 253 |
+
hashtags = generate_hashtags(quote['tradition'])
|
| 254 |
+
caption = f"""β¨ Wisdom from {quote['tradition']} β¨
|
| 255 |
+
|
| 256 |
+
"{quote['text']}"
|
| 257 |
+
- {quote['author']}
|
| 258 |
+
|
| 259 |
+
{' '.join(hashtags)}"""
|
| 260 |
+
|
| 261 |
+
st.text_area("Instagram Caption", caption, height=200)
|
| 262 |
+
|
| 263 |
+
# Download buttons
|
| 264 |
+
col1, col2 = st.columns(2)
|
| 265 |
+
with col1:
|
| 266 |
+
if st.session_state.generated_image:
|
| 267 |
+
image_data = requests.get(st.session_state.generated_image).content
|
| 268 |
+
st.download_button(
|
| 269 |
+
"π₯ Download Image",
|
| 270 |
+
data=image_data,
|
| 271 |
+
file_name="spiritual_quote.jpg",
|
| 272 |
+
mime="image/jpeg"
|
| 273 |
+
)
|
| 274 |
+
|
| 275 |
+
with col2:
|
| 276 |
+
if st.session_state.generated_audio:
|
| 277 |
+
audio_data = requests.get(st.session_state.generated_audio).content
|
| 278 |
+
st.download_button(
|
| 279 |
+
"π₯ Download Audio",
|
| 280 |
+
data=audio_data,
|
| 281 |
+
file_name="background_music.mp3",
|
| 282 |
+
mime="audio/mp3"
|
| 283 |
+
)
|
| 284 |
+
|
| 285 |
+
# Posting tips
|
| 286 |
+
st.markdown("""
|
| 287 |
+
### π± Posting Tips
|
| 288 |
+
1. Download the image and audio
|
| 289 |
+
2. Copy the generated caption
|
| 290 |
+
3. Best posting times: 8-10 AM or 6-8 PM
|
| 291 |
+
4. Engage with your audience about the quote's meaning
|
| 292 |
+
5. Consider creating a Reel with the image and background music
|
| 293 |
+
""")
|
| 294 |
+
|
| 295 |
+
else:
|
| 296 |
+
st.info("Generate content first to see the Instagram preview")
|
| 297 |
|
| 298 |
if __name__ == "__main__":
|
| 299 |
main()
|