Spaces:
Sleeping
Sleeping
File size: 6,042 Bytes
b53f321 45906fa |
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 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 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 |
from flask import Flask, request, jsonify, send_from_directory
import os
import subprocess
import json
from flask_cors import CORS
app = Flask(__name__)
CORS(app)
# Define folders for storing uploaded audio and output audio
UPLOAD_FOLDER = 'uploads'
OUTPUT_FOLDER = 'output'
os.makedirs(UPLOAD_FOLDER, exist_ok=True)
os.makedirs(OUTPUT_FOLDER, exist_ok=True)
# Serve the index.html file as homepage
@app.route('/')
def index():
return send_from_directory('.', 'index.html')
# Serve uploaded audio files
@app.route('/uploads/<filename>')
def uploaded_file(filename):
return send_from_directory(UPLOAD_FOLDER, filename)
# Serve output audio files
@app.route('/output/<filename>')
def output_file(filename):
return send_from_directory(OUTPUT_FOLDER, filename)
# Main route to handle upload and processing stages
@app.route('/upload', methods=['POST'])
def upload():
print("Upload route hit")
language = request.form.get('language')
audio = request.files.get('audio')
stage = request.args.get('stage', 'full')
print(f"Language: {language}")
print(f"Audio received: {bool(audio)}")
print(f"Stage: {stage}")
if not language or not audio:
return jsonify({'error': 'Missing language or audio'}), 400
lang_short = language.split('-')[0]
input_path = os.path.join(UPLOAD_FOLDER, 'recording.wav')
output_path = os.path.join(OUTPUT_FOLDER, 'output_audio.wav')
audio.save(input_path)
transcription = None
translation = None
# Handle full or translation-only processing
if stage in ['translate', 'full']:
# Transcription step
try:
transcribe_cmd = ['python3', 'process_audio.py', 'transcribe', input_path]
print(f"Running transcription command: {' '.join(transcribe_cmd)}")
transcribe_output = subprocess.check_output(transcribe_cmd, stderr=subprocess.STDOUT)
print("Raw transcription output:\n", transcribe_output.decode())
json_start = transcribe_output.decode().rfind('{')
if json_start == -1:
raise ValueError("No JSON object found in transcription output")
transcribe_json = json.loads(transcribe_output.decode()[json_start:])
transcription = transcribe_json.get('transcription', '').strip()
except subprocess.CalledProcessError as e:
print("Transcription failed:", e.output.decode())
return jsonify({'error': 'Transcription failed', 'details': e.output.decode()}), 500
except Exception as e:
print("Error parsing transcription JSON:", str(e))
return jsonify({'error': 'Invalid JSON from transcription', 'details': str(e)}), 500
# Translation step
try:
translate_cmd = ['python3', 'process_audio.py', 'translate-text', transcription, lang_short]
print(f"Running translation command: {' '.join(translate_cmd)}")
translate_output = subprocess.check_output(translate_cmd, stderr=subprocess.STDOUT)
print("Raw translation output:\n", translate_output.decode())
json_start = translate_output.decode().rfind('{')
if json_start == -1:
raise ValueError("No JSON object found in translation output")
translate_json = json.loads(translate_output.decode()[json_start:])
translation = translate_json.get('translation', '').strip()
except subprocess.CalledProcessError as e:
print("Translation failed:", e.output.decode())
return jsonify({'error': 'Translation failed', 'details': e.output.decode()}), 500
except Exception as e:
print("Error parsing translation JSON:", str(e))
return jsonify({'error': 'Invalid JSON from translation', 'details': str(e)}), 500
# Audio synthesis step
try:
synth_cmd = ['python3', 'process_audio.py', 'synthesize-audio', translation, output_path, lang_short]
print(f"Running synthesis command: {' '.join(synth_cmd)}")
subprocess.check_call(synth_cmd, stderr=subprocess.STDOUT)
except subprocess.CalledProcessError as e:
print("Audio synthesis failed:", e.output.decode())
return jsonify({'error': 'Audio synthesis failed', 'details': e.output.decode()}), 500
# Final response for full pipeline
return jsonify({
'transcription': transcription,
'translation': translation,
'inputAudioUrl': '/uploads/recording.wav',
'outputAudioUrl': '/output/output_audio.wav'
})
# Handle transcription-only stage
elif stage == 'transcribe':
try:
transcribe_cmd = ['python3', 'process_audio.py', 'transcribe', input_path]
print(f"Running transcription command: {' '.join(transcribe_cmd)}")
transcribe_output = subprocess.check_output(transcribe_cmd, stderr=subprocess.STDOUT)
print("Raw transcription output:\n", transcribe_output.decode())
json_start = transcribe_output.decode().rfind('{')
if json_start == -1:
raise ValueError("No JSON object found in transcription output")
transcribe_json = json.loads(transcribe_output.decode()[json_start:])
transcription = transcribe_json.get('transcription', '').strip()
return jsonify({
'transcription': transcription,
'inputAudioUrl': '/uploads/recording.wav'
})
except subprocess.CalledProcessError as e:
return jsonify({'error': 'Transcription failed', 'details': e.output.decode()}), 500
except Exception as e:
return jsonify({'error': 'Invalid JSON from transcription', 'details': str(e)}), 500
# Invalid stage provided
else:
return jsonify({'error': 'Invalid stage provided'}), 400
# Start Flask server on port 5050 with debug mode on
if __name__ == '__main__':
app.run(host='0.0.0.0', port=7860, debug=True) |