-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWebServer.py
More file actions
52 lines (41 loc) · 1.76 KB
/
WebServer.py
File metadata and controls
52 lines (41 loc) · 1.76 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
import os
from flask import Flask, request, jsonify, app
from ASR import ASR
from flask_cors import CORS
from pydub import AudioSegment
class ASRServer:
def __init__(self):
self.app = Flask(__name__)
CORS(self.app)
self.setup_routes()
def saveFile(self, file):
if file:
file_path = os.path.join("tmp", file.filename)
file.save(file_path) # Save the original file first
# Convert to WAV using pydub
sound = AudioSegment.from_file(file_path)
wav_path = os.path.join("tmp", "input.wav")
sound.export(wav_path, format="wav")
def setup_routes(self):
self.asr_system = ASR()
@self.app.route('/transcribe', methods=['POST'])
def transcribe_audio():
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
# Extract splitter type from form data, default to "best first" if not provided
splitter_type = request.form.get('splitterType', 'best first')
width = int(request.form.get('width', 3)) # Default width to 3 if not provided
self.saveFile(file)
try:
output = self.asr_system.transcribeAudioFile("tmp/input.wav",splitterType=splitter_type,width=width)
return jsonify({'transcription': output})
except Exception as e:
return jsonify({'error': str(e)}), 500
def run(self, host='0.0.0.0', port=5000, debug=True):
self.app.run(host=host, port=port, debug=debug)
if __name__ == '__main__':
server = ASRServer()
server.run()