-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
57 lines (46 loc) · 1.61 KB
/
app.py
File metadata and controls
57 lines (46 loc) · 1.61 KB
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
from flask import Flask, render_template, request, jsonify
import whisper
import os
app = Flask(__name__)
# Create an uploads directory if it doesn't exist
UPLOAD_FOLDER = "uploads"
os.makedirs(UPLOAD_FOLDER, exist_ok=True)
# Ensure the maximum file size is set (e.g., 10MB)
app.config['MAX_CONTENT_LENGTH'] = 10 * 1024 * 1024 # 10 MB limit
# Load the Whisper model
model = whisper.load_model("base")
# Set up the main route
@app.route("/")
def index():
return render_template("index.html")
# Set up the route for transcribing audio files
@app.route('/transcribe', methods=['POST'])
def transcribe():
if 'audio' not in request.files:
return jsonify({'error': 'No file part'}), 400
file = request.files['audio']
if file.filename == '':
return jsonify({'error': 'No selected file'}), 400
# Save the file safely (this ensures malicious files are not executed)
filename = 'recording.webm'
filepath = os.path.join(UPLOAD_FOLDER, filename)
file.save(filepath)
# Convert or decode file if needed before passing to Whisper
try:
result = model.transcribe(filepath)
text = result['text']
except Exception as e:
return jsonify({'error': str(e)}), 500
finally:
if os.path.exists(filepath):
os.remove(filepath)
return jsonify({'text': text})
# Set up the route for handling errors
@app.errorhandler(404)
def not_found(error):
return jsonify({'error': 'Not found'}), 404
@app.errorhandler(413)
def too_large(e):
return jsonify({'error': 'File too large. Max size is 10MB.'}), 413
if __name__ == "__main__":
app.run(debug=True)