AmnaHassan commited on
Commit
3a9f730
·
verified ·
1 Parent(s): 03dac85

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +32 -27
app.py CHANGED
@@ -1,31 +1,30 @@
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
 
@@ -36,17 +35,21 @@ def create_crew_agents(transcript_text):
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")
@@ -54,3 +57,5 @@ if st.button("Generate Blog"):
54
 
55
  except Exception as e:
56
  st.error(f"Error: {e}")
 
 
 
 
1
  import streamlit as st
2
+ from pytube import YouTube
3
  from crewai import Agent, Task, Crew
4
  from transformers import pipeline
5
  import torch
6
+ import whisper
 
 
7
 
8
  st.title("AI Blog Writer from YouTube")
9
 
10
+ # Whisper model setup (Base Model for better speed)
11
+ whisper_model = whisper.load_model("base")
12
 
13
+ def download_audio(video_url):
14
+ """Downloads audio from the YouTube video."""
15
+ yt = YouTube(video_url)
16
+ audio_stream = yt.streams.filter(only_audio=True).first()
17
+ audio_path = audio_stream.download(filename="audio.mp4")
18
+ return audio_path
19
+
20
+ def transcribe_audio(audio_path):
21
+ """Transcribes the audio using Whisper AI."""
22
+ transcription = whisper_model.transcribe(audio_path)
23
+ return transcription['text']
24
 
25
+ # CrewAI Setup
26
  def create_crew_agents(transcript_text):
27
+ research_agent = Agent(name="Research Agent", goal="Extract video transcript.")
28
  blog_writer_agent = Agent(name="Blog Writer", goal="Generate blog content.")
29
  editor_agent = Agent(name="Editor", goal="Refine blog for clarity.")
30
 
 
35
  crew = Crew(agents=[research_agent, blog_writer_agent, editor_agent], tasks=[research_task, blog_task, editor_task])
36
  return crew
37
 
38
+ # User Input for Video URL
39
+ video_url = st.text_input("Enter YouTube Video URL:")
40
+
41
  if st.button("Generate Blog"):
42
+ if video_url:
 
43
  try:
44
+ st.info("Downloading audio...")
45
+ audio_path = download_audio(video_url)
46
+ st.success("Audio downloaded successfully!")
47
+
48
+ st.info("Transcribing audio using Whisper...")
49
+ transcript_text = transcribe_audio(audio_path)
50
+ st.text_area("Transcript Extracted", transcript_text, height=200)
51
+
52
+ # Generate Blog with CrewAI
53
  crew = create_crew_agents(transcript_text)
54
  results = crew.kickoff()
55
  st.subheader("Generated Blog")
 
57
 
58
  except Exception as e:
59
  st.error(f"Error: {e}")
60
+ else:
61
+ st.warning("Please enter a valid YouTube URL.")