barto17 commited on
Commit
a12d0b2
1 Parent(s): 9ca5bac

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +20 -2
app.py CHANGED
@@ -1,4 +1,5 @@
1
  import torch
 
2
  from transformers import AutoModelForSequenceClassification, AutoTokenizer
3
  from transformers.pipelines.audio_utils import ffmpeg_read
4
  import gradio as gr
@@ -73,16 +74,33 @@ def transcribe(Microphone, File_Upload, URL):
73
  elif Microphone is not None:
74
  file = Microphone
75
 
 
 
 
76
  elif URL:
77
  link = YouTube(URL)
78
- file = link.streams.filter(only_audio=True)[0].download(filename="audio.mp4")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
79
  else:
80
  file = File_Upload
81
 
82
 
83
  language = None
84
 
85
- options = whisper.DecodingOptions(without_timestamps=True)
86
 
87
  loaded_model = whisper.load_model("base")
88
  transcript = loaded_model.transcribe(file, language=language)
 
1
  import torch
2
+ from pydub import AudioSegment
3
  from transformers import AutoModelForSequenceClassification, AutoTokenizer
4
  from transformers.pipelines.audio_utils import ffmpeg_read
5
  import gradio as gr
 
74
  elif Microphone is not None:
75
  file = Microphone
76
 
77
+ #elif URL:
78
+ # link = YouTube(URL)
79
+ # file = link.streams.filter(only_audio=True)[0].download(filename="audio.mp3")
80
  elif URL:
81
  link = YouTube(URL)
82
+ stream = link.streams.filter(only_audio=True).first()
83
+
84
+ # Download the audio file with a temporary filename
85
+ temp_filename = "temp_audio_file"
86
+ stream.download(filename=temp_filename)
87
+
88
+ # Load the downloaded file with pydub and convert it to mp3
89
+ audio = AudioSegment.from_file(temp_filename, format="mp4")
90
+
91
+ # Truncate it to the first 30 seconds
92
+ truncated_audio = audio[:30000] # AudioSegment works in milliseconds
93
+
94
+ file = "file.mp3"
95
+ truncated_audio.export(file, format="mp3")
96
+
97
  else:
98
  file = File_Upload
99
 
100
 
101
  language = None
102
 
103
+ options = whisper.DecodingOptions()
104
 
105
  loaded_model = whisper.load_model("base")
106
  transcript = loaded_model.transcribe(file, language=language)