-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.py
194 lines (167 loc) · 5.09 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
from flask import Flask, render_template, request, flash, send_file, redirect, url_for
import os
import whisper
import tempfile
import time
UPLOAD_FOLDER = 'uploads'
ALLOWED_EXTENSIONS = {'mp4', 'mp3', 'm4a', 'webm', 'mpga', 'mpeg', 'wav'}
LANGUAGES = {
"en": "English",
"zh": "Chinese",
"de": "German",
"es": "Spanish",
"ru": "Russian",
"ko": "Korean",
"fr": "French",
"ja": "Japanese",
"pt": "Portuguese",
"tr": "Turkish",
"pl": "Polish",
"ca": "Catalan",
"nl": "Dutch",
"ar": "Arabic",
"sv": "Swedish",
"it": "Italian",
"id": "Indonesian",
"hi": "Hindi",
"fi": "Finnish",
"vi": "Vietnamese",
"he": "Hebrew",
"uk": "Ukrainian",
"el": "Greek",
"ms": "Malay",
"cs": "Czech",
"ro": "Romanian",
"da": "Danish",
"hu": "Hungarian",
"ta": "Tamil",
"no": "Norwegian",
"th": "Thai",
"ur": "Urdu",
"hr": "Croatian",
"bg": "Bulgarian",
"lt": "Lithuanian",
"la": "Latin",
"mi": "Maori",
"ml": "Malayalam",
"cy": "Welsh",
"sk": "Slovak",
"te": "Telugu",
"fa": "Persian",
"lv": "Latvian",
"bn": "Bengali",
"sr": "Serbian",
"az": "Azerbaijani",
"sl": "Slovenian",
"kn": "Kannada",
"et": "Estonian",
"mk": "Macedonian",
"br": "Breton",
"eu": "Basque",
"is": "Icelandic",
"hy": "Armenian",
"ne": "Nepali",
"mn": "Mongolian",
"bs": "Bosnian",
"kk": "Kazakh",
"sq": "Albanian",
"sw": "Swahili",
"gl": "Galician",
"mr": "Marathi",
"pa": "Punjabi",
"si": "Sinhala",
"km": "Khmer",
"sn": "Shona",
"yo": "Yoruba",
"so": "Somali",
"af": "Afrikaans",
"oc": "Occitan",
"ka": "Georgian",
"be": "Belarusian",
"tg": "Tajik",
"sd": "Sindhi",
"gu": "Gujarati",
"am": "Amharic",
"yi": "Yiddish",
"lo": "Lao",
"uz": "Uzbek",
"fo": "Faroese",
"ht": "Haitian creole",
"ps": "Pashto",
"tk": "Turkmen",
"nn": "Nynorsk",
"mt": "Maltese",
"sa": "Sanskrit",
"lb": "Luxembourgish",
"my": "Myanmar",
"bo": "Tibetan",
"tl": "Tagalog",
"mg": "Malagasy",
"as": "Assamese",
"tt": "Tatar",
"haw": "Hawaiian",
"ln": "Lingala",
"ha": "Hausa",
"ba": "Bashkir",
"jw": "Javanese",
"su": "Sundanese",
"yue": "Cantonese",
}
app = Flask(__name__)
app.secret_key = 'super secret key'
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
def allowed_file(filename):
return '.' in filename and \
filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS
def process_file(file_path, model_type, language):
print(f"The language is {language}, model is {model_type}, and filename is {file_path}")
model = whisper.load_model(model_type)
audio = whisper.load_audio(file_path)
result = model.transcribe(audio, language=language)
print(result)
transcript_path = save_transcript(result['text'])
return transcript_path
def save_transcript(transcript_text):
temp_dir = tempfile.mkdtemp()
transcript_path = os.path.join(temp_dir, 'transcript.txt')
with open(transcript_path, 'w', encoding='utf-8') as file:
file.write(transcript_text)
return transcript_path
@app.route("/")
def hello_world():
return render_template("index.html",LANGUAGES=LANGUAGES)
@app.route("/transcribe", methods=['POST'])
def transcribe():
try:
audio_file = request.files['audioFile']
language = request.form['language']
model_type = request.form['model']
if audio_file and allowed_file(audio_file.filename):
file_path = os.path.join(app.config['UPLOAD_FOLDER'], audio_file.filename)
audio_file.save(file_path)
flash('Transcribing audio. Please wait...', 'info')
time.sleep(2)
transcript_path = process_file(file_path, model_type, language)
with open(transcript_path, 'r', encoding='utf-8') as file:
transcript_text = file.read()
return render_template("result.html", result={'language': language, 'text': transcript_text, 'transcript_path': transcript_path})
else:
flash('Invalid file format. Please upload an allowed audio file.', 'danger')
except Exception as e:
print(str(e))
flash('Error during transcription. Please try again.', 'danger')
return redirect(url_for('hello_world')) # Redirect to the home page if there's an issue
@app.route("/download")
def download_transcription():
transcript_path = request.args.get('transcript_path', default='', type=str)
if not transcript_path:
flash('Error loading transcription result. Please try again.', 'danger')
return redirect(url_for('hello_world'))
return send_file(transcript_path, as_attachment=True)
@app.route("/features")
def features():
return render_template("features.html")
def run_app():
app.run(debug=True)
if __name__ == '__main__':
run_app()