File size: 3,029 Bytes
935ba57
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9a6e69b
 
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
from flask import Flask, request, jsonify
import whisper
import os
import tempfile
import logging
import subprocess

app = Flask(__name__)

# Configure logging
logging.basicConfig(level=logging.ERROR)
logger = logging.getLogger(__name__)

# Load the Whisper model (you can choose different sizes: tiny, base, small, medium, large)
model = whisper.load_model("base")

def check_ffmpeg():
    try:
        subprocess.run(['ffmpeg', '-version'], capture_output=True, check=True)
        logger.info("FFmpeg is installed and working")
        return True
    except (subprocess.SubprocessError, FileNotFoundError) as e:
        logger.error(f"FFmpeg check failed: {str(e)}")
        return False

@app.route('/transcribe', methods=['POST'])
def transcribe_audio():
    temp_file_path = None
    try:
        # Check if FFmpeg is installed
        if not check_ffmpeg():
            return jsonify({"error": "FFmpeg is not installed. Please install FFmpeg to process audio files."}), 500

        # Check if file is in the request
        if 'file' not in request.files:
            return jsonify({"error": "No file provided"}), 400
        
        file = request.files['file']
        if file.filename == '':
            return jsonify({"error": "No file selected"}), 400

        # Get the file extension
        file_ext = os.path.splitext(file.filename)[1]
        if not file_ext:
            file_ext = '.mp3'  # default extension

        # Create a temporary file with the correct extension
        temp_file = tempfile.NamedTemporaryFile(delete=False, suffix=file_ext)
        temp_file_path = temp_file.name
        temp_file.close()

        # Save the uploaded file
        file.save(temp_file_path)
        logger.debug(f"File saved to: {temp_file_path}")

        # Verify the file exists and has content
        if not os.path.exists(temp_file_path):
            return jsonify({"error": "Failed to save temporary file"}), 500
        
        file_size = os.path.getsize(temp_file_path)
        if file_size == 0:
            return jsonify({"error": "Uploaded file is empty"}), 400

        logger.debug(f"File size: {file_size} bytes")

        # Transcribe the audio
        result = model.transcribe(temp_file_path)
        logger.debug("Transcription completed successfully")

        return jsonify({
            "text": result["text"],
            "segments": result["segments"]
        })

    except Exception as e:
        logger.error(f"Error during transcription: {str(e)}", exc_info=True)
        return jsonify({"error": str(e)}), 500

    finally:
        # Clean up the temporary file
        if temp_file_path and os.path.exists(temp_file_path):
            try:
                os.unlink(temp_file_path)
                logger.debug(f"Temporary file deleted: {temp_file_path}")
            except Exception as e:
                logger.error(f"Error deleting temporary file: {str(e)}")

if __name__ == '__main__':
    port = int(os.environ.get('PORT', 7860))
    app.run(host='0.0.0.0', port=port)