divython commited on
Commit
5e74ff4
·
verified ·
1 Parent(s): 1707036

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +29 -38
app.py CHANGED
@@ -1,45 +1,36 @@
1
  import gradio as gr
2
- import os
3
- import torch
4
- import gc
5
- from transformers import pipeline, AutoTokenizer, AutoModelForSeq2SeqLM
6
- import tempfile
7
  import yt_dlp
 
 
8
 
9
- summarizer = pipeline("summarization", model="facebook/bart-large-cnn")
10
- asr_pipeline = pipeline("automatic-speech-recognition", model="openai/whisper-base")
11
-
12
- def download_audio(url: str, temp_dir: str) -> str:
13
- output_path = os.path.join(temp_dir, "audio.%(ext)s")
14
- ydl_opts = {
15
- 'format': 'bestaudio/best',
16
- 'outtmpl': output_path,
17
- 'quiet': True,
18
- 'postprocessors': [{
19
- 'key': 'FFmpegExtractAudio',
20
- 'preferredcodec': 'mp3',
21
- 'preferredquality': '192',
22
- }],
23
- }
24
- with yt_dlp.YoutubeDL(ydl_opts) as ydl:
25
- ydl.download([url])
26
- return output_path.replace('%(ext)s', 'mp3')
27
 
28
- def process_video(url: str) -> str:
29
- with tempfile.TemporaryDirectory() as tmpdir:
30
- audio_path = download_audio(url, tmpdir)
31
- transcription_result = asr_pipeline(audio_path)
32
- text = transcription_result['text']
33
- if len(text.strip()) < 50:
34
- return "Transcription too short or unclear"
35
- gc.collect()
36
- if torch.cuda.is_available():
37
- torch.cuda.empty_cache()
38
- summary_result = summarizer(text, max_length=150, min_length=50, do_sample=False)
39
- return summary_result[0]['summary_text']
40
 
41
  def main(url):
42
- return process_video(url)
 
 
 
 
43
 
44
- iface = gr.Interface(fn=main, inputs="text", outputs="text", title="YouTube Audio Summarizer")
45
- iface.launch()
 
1
  import gradio as gr
 
 
 
 
 
2
  import yt_dlp
3
+ import whisper
4
+ import tempfile
5
 
6
+ def download_audio(url, cookies_path):
7
+ with tempfile.NamedTemporaryFile(suffix='.mp3', delete=False) as tmpfile:
8
+ ydl_opts = {
9
+ 'format': 'bestaudio/best',
10
+ 'outtmpl': tmpfile.name,
11
+ 'quiet': True,
12
+ 'cookiefile': cookies_path,
13
+ 'postprocessors': [{
14
+ 'key': 'FFmpegExtractAudio',
15
+ 'preferredcodec': 'mp3',
16
+ 'preferredquality': '192',
17
+ }]
18
+ }
19
+ with yt_dlp.YoutubeDL(ydl_opts) as ydl:
20
+ ydl.download([url])
21
+ return tmpfile.name
 
 
22
 
23
+ def process_video(url, cookies_path):
24
+ audio_file = download_audio(url, cookies_path)
25
+ model = whisper.load_model("base")
26
+ result = model.transcribe(audio_file)
27
+ return result['text']
 
 
 
 
 
 
 
28
 
29
  def main(url):
30
+ cookies_path = 'cookies.txt' # Provide path to your exported cookies file
31
+ transcript = process_video(url, cookies_path)
32
+ return transcript
33
+
34
+ demo = gr.Interface(fn=main, inputs=gr.Textbox(label="YouTube URL"), outputs="text")
35
 
36
+ demo.launch()