AndrewRWilliams commited on
Commit
4b1216b
·
1 Parent(s): 6740256

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +36 -66
app.py CHANGED
@@ -1,26 +1,20 @@
1
  # https://huggingface.co/spaces/aadnk/whisper-webui/blob/main/app.py
2
 
3
- try:
4
- import gradio as gr
5
- import os
6
- import re
7
- import unicodedata
8
- import pathlib
9
- import asyncio
10
-
11
- import whisper
12
- from whisper.utils import write_srt
13
-
14
- MAX_FILE_PREFIX_LENGTH = 17
15
 
16
- model = whisper.load_model("base")
17
- except:
18
- print("Error in imports or loading model")
19
 
20
- try:
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
- try:
44
- audio = whisper.load_audio(file.name)
45
- except Exception as e:
46
- print(e)
47
- # transcribe_options = dict(beam_size=5, best_of=5, without_timestamps=False)
48
-
49
- # result = model.transcribe(file, **transcribe_options)
50
- try:
51
- result = await model.transcribe(audio)
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
- try:
64
- with open(filePrefix + "-transcript.txt", 'w', encoding="utf-8") as f:
65
- f.write(result['text'])
66
 
67
- except Exception as e:
68
- print(e)
69
-
70
  #subtitles
71
- try:
72
- with open(filePrefix + "-subs.srt", 'w', encoding="utf-8") as srt:
73
- write_srt(result["segments"], file=srt)
74
-
75
- download = []
76
- download.append(filePrefix + "-subs.srt");
77
- download.append(filePrefix + "-transcript.txt");à
78
 
79
- return download
 
 
80
 
81
- except Exception as e:
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
- try:
94
- demo.launch()
95
- except Exception as e:
96
- print(e)
 
 
 
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()