Spaces:
Runtime error
Runtime error
Commit
·
4b1216b
1
Parent(s):
6740256
Update app.py
Browse files
app.py
CHANGED
@@ -1,26 +1,20 @@
|
|
1 |
# https://huggingface.co/spaces/aadnk/whisper-webui/blob/main/app.py
|
2 |
|
3 |
-
|
4 |
-
|
5 |
-
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
from whisper.utils import write_srt
|
13 |
-
|
14 |
-
MAX_FILE_PREFIX_LENGTH = 17
|
15 |
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
|
20 |
-
|
21 |
-
demo = gr.Blocks(cache_examples=False)
|
22 |
-
except Exception as e:
|
23 |
-
print(e)
|
24 |
|
25 |
def slugify(value, allow_unicode=False):
|
26 |
"""
|
@@ -40,57 +34,33 @@ def slugify(value, allow_unicode=False):
|
|
40 |
|
41 |
async def transcribe(file):
|
42 |
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
except Exception as e:
|
53 |
-
print(e)
|
54 |
-
|
55 |
-
try:
|
56 |
-
file_path = pathlib.Path(file.name)
|
57 |
-
sourceName = file_path.stem[:MAX_FILE_PREFIX_LENGTH] + file_path.suffix
|
58 |
-
filePrefix = slugify(sourceName, allow_unicode=True)
|
59 |
-
except Exception as e:
|
60 |
-
print(e)
|
61 |
|
62 |
#write to file
|
63 |
-
|
64 |
-
|
65 |
-
f.write(result['text'])
|
66 |
|
67 |
-
except Exception as e:
|
68 |
-
print(e)
|
69 |
-
|
70 |
#subtitles
|
71 |
-
|
72 |
-
|
73 |
-
write_srt(result["segments"], file=srt)
|
74 |
-
|
75 |
-
download = []
|
76 |
-
download.append(filePrefix + "-subs.srt");
|
77 |
-
download.append(filePrefix + "-transcript.txt");à
|
78 |
|
79 |
-
|
|
|
|
|
80 |
|
81 |
-
|
82 |
-
print(e)
|
83 |
-
|
84 |
-
try:
|
85 |
-
with demo:
|
86 |
-
audio_file = gr.File()
|
87 |
-
transcript = gr.File(label="transcript")
|
88 |
-
b1 = gr.Button("Transcrire")
|
89 |
-
b1.click(transcribe, inputs=audio_file, outputs=transcript)
|
90 |
-
except Exception as e:
|
91 |
-
print(e)
|
92 |
|
93 |
-
|
94 |
-
|
95 |
-
|
96 |
-
|
|
|
|
|
|
1 |
# https://huggingface.co/spaces/aadnk/whisper-webui/blob/main/app.py
|
2 |
|
3 |
+
import gradio as gr
|
4 |
+
import os
|
5 |
+
import re
|
6 |
+
import unicodedata
|
7 |
+
import pathlib
|
8 |
+
import asyncio
|
9 |
+
|
10 |
+
import whisper
|
11 |
+
from whisper.utils import write_srt
|
|
|
|
|
|
|
12 |
|
13 |
+
MAX_FILE_PREFIX_LENGTH = 17
|
14 |
+
|
15 |
+
model = whisper.load_model("base")
|
16 |
|
17 |
+
demo = gr.Blocks(cache_examples=False)
|
|
|
|
|
|
|
18 |
|
19 |
def slugify(value, allow_unicode=False):
|
20 |
"""
|
|
|
34 |
|
35 |
async def transcribe(file):
|
36 |
|
37 |
+
audio = whisper.load_audio(file.name)
|
38 |
+
# transcribe_options = dict(beam_size=5, best_of=5, without_timestamps=False)
|
39 |
+
|
40 |
+
# result = model.transcribe(file, **transcribe_options)
|
41 |
+
result = await model.transcribe(audio)
|
42 |
+
|
43 |
+
file_path = pathlib.Path(file.name)
|
44 |
+
sourceName = file_path.stem[:MAX_FILE_PREFIX_LENGTH] + file_path.suffix
|
45 |
+
filePrefix = slugify(sourceName, allow_unicode=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
46 |
|
47 |
#write to file
|
48 |
+
with open(filePrefix + "-transcript.txt", 'w', encoding="utf-8") as f:
|
49 |
+
f.write(result['text'])
|
|
|
50 |
|
|
|
|
|
|
|
51 |
#subtitles
|
52 |
+
with open(filePrefix + "-subs.srt", 'w', encoding="utf-8") as srt:
|
53 |
+
write_srt(result["segments"], file=srt)
|
|
|
|
|
|
|
|
|
|
|
54 |
|
55 |
+
download = []
|
56 |
+
download.append(filePrefix + "-subs.srt");
|
57 |
+
download.append(filePrefix + "-transcript.txt");à
|
58 |
|
59 |
+
return download
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
60 |
|
61 |
+
with demo:
|
62 |
+
audio_file = gr.File()
|
63 |
+
transcript = gr.File(label="transcript")
|
64 |
+
b1 = gr.Button("Transcrire")
|
65 |
+
b1.click(transcribe, inputs=audio_file, outputs=transcript)
|
66 |
+
demo.launch()
|