Update app.py
Browse files
app.py
CHANGED
|
@@ -1,123 +1,153 @@
|
|
| 1 |
-
from flask import Flask, request, jsonify, Response
|
| 2 |
-
from faster_whisper import WhisperModel
|
| 3 |
-
import torch
|
| 4 |
-
import io
|
| 5 |
-
import time
|
| 6 |
-
import datetime
|
| 7 |
-
from threading import Semaphore
|
| 8 |
-
import os
|
| 9 |
-
from werkzeug.utils import secure_filename
|
| 10 |
-
import tempfile
|
| 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 |
app.run(host="0.0.0.0", port=7860, threaded=True)
|
|
|
|
| 1 |
+
from flask import Flask, request, jsonify, Response
|
| 2 |
+
from faster_whisper import WhisperModel
|
| 3 |
+
import torch
|
| 4 |
+
import io
|
| 5 |
+
import time
|
| 6 |
+
import datetime
|
| 7 |
+
from threading import Semaphore
|
| 8 |
+
import os
|
| 9 |
+
from werkzeug.utils import secure_filename
|
| 10 |
+
import tempfile
|
| 11 |
+
from moviepy.editor import VideoFileClip # Added for video processing
|
| 12 |
+
|
| 13 |
+
app = Flask(__name__)
|
| 14 |
+
|
| 15 |
+
# Configuration
|
| 16 |
+
MAX_CONCURRENT_REQUESTS = 2 # Adjust based on server capacity
|
| 17 |
+
MAX_FILE_DURATION = 60 * 30 # 30 minutes maximum duration (adjust as needed)
|
| 18 |
+
TEMPORARY_FOLDER = tempfile.gettempdir()
|
| 19 |
+
ALLOWED_AUDIO_EXTENSIONS = {'mp3', 'wav', 'ogg', 'm4a', 'flac', 'aac', 'wma', 'opus', 'aiff'}
|
| 20 |
+
ALLOWED_VIDEO_EXTENSIONS = {'mp4', 'avi', 'mov', 'mkv', 'webm', 'flv', 'wmv', 'mpeg', 'mpg', '3gp'}
|
| 21 |
+
ALLOWED_EXTENSIONS = ALLOWED_AUDIO_EXTENSIONS.union(ALLOWED_VIDEO_EXTENSIONS)
|
| 22 |
+
|
| 23 |
+
# Device check for faster-whisper
|
| 24 |
+
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 25 |
+
compute_type = "float16" if device == "cuda" else "int8"
|
| 26 |
+
print(f"Using device: {device} with compute_type: {compute_type}")
|
| 27 |
+
|
| 28 |
+
# Faster Whisper setup with optimized parameters for long audio
|
| 29 |
+
beamsize = 2
|
| 30 |
+
wmodel = WhisperModel(
|
| 31 |
+
"guillaumekln/faster-whisper-small",
|
| 32 |
+
device=device,
|
| 33 |
+
compute_type=compute_type,
|
| 34 |
+
download_root="./model_cache"
|
| 35 |
+
)
|
| 36 |
+
|
| 37 |
+
# Concurrency control
|
| 38 |
+
request_semaphore = Semaphore(MAX_CONCURRENT_REQUESTS)
|
| 39 |
+
active_requests = 0
|
| 40 |
+
|
| 41 |
+
def allowed_file(filename):
|
| 42 |
+
return '.' in filename and \
|
| 43 |
+
filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS
|
| 44 |
+
|
| 45 |
+
def cleanup_temp_files(*file_paths):
|
| 46 |
+
"""Ensure temporary files are deleted after processing"""
|
| 47 |
+
for file_path in file_paths:
|
| 48 |
+
try:
|
| 49 |
+
if file_path and os.path.exists(file_path):
|
| 50 |
+
os.remove(file_path)
|
| 51 |
+
except Exception as e:
|
| 52 |
+
print(f"Error cleaning up temp file {file_path}: {str(e)}")
|
| 53 |
+
|
| 54 |
+
def extract_audio_from_video(video_path, output_audio_path):
|
| 55 |
+
"""Extract audio from a video file and save it as a temporary audio file"""
|
| 56 |
+
try:
|
| 57 |
+
video = VideoFileClip(video_path)
|
| 58 |
+
if video.duration > MAX_FILE_DURATION:
|
| 59 |
+
video.close()
|
| 60 |
+
raise ValueError(f"Video duration exceeds {MAX_FILE_DURATION} seconds")
|
| 61 |
+
video.audio.write_audiofile(output_audio_path)
|
| 62 |
+
video.close()
|
| 63 |
+
return output_audio_path
|
| 64 |
+
except Exception as e:
|
| 65 |
+
raise Exception(f"Failed to extract audio from video: {str(e)}")
|
| 66 |
+
|
| 67 |
+
@app.route("/health", methods=["GET"])
|
| 68 |
+
def health_check():
|
| 69 |
+
"""Endpoint to check if API is running"""
|
| 70 |
+
return jsonify({
|
| 71 |
+
'status': 'API is running',
|
| 72 |
+
'timestamp': datetime.datetime.now().isoformat(),
|
| 73 |
+
'device': device,
|
| 74 |
+
'compute_type': compute_type,
|
| 75 |
+
'active_requests': active_requests,
|
| 76 |
+
'max_duration_supported': MAX_FILE_DURATION,
|
| 77 |
+
'supported_formats': list(ALLOWED_EXTENSIONS)
|
| 78 |
+
})
|
| 79 |
+
|
| 80 |
+
@app.route("/status/busy", methods=["GET"])
|
| 81 |
+
def server_busy():
|
| 82 |
+
"""Endpoint to check if server is busy"""
|
| 83 |
+
is_busy = active_requests >= MAX_CONCURRENT_REQUESTS
|
| 84 |
+
return jsonify({
|
| 85 |
+
'is_busy': is_busy,
|
| 86 |
+
'active_requests': active_requests,
|
| 87 |
+
'max_capacity': MAX_CONCURRENT_REQUESTS
|
| 88 |
+
})
|
| 89 |
+
|
| 90 |
+
@app.route("/whisper_transcribe", methods=["POST"])
|
| 91 |
+
def transcribe():
|
| 92 |
+
global active_requests
|
| 93 |
+
|
| 94 |
+
if not request_semaphore.acquire(blocking=False):
|
| 95 |
+
return jsonify({'error': 'Server busy'}), 503
|
| 96 |
+
|
| 97 |
+
active_requests += 1
|
| 98 |
+
start_time = time.time()
|
| 99 |
+
temp_file_path = None
|
| 100 |
+
temp_audio_path = None
|
| 101 |
+
|
| 102 |
+
try:
|
| 103 |
+
if 'file' not in request.files:
|
| 104 |
+
return jsonify({'error': 'No file provided'}), 400
|
| 105 |
+
|
| 106 |
+
file = request.files['file']
|
| 107 |
+
if not (file and allowed_file(file.filename)):
|
| 108 |
+
return jsonify({'error': f'Invalid file format. Supported: {", ".join(ALLOWED_EXTENSIONS)}'}), 400
|
| 109 |
+
|
| 110 |
+
# Save uploaded file to temporary location
|
| 111 |
+
temp_file_path = os.path.join(TEMPORARY_FOLDER, secure_filename(file.filename))
|
| 112 |
+
file.save(temp_file_path)
|
| 113 |
+
|
| 114 |
+
# Check if file is a video and extract audio if necessary
|
| 115 |
+
file_extension = file.filename.rsplit('.', 1)[1].lower()
|
| 116 |
+
if file_extension in ALLOWED_VIDEO_EXTENSIONS:
|
| 117 |
+
temp_audio_path = os.path.join(TEMPORARY_FOLDER, f"temp_audio_{int(time.time())}.wav")
|
| 118 |
+
extract_audio_from_video(temp_file_path, temp_audio_path)
|
| 119 |
+
transcription_file = temp_audio_path
|
| 120 |
+
else:
|
| 121 |
+
transcription_file = temp_file_path
|
| 122 |
+
|
| 123 |
+
# Transcribe the audio file
|
| 124 |
+
segments, _ = wmodel.transcribe(
|
| 125 |
+
transcription_file,
|
| 126 |
+
beam_size=beamsize,
|
| 127 |
+
vad_filter=True,
|
| 128 |
+
without_timestamps=True,
|
| 129 |
+
compression_ratio_threshold=2.4,
|
| 130 |
+
word_timestamps=False
|
| 131 |
+
)
|
| 132 |
+
|
| 133 |
+
full_text = " ".join(segment.text for segment in segments)
|
| 134 |
+
return jsonify({
|
| 135 |
+
'transcription': full_text,
|
| 136 |
+
'file_type': 'video' if file_extension in ALLOWED_VIDEO_EXTENSIONS else 'audio'
|
| 137 |
+
}), 200
|
| 138 |
+
|
| 139 |
+
except Exception as e:
|
| 140 |
+
return jsonify({'error': str(e)}), 500
|
| 141 |
+
|
| 142 |
+
finally:
|
| 143 |
+
cleanup_temp_files(temp_file_path, temp_audio_path)
|
| 144 |
+
active_requests -= 1
|
| 145 |
+
request_semaphore.release()
|
| 146 |
+
print(f"Processed in {time.time()-start_time:.2f}s (Active: {active_requests})")
|
| 147 |
+
|
| 148 |
+
if __name__ == "__main__":
|
| 149 |
+
# Create temporary folder if it doesn't exist
|
| 150 |
+
if not os.path.exists(TEMPORARY_FOLDER):
|
| 151 |
+
os.makedirs(TEMPORARY_FOLDER)
|
| 152 |
+
|
| 153 |
app.run(host="0.0.0.0", port=7860, threaded=True)
|