Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,93 +1,56 @@
|
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
-
from pytube import YouTube
|
| 3 |
from youtube_transcript_api import YouTubeTranscriptApi
|
| 4 |
from crewai import Agent, Task, Crew
|
| 5 |
from transformers import pipeline
|
| 6 |
-
import torch
|
| 7 |
|
| 8 |
-
#
|
| 9 |
summarizer = pipeline("summarization", model="facebook/bart-large-cnn")
|
| 10 |
|
| 11 |
-
|
| 12 |
-
st.title("AI-Powered Blog Writer with CrewAI")
|
| 13 |
-
st.write("Input a YouTube video URL, and AI agents will collaboratively create a blog.")
|
| 14 |
-
|
| 15 |
-
# Input for YouTube URL
|
| 16 |
-
video_url = st.text_input("Enter YouTube Video URL")
|
| 17 |
-
|
| 18 |
-
# Define CrewAI Agents and Tasks
|
| 19 |
-
def create_crew_agents(transcript_text):
|
| 20 |
-
# Research Agent - Extracts transcript
|
| 21 |
-
research_agent = Agent(
|
| 22 |
-
name="Research Agent",
|
| 23 |
-
role="Transcript Extractor",
|
| 24 |
-
goal="Extract transcript from YouTube videos.",
|
| 25 |
-
backstory="Expert in video transcription and data extraction."
|
| 26 |
-
)
|
| 27 |
-
|
| 28 |
-
# Blog Writer Agent - Generates a blog
|
| 29 |
-
blog_writer_agent = Agent(
|
| 30 |
-
name="Blog Writer Agent",
|
| 31 |
-
role="Blog Creator",
|
| 32 |
-
goal="Write an engaging blog post from the transcript.",
|
| 33 |
-
backstory="Creative writer specializing in transforming transcripts into engaging blogs."
|
| 34 |
-
)
|
| 35 |
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
name="Editor Agent",
|
| 39 |
-
role="Content Editor",
|
| 40 |
-
goal="Ensure blog quality by refining the text.",
|
| 41 |
-
backstory="Experienced editor ensuring clarity and quality in written content."
|
| 42 |
-
)
|
| 43 |
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 49 |
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
)
|
| 55 |
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
)
|
| 60 |
|
| 61 |
-
|
| 62 |
-
crew = Crew(
|
| 63 |
-
agents=[research_agent, blog_writer_agent, editor_agent],
|
| 64 |
-
tasks=[research_task, blog_task, editor_task]
|
| 65 |
-
)
|
| 66 |
-
|
| 67 |
return crew
|
| 68 |
|
| 69 |
-
# Button
|
| 70 |
-
if st.button("Generate Blog
|
| 71 |
-
|
|
|
|
| 72 |
try:
|
| 73 |
-
|
| 74 |
-
st.write(f"**Video Title:** {yt.title}")
|
| 75 |
-
|
| 76 |
-
# Extract Transcript
|
| 77 |
-
video_id = yt.video_id
|
| 78 |
transcript = YouTubeTranscriptApi.get_transcript(video_id)
|
| 79 |
transcript_text = " ".join([item['text'] for item in transcript])
|
| 80 |
-
st.text_area("Transcript Extracted", transcript_text
|
| 81 |
-
|
| 82 |
-
#
|
| 83 |
crew = create_crew_agents(transcript_text)
|
| 84 |
-
results = crew.kickoff()
|
| 85 |
-
|
| 86 |
-
# Display the Blog Result
|
| 87 |
-
st.subheader("Final Blog Generated by the Crew")
|
| 88 |
st.write(results)
|
|
|
|
| 89 |
except Exception as e:
|
| 90 |
st.error(f"Error: {e}")
|
| 91 |
-
else:
|
| 92 |
-
st.warning("Please enter a valid YouTube video URL.")
|
| 93 |
-
|
|
|
|
| 1 |
+
|
| 2 |
import streamlit as st
|
|
|
|
| 3 |
from youtube_transcript_api import YouTubeTranscriptApi
|
| 4 |
from crewai import Agent, Task, Crew
|
| 5 |
from transformers import pipeline
|
| 6 |
+
import torch
|
| 7 |
|
| 8 |
+
# Hugging Face summarizer setup
|
| 9 |
summarizer = pipeline("summarization", model="facebook/bart-large-cnn")
|
| 10 |
|
| 11 |
+
st.title("AI Blog Writer from YouTube")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
|
| 13 |
+
# YouTube URL Input
|
| 14 |
+
video_url = st.text_input("Enter YouTube Video URL:")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
|
| 16 |
+
def get_video_id(url):
|
| 17 |
+
"""Extract YouTube video ID from the URL."""
|
| 18 |
+
import re
|
| 19 |
+
match = re.search(r"(?<=v=)[^&]+", url)
|
| 20 |
+
if match:
|
| 21 |
+
return match.group(0)
|
| 22 |
+
else:
|
| 23 |
+
st.error("Invalid YouTube URL. Please try again.")
|
| 24 |
+
return None
|
| 25 |
|
| 26 |
+
# CrewAI Agent Setup
|
| 27 |
+
def create_crew_agents(transcript_text):
|
| 28 |
+
research_agent = Agent(name="Research Agent", goal="Extract transcript from videos.")
|
| 29 |
+
blog_writer_agent = Agent(name="Blog Writer", goal="Generate blog content.")
|
| 30 |
+
editor_agent = Agent(name="Editor", goal="Refine blog for clarity.")
|
| 31 |
|
| 32 |
+
research_task = Task(description="Fetch transcript.", agent=research_agent)
|
| 33 |
+
blog_task = Task(description="Write blog using transcript.", agent=blog_writer_agent, context=transcript_text)
|
| 34 |
+
editor_task = Task(description="Edit the blog.", agent=editor_agent)
|
|
|
|
| 35 |
|
| 36 |
+
crew = Crew(agents=[research_agent, blog_writer_agent, editor_agent], tasks=[research_task, blog_task, editor_task])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 37 |
return crew
|
| 38 |
|
| 39 |
+
# Button Trigger
|
| 40 |
+
if st.button("Generate Blog"):
|
| 41 |
+
video_id = get_video_id(video_url)
|
| 42 |
+
if video_id:
|
| 43 |
try:
|
| 44 |
+
# Fetch Transcript using YouTube API
|
|
|
|
|
|
|
|
|
|
|
|
|
| 45 |
transcript = YouTubeTranscriptApi.get_transcript(video_id)
|
| 46 |
transcript_text = " ".join([item['text'] for item in transcript])
|
| 47 |
+
st.text_area("Transcript Extracted", transcript_text)
|
| 48 |
+
|
| 49 |
+
# CrewAI Agents Collaboration
|
| 50 |
crew = create_crew_agents(transcript_text)
|
| 51 |
+
results = crew.kickoff()
|
| 52 |
+
st.subheader("Generated Blog")
|
|
|
|
|
|
|
| 53 |
st.write(results)
|
| 54 |
+
|
| 55 |
except Exception as e:
|
| 56 |
st.error(f"Error: {e}")
|
|
|
|
|
|
|
|
|