cfc-tech commited on
Commit
04a0f7f
·
verified ·
1 Parent(s): c1f076a
Files changed (1) hide show
  1. app.py +32 -15
app.py CHANGED
@@ -3,38 +3,55 @@ from pytube import YouTube
3
  import subprocess
4
  from huggingsound import SpeechRecognitionModel
5
  import torch
6
- import librosa
7
  from transformers import pipeline
8
 
9
  def process_video(video_url):
 
 
 
 
 
 
10
  try:
11
- # Download audio from YouTube
12
  yt = YouTube(video_url)
13
  audio_file = yt.streams.filter(only_audio=True, file_extension='mp4').first().download(filename='ytaudio.mp4')
14
- except Exception as e:
15
- return f"Error downloading audio from YouTube: {e}"
16
-
17
- try:
18
- # Convert to suitable format for speech recognition
19
  subprocess.run(['ffmpeg', '-i', 'ytaudio.mp4', '-acodec', 'pcm_s16le', '-ar', '16000', 'ytaudio.wav'], check=True)
20
- except subprocess.CalledProcessError as e:
21
- return f"Error converting audio file: {e}"
 
 
22
 
23
  try:
24
- # Speech Recognition
25
  device = "cuda" if torch.cuda.is_available() else "cpu"
26
  model = SpeechRecognitionModel("jonatasgrosman/wav2vec2-large-xlsr-53-english", device=device)
27
  transcription = model.transcribe(['ytaudio.wav'])[0]['transcription']
28
  except Exception as e:
29
- return f"Error in speech recognition: {e}"
 
 
30
 
31
  try:
32
- # Summarize Transcription
33
  summarization = pipeline('summarization')
34
  summarized_text = summarization(transcription, max_length=130, min_length=30, do_sample=False)
35
- return summarized_text[0]['summary_text']
36
  except Exception as e:
37
- return f"Error summarizing text: {e}"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
38
 
39
- iface = gr.Interface(fn=process_video, inputs=gr.inputs.Textbox(lines=2, placeholder="Enter YouTube Video URL Here..."), outputs="text", title="YouTube Video Summarizer", description="This tool extracts audio from a YouTube video, transcribes it, and provides a summary.")
40
  iface.launch()
 
3
  import subprocess
4
  from huggingsound import SpeechRecognitionModel
5
  import torch
 
6
  from transformers import pipeline
7
 
8
  def process_video(video_url):
9
+ response = {
10
+ 'status': 'Success',
11
+ 'message': '',
12
+ 'data': ''
13
+ }
14
+
15
  try:
 
16
  yt = YouTube(video_url)
17
  audio_file = yt.streams.filter(only_audio=True, file_extension='mp4').first().download(filename='ytaudio.mp4')
 
 
 
 
 
18
  subprocess.run(['ffmpeg', '-i', 'ytaudio.mp4', '-acodec', 'pcm_s16le', '-ar', '16000', 'ytaudio.wav'], check=True)
19
+ except Exception as e:
20
+ response['status'] = 'Error'
21
+ response['message'] = f'Failed to download and convert video: {str(e)}'
22
+ return response
23
 
24
  try:
 
25
  device = "cuda" if torch.cuda.is_available() else "cpu"
26
  model = SpeechRecognitionModel("jonatasgrosman/wav2vec2-large-xlsr-53-english", device=device)
27
  transcription = model.transcribe(['ytaudio.wav'])[0]['transcription']
28
  except Exception as e:
29
+ response['status'] = 'Error'
30
+ response['message'] = f'Failed during speech recognition: {str(e)}'
31
+ return response
32
 
33
  try:
 
34
  summarization = pipeline('summarization')
35
  summarized_text = summarization(transcription, max_length=130, min_length=30, do_sample=False)
36
+ response['data'] = summarized_text[0]['summary_text']
37
  except Exception as e:
38
+ response['status'] = 'Error'
39
+ response['message'] = f'Failed during summarization: {str(e)}'
40
+ return response
41
+
42
+ return response
43
+
44
+ iface = gr.Interface(
45
+ fn=process_video,
46
+ inputs=gr.inputs.Textbox(lines=2, placeholder="Enter YouTube Video URL Here..."),
47
+ outputs=[
48
+ gr.outputs.Textbox(label="Status"),
49
+ gr.outputs.Textbox(label="Message"),
50
+ gr.outputs.Textbox(label="Summary")
51
+ ],
52
+ title="YouTube Video Summarizer",
53
+ description="This tool extracts audio from a YouTube video, transcribes it, and provides a summary.",
54
+ enable_queue=True # Enable request queuing
55
+ )
56
 
 
57
  iface.launch()