File size: 8,166 Bytes
381fba2 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 |
import streamlit as st
import os
import tempfile
import gc
import base64
import time
import yaml
from tqdm import tqdm
from datetime import datetime
from typing import Optional
from crawl4ai_scrapper import scrape_multiple_channels
from crewai import Agent, Crew, Process, Task, LLM
from crewai_tools import FileReadTool
from dotenv import load_dotenv
load_dotenv()
# ===========================
# Cerebras LLM Integration
# ===========================
class CerebrasLLM(LLM):
def __init__(self, model: str, api_key: str, base_url: str, **kwargs):
from llama_index.llms.cerebras import Cerebras
self.client = Cerebras(
model=model,
api_key=api_key,
base_url=base_url,
**kwargs
)
def generate(self, prompt: str, **kwargs) -> str:
response = self.client.complete(prompt, **kwargs)
return response.text
@st.cache_resource
def load_llm() -> CerebrasLLM:
return CerebrasLLM(
model="llama-3.3-70b",
api_key=os.getenv("CEREBRAS_API_KEY"),
base_url="https://api.cerebras.ai/v1",
temperature=0.7,
max_tokens=4096,
top_p=0.95,
timeout=30
)
# ===========================
# Core Application Logic
# ===========================
class YouTubeAnalyzer:
def __init__(self):
self.docs_tool = FileReadTool()
self.llm = load_llm()
def create_crew(self):
with open("config.yaml", 'r') as file:
config = yaml.safe_load(file)
analysis_agent = Agent(
role=config["agents"][0]["role"],
goal=config["agents"][0]["goal"],
backstory=config["agents"][0]["backstory"],
verbose=True,
tools=[self.docs_tool],
llm=self.llm,
memory=True
)
synthesis_agent = Agent(
role=config["agents"][1]["role"],
goal=config["agents"][1]["goal"],
backstory=config["agents"][1]["backstory"],
verbose=True,
llm=self.llm,
allow_delegation=False
)
analysis_task = Task(
description=config["tasks"][0]["description"],
expected_output=config["tasks"][0]["expected_output"],
agent=analysis_agent,
output_file="analysis_raw.md"
)
synthesis_task = Task(
description=config["tasks"][1]["description"],
expected_output=config["tasks"][1]["expected_output"],
agent=synthesis_agent,
context=[analysis_task],
output_file="final_report.md"
)
return Crew(
agents=[analysis_agent, synthesis_agent],
tasks=[analysis_task, synthesis_task],
process=Process.sequential,
verbose=2
)
# ===========================
# Streamlit Interface
# ===========================
class StreamlitApp:
def __init__(self):
self.analyzer = YouTubeAnalyzer()
self._init_session_state()
def _init_session_state(self):
if "response" not in st.session_state:
st.session_state.response = None
if "crew" not in st.session_state:
st.session_state.crew = None
if "youtube_channels" not in st.session_state:
st.session_state.youtube_channels = [""]
def _setup_sidebar(self):
with st.sidebar:
st.header("YouTube Analysis Configuration")
# Channel Management
for i, channel in enumerate(st.session_state.youtube_channels):
cols = st.columns([6, 1])
with cols[0]:
url = st.text_input(
"Channel URL",
value=channel,
key=f"channel_{i}",
help="Example: https://www.youtube.com/@ChannelName"
)
with cols[1]:
if i > 0 and st.button("β", key=f"remove_{i}"):
st.session_state.youtube_channels.pop(i)
st.rerun()
st.button("Add Channel β", on_click=lambda: st.session_state.youtube_channels.append(""))
# Date Selection
st.divider()
st.subheader("Analysis Period")
self.start_date = st.date_input("Start Date", key="start_date")
self.end_date = st.date_input("End Date", key="end_date")
# Analysis Control
st.divider()
if st.button("π Start Analysis", type="primary"):
self._trigger_analysis()
def _trigger_analysis(self):
with st.spinner('Initializing deep content analysis...'):
try:
valid_urls = [
url for url in st.session_state.youtube_channels
if self._is_valid_youtube_url(url)
]
if not valid_urls:
st.error("Please provide at least one valid YouTube channel URL")
return
# Scrape and process data
channel_data = asyncio.run(
scrape_multiple_channels(
valid_urls,
start_date=self.start_date.strftime("%Y-%m-%d"),
end_date=self.end_date.strftime("%Y-%m-%d")
)
)
# Save transcripts
self._save_transcripts(channel_data)
# Execute analysis
with st.spinner('Running AI-powered analysis...'):
st.session_state.crew = self.analyzer.create_crew()
st.session_state.response = st.session_state.crew.kickoff(
inputs={"files": st.session_state.all_files}
)
except Exception as e:
st.error(f"Analysis failed: {str(e)}")
st.stop()
def _save_transcripts(self, channel_data):
st.session_state.all_files = []
os.makedirs("transcripts", exist_ok=True)
with tqdm(total=sum(len(ch) for ch in channel_data), desc="Processing Videos") as pbar:
for channel in channel_data:
for video in channel:
file_path = f"transcripts/{video['id']}.txt"
with open(file_path, "w") as f:
f.write("\n".join(
[f"[{seg['start']}-{seg['end']}] {seg['text']}"
for seg in video['transcript']]
))
st.session_state.all_files.append(file_path)
pbar.update(1)
def _display_results(self):
st.markdown("## Analysis Report")
with st.expander("View Full Technical Analysis"):
st.markdown(st.session_state.response)
col1, col2 = st.columns([3, 1])
with col1:
st.download_button(
label="π₯ Download Full Report",
data=st.session_state.response,
file_name="youtube_analysis_report.md",
mime="text/markdown"
)
with col2:
if st.button("π New Analysis"):
gc.collect()
st.session_state.response = None
st.rerun()
@staticmethod
def _is_valid_youtube_url(url: str) -> bool:
return any(pattern in url for pattern in ["youtube.com/", "youtu.be/"])
def run(self):
# Move st.set_page_config to the top to fix the error
st.set_page_config(page_title="YouTube Intelligence System", layout="wide") # First Streamlit command
st.title("YouTube Content Analysis Platform")
st.markdown("---")
self._setup_sidebar()
if st.session_state.response:
self._display_results()
else:
st.info("Configure analysis parameters in the sidebar to begin") |