Also delete uploaded files to save space
Browse files
app.py
CHANGED
|
@@ -21,6 +21,9 @@ from utils import slugify, write_srt, write_vtt
|
|
| 21 |
# Limitations (set to -1 to disable)
|
| 22 |
DEFAULT_INPUT_AUDIO_MAX_DURATION = 600 # seconds
|
| 23 |
|
|
|
|
|
|
|
|
|
|
| 24 |
LANGUAGES = [
|
| 25 |
"English", "Chinese", "German", "Spanish", "Russian", "Korean",
|
| 26 |
"French", "Japanese", "Portuguese", "Turkish", "Polish", "Catalan",
|
|
@@ -49,40 +52,48 @@ class UI:
|
|
| 49 |
|
| 50 |
def transcribeFile(self, modelName, languageName, urlData, uploadFile, microphoneData, task):
|
| 51 |
source, sourceName = getSource(urlData, uploadFile, microphoneData)
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 59 |
|
| 60 |
-
if
|
| 61 |
-
|
|
|
|
| 62 |
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
|
|
|
| 68 |
|
| 69 |
-
|
| 70 |
-
|
|
|
|
| 71 |
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
|
|
|
| 75 |
|
| 76 |
-
|
| 77 |
-
downloadDirectory = tempfile.mkdtemp()
|
| 78 |
-
filePrefix = slugify(sourceName, allow_unicode=True)
|
| 79 |
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
|
|
|
| 84 |
|
| 85 |
-
return download, text, vtt
|
| 86 |
|
| 87 |
def getSource(urlData, uploadFile, microphoneData):
|
| 88 |
if urlData:
|
|
|
|
| 21 |
# Limitations (set to -1 to disable)
|
| 22 |
DEFAULT_INPUT_AUDIO_MAX_DURATION = 600 # seconds
|
| 23 |
|
| 24 |
+
# Whether or not to automatically delete all uploaded files, to save disk space
|
| 25 |
+
DELETE_UPLOADED_FILES = True
|
| 26 |
+
|
| 27 |
LANGUAGES = [
|
| 28 |
"English", "Chinese", "German", "Spanish", "Russian", "Korean",
|
| 29 |
"French", "Japanese", "Portuguese", "Turkish", "Polish", "Catalan",
|
|
|
|
| 52 |
|
| 53 |
def transcribeFile(self, modelName, languageName, urlData, uploadFile, microphoneData, task):
|
| 54 |
source, sourceName = getSource(urlData, uploadFile, microphoneData)
|
| 55 |
+
|
| 56 |
+
try:
|
| 57 |
+
selectedLanguage = languageName.lower() if len(languageName) > 0 else None
|
| 58 |
+
selectedModel = modelName if modelName is not None else "base"
|
| 59 |
+
|
| 60 |
+
if self.inputAudioMaxDuration > 0:
|
| 61 |
+
# Calculate audio length
|
| 62 |
+
audioDuration = ffmpeg.probe(source)["format"]["duration"]
|
| 63 |
+
|
| 64 |
+
if float(audioDuration) > self.inputAudioMaxDuration:
|
| 65 |
+
return ("[ERROR]: Maximum audio file length is " + str(self.inputAudioMaxDuration) + "s, file was " + str(audioDuration) + "s"), "[ERROR]"
|
| 66 |
+
|
| 67 |
+
model = model_cache.get(selectedModel, None)
|
| 68 |
|
| 69 |
+
if not model:
|
| 70 |
+
model = whisper.load_model(selectedModel)
|
| 71 |
+
model_cache[selectedModel] = model
|
| 72 |
|
| 73 |
+
# The results
|
| 74 |
+
result = model.transcribe(source, language=selectedLanguage, task=task)
|
| 75 |
+
|
| 76 |
+
text = result["text"]
|
| 77 |
+
vtt = getSubs(result["segments"], "vtt")
|
| 78 |
+
srt = getSubs(result["segments"], "srt")
|
| 79 |
|
| 80 |
+
# Files that can be downloaded
|
| 81 |
+
downloadDirectory = tempfile.mkdtemp()
|
| 82 |
+
filePrefix = slugify(sourceName, allow_unicode=True)
|
| 83 |
|
| 84 |
+
download = []
|
| 85 |
+
download.append(createFile(srt, downloadDirectory, filePrefix + "-subs.srt"));
|
| 86 |
+
download.append(createFile(vtt, downloadDirectory, filePrefix + "-subs.vtt"));
|
| 87 |
+
download.append(createFile(text, downloadDirectory, filePrefix + "-transcript.txt"));
|
| 88 |
|
| 89 |
+
return download, text, vtt
|
|
|
|
|
|
|
| 90 |
|
| 91 |
+
finally:
|
| 92 |
+
# Cleanup source
|
| 93 |
+
if DELETE_UPLOADED_FILES:
|
| 94 |
+
print("Deleting source file " + source)
|
| 95 |
+
os.remove(source)
|
| 96 |
|
|
|
|
| 97 |
|
| 98 |
def getSource(urlData, uploadFile, microphoneData):
|
| 99 |
if urlData:
|