136 lines
3.9 KiB
Python
136 lines
3.9 KiB
Python
#!/usr/bin/env python
|
||
# -*- coding: utf-8 -*-
|
||
"""
|
||
简化版语音服务 - 无需PaddleSpeech
|
||
仅做文本对比评分,先让APP能用起来
|
||
"""
|
||
|
||
from flask import Flask, request, jsonify
|
||
from flask_cors import CORS
|
||
import os
|
||
import time
|
||
from difflib import SequenceMatcher
|
||
|
||
app = Flask(__name__)
|
||
CORS(app)
|
||
|
||
def calculate_similarity(text1, text2):
|
||
"""计算文本相似度"""
|
||
if not text1 or not text2:
|
||
return 0
|
||
|
||
text1 = ''.join(filter(str.isalnum, text1))
|
||
text2 = ''.join(filter(str.isalnum, text2))
|
||
|
||
if not text1 or not text2:
|
||
return 0
|
||
|
||
similarity = SequenceMatcher(None, text1, text2).ratio()
|
||
return round(similarity * 100, 2)
|
||
|
||
@app.route('/api/speech/recognize', methods=['POST'])
|
||
def recognize():
|
||
"""
|
||
临时方案:让用户手动输入识别文本
|
||
后续接入真实语音识别
|
||
"""
|
||
try:
|
||
# 检查是否有音频文件(暂时忽略,不处理)
|
||
if 'audio' in request.files:
|
||
audio_file = request.files['audio']
|
||
# 保存音频以便后续处理
|
||
temp_dir = './temp_audio'
|
||
if not os.path.exists(temp_dir):
|
||
os.makedirs(temp_dir)
|
||
|
||
timestamp = str(int(time.time() * 1000))
|
||
temp_path = os.path.join(temp_dir, f'audio_{timestamp}.wav')
|
||
audio_file.save(temp_path)
|
||
print(f"已保存音频: {temp_path}")
|
||
|
||
reference_text = request.form.get('referenceText', '')
|
||
|
||
# 模拟识别:返回提示让用户手动输入
|
||
# 实际应用中,这里应该调用语音识别
|
||
|
||
return jsonify({
|
||
'code': 200,
|
||
'msg': '音频已接收,请手动输入识别文本进行评分',
|
||
'data': {
|
||
'recognizedText': '', # 空的,让前端手动输入
|
||
'score': 0,
|
||
'pronunciationScore': 0,
|
||
'fluencyScore': 0,
|
||
'status': 'completed',
|
||
'needManualInput': True # 标记需要手动输入
|
||
}
|
||
})
|
||
|
||
except Exception as e:
|
||
print(f"错误: {str(e)}")
|
||
return jsonify({
|
||
'code': 500,
|
||
'msg': f'处理失败: {str(e)}'
|
||
}), 500
|
||
|
||
@app.route('/api/speech/evaluate', methods=['POST'])
|
||
def evaluate():
|
||
"""
|
||
评测接口:对比用户输入和标准文本
|
||
"""
|
||
try:
|
||
user_text = request.form.get('userText', '')
|
||
reference_text = request.form.get('referenceText', '')
|
||
|
||
if not user_text:
|
||
return jsonify({
|
||
'code': 400,
|
||
'msg': '缺少用户输入文本'
|
||
}), 400
|
||
|
||
# 计算相似度
|
||
score = calculate_similarity(user_text, reference_text)
|
||
pronunciation_score = max(0, score - 5)
|
||
fluency_score = max(0, score - 3)
|
||
|
||
return jsonify({
|
||
'code': 200,
|
||
'msg': '评测成功',
|
||
'data': {
|
||
'recognizedText': user_text,
|
||
'score': score,
|
||
'pronunciationScore': pronunciation_score,
|
||
'fluencyScore': fluency_score,
|
||
'status': 'completed'
|
||
}
|
||
})
|
||
|
||
except Exception as e:
|
||
return jsonify({
|
||
'code': 500,
|
||
'msg': f'评测失败: {str(e)}'
|
||
}), 500
|
||
|
||
@app.route('/api/speech/health', methods=['GET'])
|
||
def health():
|
||
"""健康检查"""
|
||
return jsonify({
|
||
'code': 200,
|
||
'msg': '服务正常(简化版)',
|
||
'data': {
|
||
'version': 'simple',
|
||
'speech_recognition': False
|
||
}
|
||
})
|
||
|
||
if __name__ == '__main__':
|
||
print("=" * 50)
|
||
print("简化版语音服务")
|
||
print("说明:音频接收后需手动输入识别文本")
|
||
print("=" * 50)
|
||
print("")
|
||
print("服务启动在: http://localhost:5000")
|
||
print("")
|
||
|
||
app.run(host='0.0.0.0', port=5000, debug=False)
|