Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,144 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import googleapiclient.discovery
|
| 3 |
+
import google.generativeai as genai
|
| 4 |
+
from datetime import datetime, timedelta
|
| 5 |
+
|
| 6 |
+
# Configure page
|
| 7 |
+
st.set_page_config(
|
| 8 |
+
page_title="YouTube Content Strategist",
|
| 9 |
+
page_icon="π",
|
| 10 |
+
layout="wide",
|
| 11 |
+
initial_sidebar_state="expanded"
|
| 12 |
+
)
|
| 13 |
+
|
| 14 |
+
# Custom CSS styling
|
| 15 |
+
st.markdown("""
|
| 16 |
+
<style>
|
| 17 |
+
.header {
|
| 18 |
+
font-size: 2.5em !important;
|
| 19 |
+
color: #FF4B4B !important;
|
| 20 |
+
margin-bottom: 30px !important;
|
| 21 |
+
}
|
| 22 |
+
.sidebar .sidebar-content {
|
| 23 |
+
background-color: #F0F2F6;
|
| 24 |
+
}
|
| 25 |
+
.stProgress > div > div > div > div {
|
| 26 |
+
background-color: #FF4B4B;
|
| 27 |
+
}
|
| 28 |
+
.day-card {
|
| 29 |
+
padding: 20px;
|
| 30 |
+
border-radius: 10px;
|
| 31 |
+
box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
|
| 32 |
+
margin-bottom: 20px;
|
| 33 |
+
background-color: white;
|
| 34 |
+
}
|
| 35 |
+
</style>
|
| 36 |
+
""", unsafe_allow_html=True)
|
| 37 |
+
|
| 38 |
+
# Initialize session state
|
| 39 |
+
if 'content_plan' not in st.session_state:
|
| 40 |
+
st.session_state.content_plan = None
|
| 41 |
+
|
| 42 |
+
# Sidebar configuration
|
| 43 |
+
with st.sidebar:
|
| 44 |
+
st.header("Configuration βοΈ")
|
| 45 |
+
YOUTUBE_API_KEY = st.text_input("YouTube API Key", type="password")
|
| 46 |
+
GEMINI_API_KEY = st.text_input("Gemini API Key", type="password")
|
| 47 |
+
MAX_VIDEOS = st.slider("Max Videos to Analyze", 1, 20, 10)
|
| 48 |
+
COMMENTS_PER_VIDEO = st.slider("Comments per Video", 10, 100, 50)
|
| 49 |
+
|
| 50 |
+
# Main content
|
| 51 |
+
st.markdown('<div class="header">π₯ YouTube Content Strategist</div>', unsafe_allow_html=True)
|
| 52 |
+
st.write("Generate data-driven content plans using YouTube audience insights")
|
| 53 |
+
|
| 54 |
+
# Helper functions (keep the same functions from previous code)
|
| 55 |
+
# ... [Include all the previous helper functions here] ...
|
| 56 |
+
|
| 57 |
+
# Analysis execution
|
| 58 |
+
channel_url = st.text_input("Enter YouTube Channel URL:", placeholder="https://www.youtube.com/@ChannelName")
|
| 59 |
+
|
| 60 |
+
if st.button("Generate Content Plan π"):
|
| 61 |
+
if not all([YOUTUBE_API_KEY, GEMINI_API_KEY, channel_url]):
|
| 62 |
+
st.error("Please fill all required fields!")
|
| 63 |
+
else:
|
| 64 |
+
try:
|
| 65 |
+
# Initialize APIs
|
| 66 |
+
youtube = googleapiclient.discovery.build("youtube", "v3", developerKey=YOUTUBE_API_KEY)
|
| 67 |
+
genai.configure(api_key=GEMINI_API_KEY)
|
| 68 |
+
model = genai.GenerativeModel('gemini-1.5-pro')
|
| 69 |
+
|
| 70 |
+
# Get channel data
|
| 71 |
+
with st.spinner("π Analyzing channel..."):
|
| 72 |
+
channel_id = get_channel_id(channel_url)
|
| 73 |
+
if not channel_id:
|
| 74 |
+
st.error("Invalid channel URL")
|
| 75 |
+
st.stop()
|
| 76 |
+
|
| 77 |
+
video_ids = get_channel_videos(channel_id)
|
| 78 |
+
|
| 79 |
+
# Get comments
|
| 80 |
+
all_comments = []
|
| 81 |
+
progress_bar = st.progress(0)
|
| 82 |
+
for idx, video_id in enumerate(video_ids):
|
| 83 |
+
progress = (idx + 1) / len(video_ids)
|
| 84 |
+
progress_bar.progress(progress, text=f"π₯ Collecting comments from video {idx+1}/{len(video_ids)}...")
|
| 85 |
+
all_comments.extend(get_video_comments(video_id))
|
| 86 |
+
|
| 87 |
+
# Generate content plan
|
| 88 |
+
with st.spinner("π§ Analyzing comments and generating plan..."):
|
| 89 |
+
content_plan = analyze_comments(all_comments)
|
| 90 |
+
st.session_state.content_plan = content_plan
|
| 91 |
+
progress_bar.empty()
|
| 92 |
+
|
| 93 |
+
except Exception as e:
|
| 94 |
+
st.error(f"Error: {str(e)}")
|
| 95 |
+
|
| 96 |
+
# Display results
|
| 97 |
+
if st.session_state.content_plan:
|
| 98 |
+
st.markdown("## π
10-Day Content Plan")
|
| 99 |
+
st.success("Here's your personalized content strategy based on audience insights!")
|
| 100 |
+
|
| 101 |
+
# Create date cards
|
| 102 |
+
start_date = datetime.now()
|
| 103 |
+
for day in range(10):
|
| 104 |
+
current_date = start_date + timedelta(days=day)
|
| 105 |
+
with st.expander(f"Day {day+1} - {current_date.strftime('%b %d')}", expanded=True if day==0 else False):
|
| 106 |
+
st.markdown(f"""
|
| 107 |
+
<div class="day-card">
|
| 108 |
+
<h3>{st.session_state.content_plan.split('\n')[day*5]}</h3>
|
| 109 |
+
<p>π― Objective: {st.session_state.content_plan.split('\n')[day*5+1]}</p>
|
| 110 |
+
<p>πΉ Format: {st.session_state.content_plan.split('\n')[day*5+2]}</p>
|
| 111 |
+
<p>π‘ Engagement Strategy: {st.session_state.content_plan.split('\n')[day*5+3]}</p>
|
| 112 |
+
<p>π Success Metrics: {st.session_state.content_plan.split('\n')[day*5+4]}</p>
|
| 113 |
+
</div>
|
| 114 |
+
""", unsafe_allow_html=True)
|
| 115 |
+
|
| 116 |
+
# Download button
|
| 117 |
+
st.download_button(
|
| 118 |
+
label="π₯ Download Full Plan",
|
| 119 |
+
data=st.session_state.content_plan,
|
| 120 |
+
file_name=f"content_plan_{datetime.now().strftime('%Y%m%d')}.txt",
|
| 121 |
+
mime="text/plain"
|
| 122 |
+
)
|
| 123 |
+
|
| 124 |
+
# Instructions
|
| 125 |
+
with st.expander("βΉοΈ How to use this tool"):
|
| 126 |
+
st.markdown("""
|
| 127 |
+
1. **Get API Keys**:
|
| 128 |
+
- YouTube Data API: [Get it here](https://console.cloud.google.com/)
|
| 129 |
+
- Gemini API: [Get it here](https://makersuite.google.com/)
|
| 130 |
+
|
| 131 |
+
2. **Enter Channel URL**:
|
| 132 |
+
- Supports both channel IDs (@ChannelName) and custom URLs
|
| 133 |
+
|
| 134 |
+
3. **Adjust Settings**:
|
| 135 |
+
- Control number of videos/comments analyzed in sidebar
|
| 136 |
+
|
| 137 |
+
4. **Generate Plan**:
|
| 138 |
+
- Click the rocket button to create your strategy
|
| 139 |
+
|
| 140 |
+
5. **Implement & Track**:
|
| 141 |
+
- Download the plan and track performance in YouTube Analytics
|
| 142 |
+
""")
|
| 143 |
+
|
| 144 |
+
# Run with: streamlit run app.py
|