Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -3,36 +3,9 @@ select_model ="base" # ['tiny', 'base']
|
|
3 |
model = whisper.load_model(select_model)
|
4 |
|
5 |
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
import uuid
|
10 |
-
import re
|
11 |
-
|
12 |
-
def extract_video_id(url):
|
13 |
-
# Regular expression to extract the video ID from different YouTube URL formats
|
14 |
-
pattern = r"(?:youtu\.be/|youtube(?:-nocookie)?\.com/(?:embed/|v/|shorts/|watch\?v=|watch\?.+&v=))([\w-]+)"
|
15 |
-
match = re.search(pattern, url)
|
16 |
-
if match:
|
17 |
-
return match.group(1)
|
18 |
-
return None
|
19 |
-
|
20 |
-
def download_audio(Youtube_Video_Link):
|
21 |
-
video_id = extract_video_id(Youtube_Video_Link)
|
22 |
-
yt_url = f"https://www.youtube.com/watch?v={video_id}"
|
23 |
-
random_uuid = str(uuid.uuid4())[:8]
|
24 |
-
ydl_opts = {
|
25 |
-
'format': 'bestaudio/best',
|
26 |
-
# 'outtmpl': 'output.%(ext)s',
|
27 |
-
'postprocessors': [{
|
28 |
-
'key': 'FFmpegExtractAudio',
|
29 |
-
'preferredcodec': 'mp3',
|
30 |
-
}],
|
31 |
-
"outtmpl": f'{random_uuid}', # this is where you can edit how you'd like the filenames to be formatted
|
32 |
-
}
|
33 |
-
with yt_dlp.YoutubeDL(ydl_opts) as ydl:
|
34 |
-
ydl.download([yt_url])
|
35 |
-
return f"{random_uuid}.mp3"
|
36 |
|
37 |
def store_path_in_json(path, json_file_path="stored_paths.json"):
|
38 |
# Create a dictionary with the path and timestamp
|
@@ -107,41 +80,20 @@ def convert_to_text(audio_path):
|
|
107 |
store_path_in_json(audio_path)
|
108 |
result = model.transcribe(audio_path,fp16=False)
|
109 |
return result["text"]
|
110 |
-
import os
|
111 |
-
def audio_to_text(youtube_link,audio_path):
|
112 |
-
if len(youtube_link)>3:
|
113 |
-
audio_file_path=download_audio(youtube_link)
|
114 |
-
audio_file_path=os.getcwd()+"/"+audio_file_path
|
115 |
-
text=convert_to_text(audio_file_path)
|
116 |
-
return text
|
117 |
-
if os.path.exists(audio_path):
|
118 |
-
text=convert_to_text(audio_path)
|
119 |
-
return text
|
120 |
|
121 |
|
122 |
import gradio as gr
|
123 |
import os
|
124 |
|
125 |
|
126 |
-
def transcribe_audio(
|
127 |
-
|
128 |
-
result = audio_to_text(youtube_link, "None")
|
129 |
-
elif audio_file:
|
130 |
-
if os.path.exists(audio_file):
|
131 |
-
result = audio_to_text("None",audio_file)
|
132 |
-
else:
|
133 |
-
result = "Please provide a YouTube link or upload an audio file."
|
134 |
-
|
135 |
return result
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
136 |
|
137 |
-
iface = gr.Interface(
|
138 |
-
fn=transcribe_audio,
|
139 |
-
inputs=[
|
140 |
-
gr.Textbox(),
|
141 |
-
gr.File()
|
142 |
-
],
|
143 |
-
outputs="text",
|
144 |
-
live=True
|
145 |
-
)
|
146 |
-
|
147 |
-
iface.launch()
|
|
|
3 |
model = whisper.load_model(select_model)
|
4 |
|
5 |
|
6 |
+
|
7 |
+
|
8 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
9 |
|
10 |
def store_path_in_json(path, json_file_path="stored_paths.json"):
|
11 |
# Create a dictionary with the path and timestamp
|
|
|
80 |
store_path_in_json(audio_path)
|
81 |
result = model.transcribe(audio_path,fp16=False)
|
82 |
return result["text"]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
83 |
|
84 |
|
85 |
import gradio as gr
|
86 |
import os
|
87 |
|
88 |
|
89 |
+
def transcribe_audio(audio_file):
|
90 |
+
result=convert_to_text(audio_file)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
91 |
return result
|
92 |
+
demo = gr.Interface(process_audio,
|
93 |
+
[gr.File(label="Upload Audio")],
|
94 |
+
[gr.Textbox(label="Transcribe")],
|
95 |
+
)
|
96 |
+
|
97 |
+
demo.launch()
|
98 |
+
|
99 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|