cfc-tech commited on
Commit
5f2b8c4
·
verified ·
1 Parent(s): 00844ce
Files changed (1) hide show
  1. app.py +42 -43
app.py CHANGED
@@ -1,44 +1,43 @@
1
  import gradio as gr
2
- from pytube import YouTube
3
- import subprocess
4
- import torch
5
- from huggingsound import SpeechRecognitionModel
6
- import librosa
7
- import soundfile as sf
8
- from transformers import pipeline
9
-
10
- def summarize_video(youtube_link):
11
- # Download YouTube video's audio
12
- yt = YouTube(youtube_link)
13
- yt.streams.filter(only_audio=True, file_extension='mp4').first().download(filename='ytaudio.mp4')
14
-
15
- # Convert to WAV format
16
- subprocess.run(['ffmpeg', '-i', 'ytaudio.mp4', '-acodec', 'pcm_s16le', '-ar', '16000', 'ytaudio.wav'], check=True)
17
-
18
- # Initialize speech recognition model
19
- device = "cuda" if torch.cuda.is_available() else "cpu"
20
- model = SpeechRecognitionModel("jonatasgrosman/wav2vec2-large-xlsr-53-english", device=device)
21
-
22
- # Process audio file and transcribe
23
- input_file = 'ytaudio.wav'
24
- stream = librosa.stream(input_file, block_length=30, frame_length=16000, hop_length=16000)
25
- full_transcript = ''
26
- for i, speech in enumerate(stream):
27
- sf.write(f'{i}.wav', speech, 16000)
28
- transcription = model.transcribe([f'{i}.wav'])
29
- full_transcript += ' '.join([item['transcription'] for item in transcription])
30
-
31
- # Summarize the transcript
32
- summarizer = pipeline('summarization')
33
- summarized_text = summarizer(full_transcript, max_length=130, min_length=30, do_sample=False)
34
- return summarized_text[0]['summary_text']
35
-
36
- # Set up the Gradio interface
37
- iface = gr.Interface(fn=summarize_video,
38
- inputs=gr.inputs.Textbox(lines=2, placeholder="Enter YouTube Video Link Here..."),
39
- outputs="text",
40
- title="YouTube Video Text Summarizer",
41
- description="This tool summarizes the text extracted from a given YouTube video. Please enter the video link below.")
42
-
43
- if __name__ == "__main__":
44
- iface.launch()
 
1
  import gradio as gr
2
+ from transformers import BartForConditionalGeneration, BartTokenizer
3
+ from youtube_transcript_api import YouTubeTranscriptApi
4
+
5
+ # Load BART model and tokenizer
6
+ model_name = 'facebook/bart-large-cnn'
7
+ tokenizer = BartTokenizer.from_pretrained(model_name)
8
+ model = BartForConditionalGeneration.from_pretrained(model_name)
9
+
10
+ def get_transcript(url):
11
+ try:
12
+ video_id = url.split('=')[1]
13
+ transcript_list = YouTubeTranscriptApi.get_transcript(video_id)
14
+ transcript_text = ""
15
+ for item in transcript_list:
16
+ transcript_text += item['text'] + "\n"
17
+ return transcript_text
18
+ except Exception as e:
19
+ return "Error fetching transcript: " + str(e)
20
+
21
+ def summarize_transcript(transcript):
22
+ input_ids = tokenizer.encode("summarize: " + transcript, return_tensors="pt", max_length=1024, truncation=True)
23
+ summary_ids = model.generate(input_ids, num_beams=4, min_length=30, max_length=200, early_stopping=True)
24
+ summary = tokenizer.decode(summary_ids[0], skip_special_tokens=True)
25
+ return summary
26
+
27
+ def summarize_video_url(video_url):
28
+ transcript = get_transcript(video_url)
29
+ if not transcript:
30
+ return "Error fetching transcript."
31
+ else:
32
+ summary = summarize_transcript(transcript)
33
+ return summary
34
+
35
+ inputs = gr.inputs.Textbox(lines=5, label="Enter YouTube Video URL")
36
+ output = gr.outputs.Textbox(label="Summary")
37
+
38
+ title = "YouTube Video Transcription Summarizer"
39
+ description = "Enter a YouTube Video URL to get a summary of its transcript."
40
+
41
+ iface = gr.Interface(fn=summarize_video_url, inputs=inputs, outputs=output, title=title, description=description)
42
+
43
+ iface.launch(share=True)