Spaces:
Runtime error
Runtime error
Greencapabara
commited on
Commit
•
7180851
1
Parent(s):
5ca9759
Upload app.py
Browse files
app.py
ADDED
@@ -0,0 +1,89 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from io import StringIO
|
2 |
+
import gradio as gr
|
3 |
+
|
4 |
+
from utils import write_vtt
|
5 |
+
import whisper
|
6 |
+
|
7 |
+
import ffmpeg
|
8 |
+
|
9 |
+
#import os
|
10 |
+
#os.system("pip install git+https://github.com/openai/whisper.git")
|
11 |
+
|
12 |
+
# Limitations (set to -1 to disable)
|
13 |
+
DEFAULT_INPUT_AUDIO_MAX_DURATION = 120 # seconds
|
14 |
+
|
15 |
+
LANGUAGES = [
|
16 |
+
"English", "Chinese", "German", "Spanish", "Russian", "Korean",
|
17 |
+
"French", "Japanese", "Portuguese", "Turkish", "Polish", "Catalan",
|
18 |
+
"Dutch", "Arabic", "Swedish", "Italian", "Indonesian", "Hindi",
|
19 |
+
"Finnish", "Vietnamese", "Hebrew", "Ukrainian", "Greek", "Malay",
|
20 |
+
"Czech", "Romanian", "Danish", "Hungarian", "Tamil", "Norwegian",
|
21 |
+
"Thai", "Urdu", "Croatian", "Bulgarian", "Lithuanian", "Latin",
|
22 |
+
"Maori", "Malayalam", "Welsh", "Slovak", "Telugu", "Persian",
|
23 |
+
"Latvian", "Bengali", "Serbian", "Azerbaijani", "Slovenian",
|
24 |
+
"Kannada", "Estonian", "Macedonian", "Breton", "Basque", "Icelandic",
|
25 |
+
"Armenian", "Nepali", "Mongolian", "Bosnian", "Kazakh", "Albanian",
|
26 |
+
"Swahili", "Galician", "Marathi", "Punjabi", "Sinhala", "Khmer",
|
27 |
+
"Shona", "Yoruba", "Somali", "Afrikaans", "Occitan", "Georgian",
|
28 |
+
"Belarusian", "Tajik", "Sindhi", "Gujarati", "Amharic", "Yiddish",
|
29 |
+
"Lao", "Uzbek", "Faroese", "Haitian Creole", "Pashto", "Turkmen",
|
30 |
+
"Nynorsk", "Maltese", "Sanskrit", "Luxembourgish", "Myanmar", "Tibetan",
|
31 |
+
"Tagalog", "Malagasy", "Assamese", "Tatar", "Hawaiian", "Lingala",
|
32 |
+
"Hausa", "Bashkir", "Javanese", "Sundanese"
|
33 |
+
]
|
34 |
+
|
35 |
+
model_cache = dict()
|
36 |
+
|
37 |
+
class UI:
|
38 |
+
def __init__(self, inputAudioMaxDuration):
|
39 |
+
self.inputAudioMaxDuration = inputAudioMaxDuration
|
40 |
+
|
41 |
+
def transcribeFile(self, modelName, languageName, uploadFile, microphoneData, task):
|
42 |
+
source = uploadFile if uploadFile is not None else microphoneData
|
43 |
+
selectedLanguage = languageName.lower() if len(languageName) > 0 else None
|
44 |
+
selectedModel = modelName if modelName is not None else "base"
|
45 |
+
|
46 |
+
if self.inputAudioMaxDuration > 0:
|
47 |
+
# Calculate audio length
|
48 |
+
audioDuration = ffmpeg.probe(source)["format"]["duration"]
|
49 |
+
|
50 |
+
if float(audioDuration) > self.inputAudioMaxDuration:
|
51 |
+
return ("[ERROR]: Maximum audio file length is " + str(self.inputAudioMaxDuration) + "s, file was " + str(audioDuration) + "s"), "[ERROR]"
|
52 |
+
|
53 |
+
model = model_cache.get(selectedModel, None)
|
54 |
+
|
55 |
+
if not model:
|
56 |
+
model = whisper.load_model(selectedModel)
|
57 |
+
model_cache[selectedModel] = model
|
58 |
+
|
59 |
+
result = model.transcribe(source, language=selectedLanguage, task=task)
|
60 |
+
|
61 |
+
segmentStream = StringIO()
|
62 |
+
write_vtt(result["segments"], file=segmentStream)
|
63 |
+
segmentStream.seek(0)
|
64 |
+
|
65 |
+
return result["text"], segmentStream.read()
|
66 |
+
|
67 |
+
|
68 |
+
def createUi(inputAudioMaxDuration, share=False):
|
69 |
+
ui = UI(inputAudioMaxDuration)
|
70 |
+
|
71 |
+
ui_description = "Whisper is a general-purpose speech recognition model. It is trained on a large dataset of diverse "
|
72 |
+
ui_description += " audio and is also a multi-task model that can perform multilingual speech recognition "
|
73 |
+
ui_description += " as well as speech translation and language identification. "
|
74 |
+
|
75 |
+
if inputAudioMaxDuration > 0:
|
76 |
+
ui_description += "\n\n" + "Max audio file length: " + str(inputAudioMaxDuration) + " s"
|
77 |
+
|
78 |
+
demo = gr.Interface(fn=ui.transcribeFile, description=ui_description, inputs=[
|
79 |
+
gr.Dropdown(choices=["tiny", "base", "small", "medium", "large"], value="medium", label="Model"),
|
80 |
+
gr.Dropdown(choices=sorted(LANGUAGES), label="Language"),
|
81 |
+
gr.Audio(source="upload", type="filepath", label="Upload Audio"),
|
82 |
+
gr.Audio(source="microphone", type="filepath", label="Microphone Input"),
|
83 |
+
gr.Dropdown(choices=["transcribe", "translate"], label="Task"),
|
84 |
+
], outputs=[gr.Text(label="Transcription"), gr.Text(label="Segments")])
|
85 |
+
|
86 |
+
demo.launch(share=share)
|
87 |
+
|
88 |
+
if __name__ == '__main__':
|
89 |
+
createUi(DEFAULT_INPUT_AUDIO_MAX_DURATION)
|