File size: 1,180 Bytes
1a75907
 
 
 
13593ef
1a75907
 
13593ef
1a75907
49f69f0
13593ef
 
 
 
1a75907
 
0b0e86f
1a75907
0b0e86f
1a75907
 
 
0b0e86f
 
 
 
1a75907
 
49f69f0
0b0e86f
 
 
1a75907
0b0e86f
1a75907
0b0e86f
 
 
 
 
1a75907
 
0b0e86f
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
from flask import Flask, request, jsonify
from flask_cors import CORS
from faster_whisper import WhisperModel
import tempfile
import os

app = Flask(__name__)
CORS(app)

# Load model (same as your working Gradio code)
model_size = "tiny"
device = "cpu"
compute_type = "int8"
model = WhisperModel(model_size, device=device, compute_type=compute_type)

@app.route('/transcribe', methods=['POST'])
def transcribe():
    if 'audio' not in request.files:
        return jsonify({'error': 'No audio file'}), 400
    
    file = request.files['audio']
    
    # Save temp file
    temp = tempfile.NamedTemporaryFile(delete=False, suffix='.wav')
    file.save(temp.name)
    temp.close()
    
    try:
        # Same transcribe function that worked
        segments, _ = model.transcribe(temp.name, beam_size=5)
        text = "".join([segment.text for segment in segments])
        return jsonify({'text': text})
    except Exception as e:
        return jsonify({'error': str(e)}), 500
    finally:
        os.unlink(temp.name)

@app.route('/health', methods=['GET'])
def health():
    return jsonify({'status': 'ok'})

if __name__ == '__main__':
    app.run(host='0.0.0.0', port=7860)