-
Notifications
You must be signed in to change notification settings - Fork 3
/
app.py
55 lines (40 loc) · 1.44 KB
/
app.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
from flask import Flask, request, jsonify
import whisper
import os
import time
import json
app = Flask(__name__)
# Load Whisper model
model = whisper.load_model("base")
@app.route('/transcribe', methods=['POST'])
def transcribe_audio():
start = time.time()
if 'file' not in request.files:
return jsonify({"error": "No file part"}), 400
file = request.files['file']
if file.filename == '':
return jsonify({"error": "No selected file"}), 400
if file:
# Save the file temporarily
filepath = os.path.join("/tmp", file.filename)
file.save(filepath)
# Get the size of the file
file_size = os.path.getsize(filepath)
transcribe_start = time.time()
# Process the file with Whisper
result = model.transcribe(filepath)
transcribe_end = time.time()
os.remove(filepath) # Remove the file after processing
result = {
"transcription": result["text"],
"stats": {
"total_processing_time": transcribe_end - transcribe_start,
"words_per_second": round(len(result["text"]) / (transcribe_end - transcribe_start), 2),
"file_size_in_bytes": file_size
},
"filename": file.filename,
}
return jsonify(result)
return jsonify({"error": "Invalid request"}), 400
if __name__ == '__main__':
app.run(debug=True, host='0.0.0.0', port=5000)