dtkne commited on
Commit
d9c9d47
·
verified ·
1 Parent(s): 5723f88

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +16 -21
app.py CHANGED
@@ -1,22 +1,8 @@
1
  import gradio as gr
2
- import torch
3
  import os
4
- import subprocess
5
  from pytubefix import YouTube
6
  from moviepy.editor import VideoFileClip
7
  from transformers import pipeline
8
- import subprocess
9
- import sys
10
-
11
- # Ensure moviepy is installed
12
- try:
13
- import moviepy.editor
14
- except ImportError:
15
- subprocess.run([sys.executable, "-m", "pip", "install", "moviepy"], check=True)
16
- import moviepy.editor # Retry import after installation
17
-
18
- # Ensure required packages are installed inside Hugging Face Spaces
19
- subprocess.run(["pip", "install", "pytubefix", "moviepy", "transformers", "torchaudio"], check=True)
20
 
21
  # Load Whisper model for transcription
22
  asr = pipeline("automatic-speech-recognition", model="distil-whisper/distil-small.en")
@@ -24,32 +10,41 @@ asr = pipeline("automatic-speech-recognition", model="distil-whisper/distil-smal
24
  # Load Summarization model
25
  summarizer = pipeline("summarization", model="facebook/bart-large-cnn")
26
 
27
-
28
  def process_youtube_link(youtube_url):
29
  try:
30
  # Download YouTube Video
31
  yt = YouTube(youtube_url)
32
- video_stream = yt.streams.filter(only_audio=True).first()
33
- video_path = video_stream.download(filename="video.mp4")
 
 
 
 
 
 
 
34
 
35
  # Extract Audio
36
- audio_path = "audio.wav"
37
  video = VideoFileClip(video_path)
38
- video.audio.write_audiofile(audio_path)
39
 
40
  # Transcribe Audio
41
- transcription = asr(audio_path)
42
  transcribed_text = transcription["text"]
43
 
44
  # Summarize Transcription
45
  summary = summarizer(transcribed_text, max_length=150, min_length=50, do_sample=False)[0]["summary_text"]
46
 
 
 
 
 
47
  return transcribed_text, summary
48
 
49
  except Exception as e:
50
  return f"Error: {str(e)}", ""
51
 
52
-
53
  # Create Gradio Interface
54
  iface = gr.Interface(
55
  fn=process_youtube_link,
 
1
  import gradio as gr
 
2
  import os
 
3
  from pytubefix import YouTube
4
  from moviepy.editor import VideoFileClip
5
  from transformers import pipeline
 
 
 
 
 
 
 
 
 
 
 
 
6
 
7
  # Load Whisper model for transcription
8
  asr = pipeline("automatic-speech-recognition", model="distil-whisper/distil-small.en")
 
10
  # Load Summarization model
11
  summarizer = pipeline("summarization", model="facebook/bart-large-cnn")
12
 
 
13
  def process_youtube_link(youtube_url):
14
  try:
15
  # Download YouTube Video
16
  yt = YouTube(youtube_url)
17
+ title = yt.title
18
+ print(f"Downloading: {title}")
19
+
20
+ video_stream = yt.streams.get_highest_resolution()
21
+ if not video_stream:
22
+ return "Error: No available video stream", ""
23
+
24
+ video_path = f"{title}.mp4"
25
+ video_stream.download(filename=video_path)
26
 
27
  # Extract Audio
28
+ audio_path = f"{title}.wav"
29
  video = VideoFileClip(video_path)
30
+ video.audio.write_audiofile(audio_path, codec="pcm_s16le")
31
 
32
  # Transcribe Audio
33
+ transcription = asr(audio_path, return_timestamps=True)
34
  transcribed_text = transcription["text"]
35
 
36
  # Summarize Transcription
37
  summary = summarizer(transcribed_text, max_length=150, min_length=50, do_sample=False)[0]["summary_text"]
38
 
39
+ # Clean up files after processing
40
+ os.remove(video_path)
41
+ os.remove(audio_path)
42
+
43
  return transcribed_text, summary
44
 
45
  except Exception as e:
46
  return f"Error: {str(e)}", ""
47
 
 
48
  # Create Gradio Interface
49
  iface = gr.Interface(
50
  fn=process_youtube_link,