a
This commit is contained in:
@@ -0,0 +1,40 @@
|
||||
# Python
|
||||
__pycache__/
|
||||
*.py[cod]
|
||||
*$py.class
|
||||
*.so
|
||||
.Python
|
||||
*.egg-info/
|
||||
dist/
|
||||
build/
|
||||
|
||||
# Virtual environments
|
||||
.venv/
|
||||
.venv-*/
|
||||
venv/
|
||||
env/
|
||||
|
||||
# Node dependencies
|
||||
node_modules/
|
||||
|
||||
# 上传的音频文件
|
||||
uploads/*.webm
|
||||
uploads/*.wav
|
||||
uploads/*.mp3
|
||||
!uploads/.gitkeep
|
||||
|
||||
# SSL 证书
|
||||
cert.pem
|
||||
key.pem
|
||||
|
||||
# 模型文件
|
||||
models/*.zip
|
||||
models/*.tar.gz
|
||||
models/cn-model/
|
||||
|
||||
# 日志
|
||||
*.log
|
||||
|
||||
# 临时文件
|
||||
*.tmp
|
||||
.DS_Store
|
||||
@@ -0,0 +1,123 @@
|
||||
# 🎙️ 音频录音工具
|
||||
|
||||
前后端分离的录音应用,支持手机浏览器录音、回放和上传。
|
||||
|
||||
## 📁 项目结构
|
||||
|
||||
```
|
||||
audio-recorder/
|
||||
├── frontend/ # 前端(纯HTML/JS)
|
||||
│ └── index.html # 录音页面
|
||||
├── backend/ # 后端(Flask API)
|
||||
│ ├── app.py # 主服务
|
||||
│ └── requirements.txt
|
||||
├── uploads/ # 音频文件存储目录
|
||||
└── start.sh # 启动脚本
|
||||
```
|
||||
|
||||
## ✨ 功能特性
|
||||
|
||||
- ✅ **开始录音** - 点击按钮开始录制音频
|
||||
- ✅ **结束录音** - 停止录制并保存
|
||||
- ✅ **回放录音** - 本地播放刚录制的音频
|
||||
- ✅ **自动上传** - 录音完成后自动上传到服务器
|
||||
- ✅ **录音计时** - 实时显示录音时长
|
||||
- ✅ **手机适配** - 响应式设计,支持移动端
|
||||
|
||||
## 🚀 快速启动
|
||||
|
||||
### 1. 启动服务
|
||||
|
||||
```bash
|
||||
cd /home/ubuntu/nanocode/audio-recorder
|
||||
chmod +x start.sh
|
||||
./start.sh
|
||||
```
|
||||
|
||||
### 2. 访问应用
|
||||
|
||||
- 本机访问: http://localhost:8890
|
||||
- 局域网访问: http://<服务器IP>:8890
|
||||
|
||||
## 📱 手机浏览器要求
|
||||
|
||||
**重要**:手机浏览器录音需要 **HTTPS** 环境
|
||||
|
||||
### 解决方案:
|
||||
|
||||
#### 方案1:使用 nginx + SSL(推荐)
|
||||
|
||||
```nginx
|
||||
server {
|
||||
listen 443 ssl;
|
||||
server_name your-domain.com;
|
||||
|
||||
ssl_certificate /path/to/cert.pem;
|
||||
ssl_certificate_key /path/to/key.pem;
|
||||
|
||||
location / {
|
||||
proxy_pass http://127.0.0.1:8890;
|
||||
proxy_set_header Host $host;
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
#### 方案2:本地测试(Chrome)
|
||||
|
||||
```bash
|
||||
# 启动带SSL的隧道(需要安装 localtunnel 或 ngrok)
|
||||
npx localtunnel --port 8890
|
||||
```
|
||||
|
||||
#### 方案3:内网穿透
|
||||
|
||||
使用 ngrok:
|
||||
```bash
|
||||
ngrok http 8890
|
||||
```
|
||||
|
||||
## 🔌 API 接口
|
||||
|
||||
| 接口 | 方法 | 说明 |
|
||||
|------|------|------|
|
||||
| `/api/upload` | POST | 上传音频文件 |
|
||||
| `/api/recordings` | GET | 列出所有录音 |
|
||||
| `/api/recordings/<filename>` | GET | 获取单个录音 |
|
||||
| `/api/recordings/<filename>` | DELETE | 删除录音 |
|
||||
| `/api/health` | GET | 健康检查 |
|
||||
|
||||
## 🛠️ 技术栈
|
||||
|
||||
**前端:**
|
||||
- 原生 HTML5 + CSS3 + JavaScript
|
||||
- MediaRecorder API(录音)
|
||||
- Web Audio API(回放)
|
||||
|
||||
**后端:**
|
||||
- Python 3.8+
|
||||
- Flask
|
||||
- Flask-CORS
|
||||
|
||||
**音频格式:**
|
||||
- 录制:WebM/Opus(默认)或 MP4
|
||||
- 存储:WebM 格式
|
||||
- 兼容性:Chrome, Firefox, Safari, Edge
|
||||
|
||||
## ⚠️ 注意事项
|
||||
|
||||
1. **权限**:首次使用需要授权麦克风权限
|
||||
2. **HTTPS**:手机浏览器必须在 HTTPS 环境下才能录音
|
||||
3. **格式**:不同浏览器支持的音频格式不同(优先 WebM)
|
||||
4. **存储**:录音文件保存在 `uploads/` 目录
|
||||
|
||||
## 📝 后续开发计划
|
||||
|
||||
- [ ] 语音转文字(ASR)
|
||||
- [ ] 录音列表管理
|
||||
- [ ] 大模型总结
|
||||
- [ ] 用户认证
|
||||
- [ ] 录音剪辑
|
||||
|
||||
---
|
||||
|
||||
创建时间:2026-03-29
|
||||
@@ -0,0 +1,589 @@
|
||||
#!/usr/bin/env python3
|
||||
"""
|
||||
录音后端服务 - 语音转文字 (FunASR + Vosk 双引擎)
|
||||
优先 FunASR(阿里达摩院,中文识别效果好),失败回退 Vosk(轻量离线)
|
||||
"""
|
||||
from flask import Flask, request, jsonify, send_from_directory
|
||||
from flask_cors import CORS
|
||||
from werkzeug.utils import secure_filename
|
||||
import os
|
||||
import ssl
|
||||
import subprocess
|
||||
import wave
|
||||
import json
|
||||
import warnings
|
||||
from datetime import datetime
|
||||
|
||||
warnings.filterwarnings('ignore')
|
||||
|
||||
app = Flask(__name__)
|
||||
CORS(app)
|
||||
|
||||
# 配置
|
||||
UPLOAD_FOLDER = '/home/ubuntu/nanocode/audio-recorder/uploads'
|
||||
VOSK_MODEL_PATH = '/home/ubuntu/nanocode/audio-recorder/models/cn-model'
|
||||
ALLOWED_EXTENSIONS = {'webm', 'mp4', 'wav', 'mp3', 'ogg'}
|
||||
MAX_CONTENT_LENGTH = 100 * 1024 * 1024 # 100MB
|
||||
|
||||
app.config['MAX_CONTENT_LENGTH'] = MAX_CONTENT_LENGTH
|
||||
os.makedirs(UPLOAD_FOLDER, exist_ok=True)
|
||||
|
||||
# 全局模型(延迟加载)
|
||||
funasr_model = None
|
||||
vosk_model = None
|
||||
|
||||
|
||||
def get_funasr_model():
|
||||
"""加载 FunASR 模型(阿里,中文识别效果好)"""
|
||||
global funasr_model
|
||||
if funasr_model is None:
|
||||
try:
|
||||
from funasr import AutoModel
|
||||
print("🔄 加载 FunASR 模型(阿里达摩院)...")
|
||||
funasr_model = AutoModel(
|
||||
model="paraformer-zh",
|
||||
vad_model="fsmn-vad",
|
||||
punc_model="ct-punc",
|
||||
)
|
||||
print("✅ FunASR 模型加载完成")
|
||||
except Exception as e:
|
||||
print(f"⚠️ FunASR 加载失败: {e}")
|
||||
return None
|
||||
return funasr_model
|
||||
|
||||
|
||||
def get_vosk_model():
|
||||
"""加载 Vosk 模型(轻量离线,备用)"""
|
||||
global vosk_model
|
||||
if vosk_model is None:
|
||||
try:
|
||||
from vosk import Model
|
||||
if os.path.exists(VOSK_MODEL_PATH):
|
||||
print(f"🔄 加载 Vosk 模型...")
|
||||
vosk_model = Model(VOSK_MODEL_PATH)
|
||||
print("✅ Vosk 模型加载完成")
|
||||
else:
|
||||
print(f"⚠️ Vosk 模型不存在: {VOSK_MODEL_PATH}")
|
||||
return None
|
||||
except Exception as e:
|
||||
print(f"⚠️ Vosk 加载失败: {e}")
|
||||
return None
|
||||
return vosk_model
|
||||
|
||||
|
||||
def allowed_file(filename):
|
||||
return '.' in filename and filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS
|
||||
|
||||
|
||||
def convert_to_wav(input_path, output_path):
|
||||
"""将音频转换为 WAV 格式(16kHz, 单声道)"""
|
||||
try:
|
||||
cmd = [
|
||||
'ffmpeg', '-y', '-i', input_path,
|
||||
'-ar', '16000', '-ac', '1', '-f', 'wav',
|
||||
output_path
|
||||
]
|
||||
subprocess.run(cmd, check=True, capture_output=True, timeout=30)
|
||||
return os.path.exists(output_path)
|
||||
except Exception as e:
|
||||
print(f"转换失败: {e}")
|
||||
return False
|
||||
|
||||
|
||||
def transcribe_with_funasr(wav_path):
|
||||
"""使用 FunASR 转写(首选)"""
|
||||
model = get_funasr_model()
|
||||
if not model:
|
||||
return None, "FunASR 模型未加载"
|
||||
|
||||
try:
|
||||
res = model.generate(input=wav_path, batch_size_s=300)
|
||||
text = res[0]["text"] if res and len(res) > 0 else ""
|
||||
return text, None
|
||||
except Exception as e:
|
||||
return None, f"FunASR 错误: {str(e)}"
|
||||
|
||||
|
||||
def transcribe_with_vosk(wav_path):
|
||||
"""使用 Vosk 转写(备用)"""
|
||||
from vosk import KaldiRecognizer
|
||||
|
||||
model = get_vosk_model()
|
||||
if not model:
|
||||
return None, "Vosk 模型未加载"
|
||||
|
||||
try:
|
||||
wf = wave.open(wav_path, 'rb')
|
||||
if wf.getnchannels() != 1 or wf.getsampwidth() != 2 or wf.getcomptype() != 'NONE':
|
||||
return None, "音频格式不支持"
|
||||
|
||||
rec = KaldiRecognizer(model, wf.getframerate())
|
||||
rec.SetWords(True)
|
||||
|
||||
results = []
|
||||
while True:
|
||||
data = wf.readframes(4000)
|
||||
if len(data) == 0:
|
||||
break
|
||||
if rec.AcceptWaveform(data):
|
||||
result = json.loads(rec.Result())
|
||||
if result.get('text'):
|
||||
results.append(result['text'])
|
||||
|
||||
final_result = json.loads(rec.FinalResult())
|
||||
if final_result.get('text'):
|
||||
results.append(final_result['text'])
|
||||
|
||||
wf.close()
|
||||
return ' '.join(results), None
|
||||
|
||||
except Exception as e:
|
||||
return None, f"Vosk 错误: {str(e)}"
|
||||
|
||||
|
||||
def transcribe_audio_file(audio_path):
|
||||
"""
|
||||
转写音频文件
|
||||
策略:先尝试 FunASR(效果好),失败则回退 Vosk
|
||||
"""
|
||||
# 转换为 WAV
|
||||
wav_path = audio_path + '.wav'
|
||||
if not convert_to_wav(audio_path, wav_path):
|
||||
return None, "音频格式转换失败"
|
||||
|
||||
try:
|
||||
# 1. 优先尝试 FunASR
|
||||
text, error = transcribe_with_funasr(wav_path)
|
||||
if text:
|
||||
return text, "funasr"
|
||||
|
||||
# 2. 回退到 Vosk
|
||||
print(f"⚠️ FunASR 失败({error}),回退到 Vosk...")
|
||||
text, error = transcribe_with_vosk(wav_path)
|
||||
if text:
|
||||
return text, "vosk"
|
||||
|
||||
return None, error or "识别失败"
|
||||
|
||||
finally:
|
||||
# 清理临时文件
|
||||
if os.path.exists(wav_path):
|
||||
os.remove(wav_path)
|
||||
|
||||
|
||||
@app.route('/api/upload', methods=['POST'])
|
||||
def upload_audio():
|
||||
"""接收音频文件上传"""
|
||||
if 'audio' not in request.files:
|
||||
return jsonify({'error': '没有文件'}), 400
|
||||
|
||||
file = request.files['audio']
|
||||
if file.filename == '':
|
||||
return jsonify({'error': '文件名为空'}), 400
|
||||
|
||||
if file and allowed_file(file.filename):
|
||||
timestamp = datetime.now().strftime('%Y%m%d_%H%M%S')
|
||||
filename = f"recording_{timestamp}.webm"
|
||||
filepath = os.path.join(UPLOAD_FOLDER, filename)
|
||||
|
||||
file.save(filepath)
|
||||
file_size = os.path.getsize(filepath)
|
||||
|
||||
return jsonify({
|
||||
'success': True,
|
||||
'filename': filename,
|
||||
'filepath': filepath,
|
||||
'size': file_size,
|
||||
'size_human': f"{file_size / 1024:.1f} KB",
|
||||
'timestamp': timestamp
|
||||
}), 200
|
||||
|
||||
return jsonify({'error': '不支持的文件类型'}), 400
|
||||
|
||||
|
||||
@app.route('/api/transcribe', methods=['POST'])
|
||||
def transcribe():
|
||||
"""语音转文字 API"""
|
||||
if 'audio' not in request.files:
|
||||
return jsonify({'success': False, 'error': '没有音频文件'}), 400
|
||||
|
||||
file = request.files['audio']
|
||||
if file.filename == '':
|
||||
return jsonify({'success': False, 'error': '文件名为空'}), 400
|
||||
|
||||
try:
|
||||
# 保存上传的音频
|
||||
timestamp = datetime.now().strftime('%Y%m%d_%H%M%S')
|
||||
filename = f"recording_{timestamp}.webm"
|
||||
filepath = os.path.join(UPLOAD_FOLDER, filename)
|
||||
file.save(filepath)
|
||||
|
||||
# 转写
|
||||
text, engine = transcribe_audio_file(filepath)
|
||||
|
||||
if text is None:
|
||||
return jsonify({
|
||||
'success': False,
|
||||
'error': engine, # 错误信息
|
||||
'filename': filename
|
||||
}), 500
|
||||
|
||||
return jsonify({
|
||||
'success': True,
|
||||
'text': text or '(未识别到语音)',
|
||||
'engine': engine, # 显示使用的引擎
|
||||
'filename': filename
|
||||
})
|
||||
|
||||
except Exception as e:
|
||||
print(f"转写异常: {e}")
|
||||
return jsonify({
|
||||
'success': False,
|
||||
'error': str(e)
|
||||
}), 500
|
||||
|
||||
|
||||
@app.route('/api/recordings', methods=['GET'])
|
||||
def list_recordings():
|
||||
"""列出所有录音文件"""
|
||||
recordings = []
|
||||
|
||||
for filename in sorted(os.listdir(UPLOAD_FOLDER), reverse=True):
|
||||
if allowed_file(filename):
|
||||
filepath = os.path.join(UPLOAD_FOLDER, filename)
|
||||
stat = os.stat(filepath)
|
||||
recordings.append({
|
||||
'filename': filename,
|
||||
'size': stat.st_size,
|
||||
'size_human': f"{stat.st_size / 1024:.1f} KB",
|
||||
'created': datetime.fromtimestamp(stat.st_mtime).isoformat()
|
||||
})
|
||||
|
||||
return jsonify({
|
||||
'count': len(recordings),
|
||||
'recordings': recordings
|
||||
})
|
||||
|
||||
|
||||
@app.route('/api/recordings/<filename>', methods=['GET'])
|
||||
def get_recording(filename):
|
||||
"""获取单个录音文件"""
|
||||
return send_from_directory(UPLOAD_FOLDER, filename)
|
||||
|
||||
|
||||
@app.route('/api/recordings/<filename>', methods=['DELETE'])
|
||||
def delete_recording(filename):
|
||||
"""删除录音文件"""
|
||||
filepath = os.path.join(UPLOAD_FOLDER, secure_filename(filename))
|
||||
|
||||
if os.path.exists(filepath):
|
||||
os.remove(filepath)
|
||||
return jsonify({'success': True, 'message': '已删除'})
|
||||
|
||||
return jsonify({'error': '文件不存在'}), 404
|
||||
|
||||
|
||||
@app.route('/api/health', methods=['GET'])
|
||||
def health_check():
|
||||
"""健康检查"""
|
||||
vosk_exists = os.path.exists(VOSK_MODEL_PATH)
|
||||
return jsonify({
|
||||
'status': 'ok',
|
||||
'vosk_model': 'available' if vosk_exists else 'not_found',
|
||||
'funasr_model': 'lazy_load', # 懒加载
|
||||
'upload_folder': UPLOAD_FOLDER,
|
||||
'recordings_count': len([f for f in os.listdir(UPLOAD_FOLDER) if allowed_file(f)])
|
||||
})
|
||||
|
||||
|
||||
@app.route('/api/analyze', methods=['POST'])
|
||||
def analyze_text():
|
||||
"""分析转写文字内容"""
|
||||
try:
|
||||
data = request.get_json()
|
||||
if not data or 'text' not in data:
|
||||
return jsonify({'success': False, 'error': '缺少 text 参数'}), 400
|
||||
|
||||
text = data['text'].strip()
|
||||
if len(text) < 10:
|
||||
return jsonify({'success': False, 'error': '文字内容太短(至少10个字符)'}), 400
|
||||
|
||||
# 获取分析类型,默认为 general
|
||||
analysis_type = data.get('type', 'general')
|
||||
|
||||
# 调用大模型分析
|
||||
analysis = call_llm_for_analysis(text, analysis_type)
|
||||
|
||||
return jsonify({
|
||||
'success': True,
|
||||
'analysis': analysis,
|
||||
'type': analysis_type
|
||||
})
|
||||
|
||||
except Exception as e:
|
||||
print(f"分析异常: {e}")
|
||||
return jsonify({'success': False, 'error': str(e)}), 500
|
||||
|
||||
|
||||
def get_analysis_prompt(analysis_type):
|
||||
"""根据分析类型返回对应的系统提示语"""
|
||||
|
||||
prompts = {
|
||||
'meeting': """你是一位专业的会议纪要助手。请对提供的会议内容进行整理,要求:
|
||||
|
||||
## 会议纪要结构:
|
||||
|
||||
📋 会议基本信息:
|
||||
- 会议主题/议题(根据内容推断)
|
||||
- 参会人员(提取提到的角色)
|
||||
- 会议时间(如有提及)
|
||||
|
||||
📌 会议决议/共识:
|
||||
逐条列出会议达成的主要决议,每条包含:
|
||||
- 决议事项
|
||||
- 具体内容和依据
|
||||
|
||||
✅ 行动项(TODO):
|
||||
明确列出需要执行的任务:
|
||||
- 任务内容
|
||||
- 负责人(如提及)
|
||||
- 时间节点/截止日期(如提及)
|
||||
|
||||
⚠️ 待确认/风险点:
|
||||
- 需要进一步明确的事项
|
||||
- 提到的风险或注意事项
|
||||
|
||||
💬 其他重要讨论:
|
||||
记录其他有价值的讨论内容
|
||||
|
||||
⚠️ 要求:
|
||||
- 不歪曲原意
|
||||
- 行动项必须具体可执行
|
||||
- 使用中文回答""",
|
||||
|
||||
'weekly': """你是一位专业的工作周报助手。请对提供的工作内容进行周报整理,要求:
|
||||
|
||||
## 周报结构:
|
||||
|
||||
📊 本周工作完成情况:
|
||||
逐条列出已完成的工作,每条包含:
|
||||
- 工作内容
|
||||
- 完成情况/成果
|
||||
- 相关数据或指标(如有)
|
||||
|
||||
🔄 本周进行中工作:
|
||||
- 工作内容
|
||||
- 当前进度
|
||||
- 预计完成时间
|
||||
|
||||
❓ 遇到的问题/困难:
|
||||
- 问题描述
|
||||
- 已尝试的解决方案
|
||||
- 需要的支持或资源
|
||||
|
||||
📅 下周工作计划:
|
||||
- 计划完成的工作项
|
||||
- 优先级排序
|
||||
- 预期目标
|
||||
|
||||
💡 其他事项:
|
||||
- 心得体会
|
||||
- 建议或想法
|
||||
|
||||
⚠️ 要求:
|
||||
- 量化成果(数字、百分比等)
|
||||
- 问题要具体
|
||||
- 计划要可衡量
|
||||
- 使用中文回答""",
|
||||
|
||||
'daily': """你是一位专业的工作日报助手。请对提供的工作内容进行日报整理,要求:
|
||||
|
||||
## 日报结构:
|
||||
|
||||
✅ 今日完成工作:
|
||||
逐条列出今日完成的工作,按重要性排序:
|
||||
- 工作事项
|
||||
- 完成结果
|
||||
- 耗时(如可推断)
|
||||
|
||||
🔄 进行中工作:
|
||||
- 工作内容
|
||||
- 今日进展
|
||||
- 明日继续计划
|
||||
|
||||
❓ 今日问题/阻塞:
|
||||
- 遇到的问题
|
||||
- 影响程度
|
||||
- 尝试的解决方案
|
||||
|
||||
📅 明日工作计划:
|
||||
- 计划工作项
|
||||
- 预期目标
|
||||
- 需要协调的事项
|
||||
|
||||
🙋 需要支持:
|
||||
- 需要领导/同事协助的事项
|
||||
- 需要的资源
|
||||
|
||||
⚠️ 要求:
|
||||
- 简洁明了
|
||||
- 问题不回避
|
||||
- 计划具体
|
||||
- 使用中文回答""",
|
||||
|
||||
'general': """你是一位专业的内容分析专家。请对提供的文字内容进行深度分析和总结,要求:
|
||||
|
||||
## 分析原则:
|
||||
1. 忠实原文:不歪曲、不遗漏原文的核心信息
|
||||
2. 深入挖掘:提取隐含的观点、态度和意图
|
||||
3. 结构清晰:层次分明,便于阅读理解
|
||||
4. 内容充实:每个要点都要有具体支撑
|
||||
|
||||
## 输出格式:
|
||||
|
||||
📌 核心要点(3-5条):
|
||||
每条要点需包含:
|
||||
- 要点概括(一句话)
|
||||
- 具体内容(详细说明,引用原文关键句)
|
||||
|
||||
🔍 深度分析:
|
||||
- 说话者/作者的主要观点或立场
|
||||
- 情绪态度(积极/消极/中性)
|
||||
- 潜在的意图或目的
|
||||
|
||||
💡 关键信息提取:
|
||||
- 涉及的人物、时间、地点
|
||||
- 重要数据或事实
|
||||
- 行动项或待办事项
|
||||
|
||||
📝 详细总结:
|
||||
用3-5句话全面概括全文内容
|
||||
|
||||
⚠️ 注意事项:
|
||||
- 不要添加原文没有的信息
|
||||
- 不要过度解读,保持客观
|
||||
- 使用中文回答"""
|
||||
}
|
||||
|
||||
return prompts.get(analysis_type, prompts['general'])
|
||||
|
||||
|
||||
def call_llm_for_analysis(text, analysis_type='general'):
|
||||
"""调用 Moonshot 大模型进行内容分析总结"""
|
||||
|
||||
system_prompt = get_analysis_prompt(analysis_type)
|
||||
|
||||
try:
|
||||
# 使用 Moonshot API
|
||||
import openai
|
||||
|
||||
api_key = os.environ.get('MOONSHOT_API_KEY')
|
||||
if not api_key:
|
||||
print("Moonshot API key 未配置")
|
||||
return simple_analysis(text)
|
||||
|
||||
# Moonshot 使用 OpenAI 兼容接口
|
||||
client = openai.OpenAI(
|
||||
api_key=api_key,
|
||||
base_url="https://api.moonshot.cn/v1"
|
||||
)
|
||||
|
||||
# 根据类型调整提示语
|
||||
type_names = {
|
||||
'meeting': '会议',
|
||||
'weekly': '周报',
|
||||
'daily': '日报',
|
||||
'general': '内容'
|
||||
}
|
||||
type_name = type_names.get(analysis_type, '内容')
|
||||
|
||||
response = client.chat.completions.create(
|
||||
model="moonshot-v1-8k", # 使用 8k 上下文模型
|
||||
messages=[
|
||||
{"role": "system", "content": system_prompt},
|
||||
{"role": "user", "content": f"请对以下{type_name}进行整理分析:\n\n{text}"}
|
||||
],
|
||||
temperature=0.3,
|
||||
max_tokens=2500 # 增加token数以获得更详细的回答
|
||||
)
|
||||
|
||||
result = response.choices[0].message.content
|
||||
print(f"Moonshot 分析完成,返回 {len(result)} 字符")
|
||||
return result
|
||||
|
||||
except Exception as e:
|
||||
print(f"Moonshot API 调用失败: {e}")
|
||||
# 失败时回退到本地分析
|
||||
return simple_analysis(text)
|
||||
|
||||
|
||||
def simple_analysis(text):
|
||||
"""本地简单分析(无需外部 API)"""
|
||||
lines = text.split('\n')
|
||||
sentences = [s.strip() for s in text.replace('。', '\n').replace('!', '\n').replace('?', '\n').split('\n') if s.strip()]
|
||||
|
||||
# 提取关键句子(长度适中,包含信息量)
|
||||
key_sentences = [s for s in sentences if 20 <= len(s) <= 100][:5]
|
||||
|
||||
# 统计信息
|
||||
char_count = len(text)
|
||||
word_count = len(text.split())
|
||||
|
||||
result = f"""📌 核心要点:
|
||||
"""
|
||||
if key_sentences:
|
||||
for i, sent in enumerate(key_sentences[:4], 1):
|
||||
result += f"{i}. {sent}\n"
|
||||
else:
|
||||
result += "1. (内容较短,未提取到明显要点)\n"
|
||||
|
||||
result += f"""
|
||||
💡 关键信息:
|
||||
- 总字数:约 {char_count} 字
|
||||
- 句子数:约 {len(sentences)} 句
|
||||
- 内容类型:{"对话/访谈" if "?" in text or "说" in text else "叙述/说明"}
|
||||
|
||||
📝 简要总结:
|
||||
{text[:100]}{"..." if len(text) > 100 else ""}
|
||||
"""
|
||||
return result.strip()
|
||||
|
||||
|
||||
@app.route('/')
|
||||
def index():
|
||||
return send_from_directory('../frontend', 'index.html')
|
||||
|
||||
|
||||
@app.route('/<path:path>')
|
||||
def static_files(path):
|
||||
return send_from_directory('../frontend', path)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
cert_file = '/home/ubuntu/nanocode/audio-recorder/cert.pem'
|
||||
key_file = '/home/ubuntu/nanocode/audio-recorder/key.pem'
|
||||
|
||||
print(f"🎙️ 录音服务启动中...")
|
||||
print(f"📁 上传目录: {UPLOAD_FOLDER}")
|
||||
print(f"🔤 Vosk 模型: {VOSK_MODEL_PATH}")
|
||||
print(f"🤖 FunASR: 延迟加载(首次转写时加载,约100MB内存)")
|
||||
|
||||
# 检查证书
|
||||
if os.path.exists(cert_file) and os.path.exists(key_file):
|
||||
print(f"🔒 HTTPS 模式: https://0.0.0.0:8891")
|
||||
context = ssl.SSLContext(ssl.PROTOCOL_TLS_SERVER)
|
||||
context.load_cert_chain(cert_file, key_file)
|
||||
|
||||
app.run(
|
||||
host='0.0.0.0',
|
||||
port=8891,
|
||||
ssl_context=context,
|
||||
debug=False
|
||||
)
|
||||
else:
|
||||
print(f"⚠️ 证书不存在,使用 HTTP: http://0.0.0.0:8890")
|
||||
app.run(
|
||||
host='0.0.0.0',
|
||||
port=8890,
|
||||
debug=False
|
||||
)
|
||||
@@ -0,0 +1,123 @@
|
||||
#!/usr/bin/env python3
|
||||
"""
|
||||
录音后端服务 - HTTPS 版本
|
||||
"""
|
||||
from flask import Flask, request, jsonify, send_from_directory
|
||||
from flask_cors import CORS
|
||||
from werkzeug.utils import secure_filename
|
||||
import os
|
||||
import ssl
|
||||
from datetime import datetime
|
||||
|
||||
app = Flask(__name__)
|
||||
CORS(app)
|
||||
|
||||
# 配置
|
||||
UPLOAD_FOLDER = '/home/ubuntu/nanocode/audio-recorder/uploads'
|
||||
ALLOWED_EXTENSIONS = {'webm', 'mp4', 'wav', 'mp3', 'ogg'}
|
||||
MAX_CONTENT_LENGTH = 100 * 1024 * 1024 # 100MB
|
||||
|
||||
app.config['MAX_CONTENT_LENGTH'] = MAX_CONTENT_LENGTH
|
||||
|
||||
# 确保上传目录存在
|
||||
os.makedirs(UPLOAD_FOLDER, exist_ok=True)
|
||||
|
||||
|
||||
def allowed_file(filename):
|
||||
return '.' in filename and filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS
|
||||
|
||||
|
||||
@app.route('/api/upload', methods=['POST'])
|
||||
def upload_audio():
|
||||
"""接收音频文件上传"""
|
||||
if 'audio' not in request.files:
|
||||
return jsonify({'error': '没有文件'}), 400
|
||||
|
||||
file = request.files['audio']
|
||||
if file.filename == '':
|
||||
return jsonify({'error': '文件名为空'}), 400
|
||||
|
||||
if file and allowed_file(file.filename):
|
||||
timestamp = datetime.now().strftime('%Y%m%d_%H%M%S')
|
||||
filename = f"recording_{timestamp}.webm"
|
||||
filepath = os.path.join(UPLOAD_FOLDER, filename)
|
||||
|
||||
file.save(filepath)
|
||||
file_size = os.path.getsize(filepath)
|
||||
|
||||
return jsonify({
|
||||
'success': True,
|
||||
'filename': filename,
|
||||
'filepath': filepath,
|
||||
'size': file_size,
|
||||
'size_human': f"{file_size / 1024:.1f} KB",
|
||||
'timestamp': timestamp
|
||||
}), 200
|
||||
|
||||
return jsonify({'error': '不支持的文件类型'}), 400
|
||||
|
||||
|
||||
@app.route('/api/recordings', methods=['GET'])
|
||||
def list_recordings():
|
||||
"""列出所有录音文件"""
|
||||
recordings = []
|
||||
|
||||
for filename in sorted(os.listdir(UPLOAD_FOLDER), reverse=True):
|
||||
if allowed_file(filename):
|
||||
filepath = os.path.join(UPLOAD_FOLDER, filename)
|
||||
stat = os.stat(filepath)
|
||||
recordings.append({
|
||||
'filename': filename,
|
||||
'size': stat.st_size,
|
||||
'size_human': f"{stat.st_size / 1024:.1f} KB",
|
||||
'created': datetime.fromtimestamp(stat.st_mtime).isoformat()
|
||||
})
|
||||
|
||||
return jsonify({
|
||||
'count': len(recordings),
|
||||
'recordings': recordings
|
||||
})
|
||||
|
||||
|
||||
@app.route('/api/recordings/<filename>', methods=['GET'])
|
||||
def get_recording(filename):
|
||||
"""获取单个录音文件"""
|
||||
return send_from_directory(UPLOAD_FOLDER, filename)
|
||||
|
||||
|
||||
@app.route('/api/health', methods=['GET'])
|
||||
def health_check():
|
||||
"""健康检查"""
|
||||
return jsonify({
|
||||
'status': 'ok',
|
||||
'upload_folder': UPLOAD_FOLDER,
|
||||
'recordings_count': len([f for f in os.listdir(UPLOAD_FOLDER) if allowed_file(f)])
|
||||
})
|
||||
|
||||
|
||||
@app.route('/')
|
||||
def index():
|
||||
return send_from_directory('../frontend', 'index.html')
|
||||
|
||||
|
||||
@app.route('/<path:path>')
|
||||
def static_files(path):
|
||||
return send_from_directory('../frontend', path)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
context = ssl.SSLContext(ssl.PROTOCOL_TLS_SERVER)
|
||||
context.load_cert_chain('/home/ubuntu/nanocode/audio-recorder/cert.pem',
|
||||
'/home/ubuntu/nanocode/audio-recorder/key.pem')
|
||||
|
||||
print(f"🎙️ HTTPS 录音服务启动中...")
|
||||
print(f"📁 上传目录: {UPLOAD_FOLDER}")
|
||||
print(f"🔒 HTTPS 地址: https://129.211.173.87:8891")
|
||||
print(f"⚠️ 由于是自签名证书,浏览器会显示安全警告,请点击'高级'->'继续访问'")
|
||||
|
||||
app.run(
|
||||
host='0.0.0.0',
|
||||
port=8891,
|
||||
ssl_context=context,
|
||||
debug=False
|
||||
)
|
||||
@@ -0,0 +1,182 @@
|
||||
#!/usr/bin/env python3
|
||||
"""
|
||||
录音后端服务 V2 - 集成语音转文字
|
||||
"""
|
||||
import warnings
|
||||
warnings.filterwarnings('ignore')
|
||||
|
||||
from flask import Flask, request, jsonify, send_from_directory
|
||||
from flask_cors import CORS
|
||||
from werkzeug.utils import secure_filename
|
||||
import os
|
||||
import ssl
|
||||
from datetime import datetime
|
||||
|
||||
# 导入转写模块 - 使用 Vosk
|
||||
from transcribe_api import transcribe_audio
|
||||
|
||||
MODEL_PATH = '/home/ubuntu/nanocode/audio-recorder/models/cn-model'
|
||||
|
||||
app = Flask(__name__)
|
||||
CORS(app)
|
||||
|
||||
# 配置
|
||||
UPLOAD_FOLDER = '/home/ubuntu/nanocode/audio-recorder/uploads'
|
||||
ALLOWED_EXTENSIONS = {'webm', 'mp4', 'wav', 'mp3', 'ogg'}
|
||||
MAX_CONTENT_LENGTH = 100 * 1024 * 1024
|
||||
|
||||
app.config['MAX_CONTENT_LENGTH'] = MAX_CONTENT_LENGTH
|
||||
os.makedirs(UPLOAD_FOLDER, exist_ok=True)
|
||||
|
||||
|
||||
def allowed_file(filename):
|
||||
return '.' in filename and filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS
|
||||
|
||||
|
||||
@app.route('/api/upload', methods=['POST'])
|
||||
def upload_audio():
|
||||
"""接收音频文件上传"""
|
||||
if 'audio' not in request.files:
|
||||
return jsonify({'error': '没有文件'}), 400
|
||||
|
||||
file = request.files['audio']
|
||||
if file.filename == '':
|
||||
return jsonify({'error': '文件名为空'}), 400
|
||||
|
||||
if file and allowed_file(file.filename):
|
||||
timestamp = datetime.now().strftime('%Y%m%d_%H%M%S')
|
||||
filename = f"recording_{timestamp}.webm"
|
||||
filepath = os.path.join(UPLOAD_FOLDER, filename)
|
||||
|
||||
file.save(filepath)
|
||||
file_size = os.path.getsize(filepath)
|
||||
|
||||
return jsonify({
|
||||
'success': True,
|
||||
'filename': filename,
|
||||
'filepath': filepath,
|
||||
'size': file_size,
|
||||
'size_human': f"{file_size / 1024:.1f} KB",
|
||||
'timestamp': timestamp
|
||||
}), 200
|
||||
|
||||
return jsonify({'error': '不支持的文件类型'}), 400
|
||||
|
||||
|
||||
@app.route('/api/transcribe', methods=['POST'])
|
||||
def transcribe():
|
||||
"""语音转文字"""
|
||||
if 'audio' not in request.files:
|
||||
return jsonify({'error': '没有音频文件'}), 400
|
||||
|
||||
file = request.files['audio']
|
||||
if file.filename == '':
|
||||
return jsonify({'error': '文件名为空'}), 400
|
||||
|
||||
try:
|
||||
# 保存上传的音频
|
||||
timestamp = datetime.now().strftime('%Y%m%d_%H%M%S')
|
||||
filename = f"recording_{timestamp}.webm"
|
||||
filepath = os.path.join(UPLOAD_FOLDER, filename)
|
||||
file.save(filepath)
|
||||
|
||||
print(f"🎵 收到音频: {filename}, 大小: {os.path.getsize(filepath)} bytes")
|
||||
|
||||
# 转写
|
||||
text, error = transcribe_audio(filepath)
|
||||
|
||||
if error:
|
||||
print(f"❌ 转写失败: {error}")
|
||||
return jsonify({
|
||||
'success': False,
|
||||
'error': error,
|
||||
'filename': filename
|
||||
}), 500
|
||||
|
||||
print(f"✅ 转写成功: {text[:50]}...")
|
||||
|
||||
return jsonify({
|
||||
'success': True,
|
||||
'text': text or '(未识别到语音)',
|
||||
'filename': filename,
|
||||
'model_type': 'vosk' if os.path.exists(MODEL_PATH) else 'mock'
|
||||
})
|
||||
|
||||
except Exception as e:
|
||||
print(f"❌ 服务器错误: {e}")
|
||||
return jsonify({
|
||||
'success': False,
|
||||
'error': str(e)
|
||||
}), 500
|
||||
|
||||
|
||||
@app.route('/api/recordings', methods=['GET'])
|
||||
def list_recordings():
|
||||
"""列出所有录音文件"""
|
||||
recordings = []
|
||||
|
||||
for filename in sorted(os.listdir(UPLOAD_FOLDER), reverse=True):
|
||||
if allowed_file(filename):
|
||||
filepath = os.path.join(UPLOAD_FOLDER, filename)
|
||||
stat = os.stat(filepath)
|
||||
recordings.append({
|
||||
'filename': filename,
|
||||
'size': stat.st_size,
|
||||
'size_human': f"{stat.st_size / 1024:.1f} KB",
|
||||
'created': datetime.fromtimestamp(stat.st_mtime).isoformat()
|
||||
})
|
||||
|
||||
return jsonify({
|
||||
'count': len(recordings),
|
||||
'recordings': recordings
|
||||
})
|
||||
|
||||
|
||||
@app.route('/api/recordings/<filename>', methods=['GET'])
|
||||
def get_recording(filename):
|
||||
"""获取单个录音文件"""
|
||||
return send_from_directory(UPLOAD_FOLDER, filename)
|
||||
|
||||
|
||||
@app.route('/api/health', methods=['GET'])
|
||||
def health_check():
|
||||
"""健康检查"""
|
||||
model_exists = os.path.exists(MODEL_PATH)
|
||||
return jsonify({
|
||||
'status': 'ok',
|
||||
'model_loaded': model_exists,
|
||||
'model_path': MODEL_PATH,
|
||||
'upload_folder': UPLOAD_FOLDER,
|
||||
'recordings_count': len([f for f in os.listdir(UPLOAD_FOLDER) if allowed_file(f)])
|
||||
})
|
||||
|
||||
|
||||
@app.route('/')
|
||||
def index():
|
||||
return send_from_directory('../frontend', 'index.html')
|
||||
|
||||
|
||||
@app.route('/<path:path>')
|
||||
def static_files(path):
|
||||
return send_from_directory('../frontend', path)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
context = ssl.SSLContext(ssl.PROTOCOL_TLS_SERVER)
|
||||
context.load_cert_chain(
|
||||
'/home/ubuntu/nanocode/audio-recorder/cert.pem',
|
||||
'/home/ubuntu/nanocode/audio-recorder/key.pem'
|
||||
)
|
||||
|
||||
print(f"🎙️ HTTPS 录音服务 V2 启动中...")
|
||||
print(f"📁 上传目录: {UPLOAD_FOLDER}")
|
||||
print(f"🔤 模型路径: {MODEL_PATH}")
|
||||
print(f"🔒 HTTPS 地址: https://129.211.173.87:8891")
|
||||
print(f"⚠️ 提示: {'✅ 模型已安装' if os.path.exists(MODEL_PATH) else '⚠️ 使用模拟转写,请安装模型'}")
|
||||
|
||||
app.run(
|
||||
host='0.0.0.0',
|
||||
port=8891,
|
||||
ssl_context=context,
|
||||
debug=False
|
||||
)
|
||||
@@ -0,0 +1,300 @@
|
||||
#!/usr/bin/env python3
|
||||
"""
|
||||
录音后端服务 - 集成语音转文字
|
||||
"""
|
||||
from flask import Flask, request, jsonify, send_from_directory
|
||||
from flask_cors import CORS
|
||||
from werkzeug.utils import secure_filename
|
||||
import os
|
||||
import ssl
|
||||
import wave
|
||||
import json
|
||||
from datetime import datetime
|
||||
|
||||
app = Flask(__name__)
|
||||
CORS(app)
|
||||
|
||||
# 配置
|
||||
UPLOAD_FOLDER = '/home/ubuntu/nanocode/audio-recorder/uploads'
|
||||
MODEL_PATH = '/home/ubuntu/nanocode/audio-recorder/models/cn-model'
|
||||
ALLOWED_EXTENSIONS = {'webm', 'mp4', 'wav', 'mp3', 'ogg'}
|
||||
MAX_CONTENT_LENGTH = 100 * 1024 * 1024
|
||||
|
||||
app.config['MAX_CONTENT_LENGTH'] = MAX_CONTENT_LENGTH
|
||||
os.makedirs(UPLOAD_FOLDER, exist_ok=True)
|
||||
|
||||
# 全局 ASR 模型(延迟加载)
|
||||
funasr_model = None
|
||||
vosk_model = None
|
||||
|
||||
def get_funasr_model():
|
||||
"""加载 FunASR 模型(阿里,中文识别效果好)- 优先使用"""
|
||||
global funasr_model
|
||||
if funasr_model is None:
|
||||
try:
|
||||
from funasr import AutoModel
|
||||
print("🔄 加载 FunASR 模型(阿里达摩院,中文优先)...")
|
||||
funasr_model = AutoModel(
|
||||
model="paraformer-zh",
|
||||
vad_model="fsmn-vad",
|
||||
punc_model="ct-punc",
|
||||
)
|
||||
print("✅ FunASR 模型加载完成")
|
||||
except Exception as e:
|
||||
print(f"⚠️ FunASR 加载失败: {e}")
|
||||
return None
|
||||
return funasr_model
|
||||
|
||||
def get_vosk_model():
|
||||
"""加载 Vosk 模型(备用)"""
|
||||
global vosk_model
|
||||
if vosk_model is None:
|
||||
try:
|
||||
from vosk import Model
|
||||
if os.path.exists(MODEL_PATH):
|
||||
print(f"🔄 加载 Vosk 模型(备用)...")
|
||||
vosk_model = Model(MODEL_PATH)
|
||||
print("✅ Vosk 模型加载完成")
|
||||
else:
|
||||
print(f"⚠️ Vosk 模型不存在: {MODEL_PATH}")
|
||||
return None
|
||||
except Exception as e:
|
||||
print(f"⚠️ Vosk 加载失败: {e}")
|
||||
return None
|
||||
return vosk_model
|
||||
|
||||
def allowed_file(filename):
|
||||
return '.' in filename and filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS
|
||||
|
||||
def convert_to_wav(input_path, output_path):
|
||||
"""将音频转换为 WAV 格式(16kHz, 单声道)"""
|
||||
import subprocess
|
||||
try:
|
||||
cmd = [
|
||||
'ffmpeg', '-y', '-i', input_path,
|
||||
'-ar', '16000', '-ac', '1', '-f', 'wav',
|
||||
output_path
|
||||
]
|
||||
subprocess.run(cmd, check=True, capture_output=True, timeout=30)
|
||||
return True
|
||||
except Exception as e:
|
||||
print(f"转换失败: {e}")
|
||||
return False
|
||||
|
||||
def transcribe_with_funasr(audio_path):
|
||||
"""使用 FunASR 转写(优先,中文效果好)"""
|
||||
model = get_funasr_model()
|
||||
if not model:
|
||||
return None, "FunASR 模型未加载"
|
||||
|
||||
try:
|
||||
res = model.generate(input=audio_path, batch_size_s=300)
|
||||
text = res[0]["text"] if res and len(res) > 0 else ""
|
||||
return text, None
|
||||
except Exception as e:
|
||||
return None, f"FunASR 错误: {str(e)}"
|
||||
|
||||
def transcribe_with_vosk(audio_path):
|
||||
"""使用 Vosk 转写音频"""
|
||||
from vosk import KaldiRecognizer
|
||||
|
||||
model = get_asr_model()
|
||||
if not model:
|
||||
return None, "模型未加载"
|
||||
|
||||
try:
|
||||
# 转换为 WAV
|
||||
wav_path = audio_path + '.wav'
|
||||
if not convert_to_wav(audio_path, wav_path):
|
||||
return None, "音频转换失败"
|
||||
|
||||
# 读取 WAV 文件
|
||||
wf = wave.open(wav_path, 'rb')
|
||||
if wf.getnchannels() != 1 or wf.getsampwidth() != 2 or wf.getcomptype() != 'NONE':
|
||||
return None, "音频格式不支持"
|
||||
|
||||
# 创建识别器
|
||||
rec = KaldiRecognizer(model, wf.getframerate())
|
||||
rec.SetWords(True)
|
||||
|
||||
# 识别
|
||||
results = []
|
||||
while True:
|
||||
data = wf.readframes(4000)
|
||||
if len(data) == 0:
|
||||
break
|
||||
if rec.AcceptWaveform(data):
|
||||
result = json.loads(rec.Result())
|
||||
if result.get('text'):
|
||||
results.append(result['text'])
|
||||
|
||||
# 获取最终结果
|
||||
final_result = json.loads(rec.FinalResult())
|
||||
if final_result.get('text'):
|
||||
results.append(final_result['text'])
|
||||
|
||||
# 清理临时文件
|
||||
wf.close()
|
||||
if os.path.exists(wav_path):
|
||||
os.remove(wav_path)
|
||||
|
||||
return ' '.join(results), None
|
||||
|
||||
except Exception as e:
|
||||
return None, str(e)
|
||||
|
||||
@app.route('/api/upload', methods=['POST'])
|
||||
def upload_audio():
|
||||
"""接收音频文件上传"""
|
||||
if 'audio' not in request.files:
|
||||
return jsonify({'error': '没有文件'}), 400
|
||||
|
||||
file = request.files['audio']
|
||||
if file.filename == '':
|
||||
return jsonify({'error': '文件名为空'}), 400
|
||||
|
||||
if file and allowed_file(file.filename):
|
||||
timestamp = datetime.now().strftime('%Y%m%d_%H%M%S')
|
||||
filename = f"recording_{timestamp}.webm"
|
||||
filepath = os.path.join(UPLOAD_FOLDER, filename)
|
||||
|
||||
file.save(filepath)
|
||||
file_size = os.path.getsize(filepath)
|
||||
|
||||
return jsonify({
|
||||
'success': True,
|
||||
'filename': filename,
|
||||
'filepath': filepath,
|
||||
'size': file_size,
|
||||
'size_human': f"{file_size / 1024:.1f} KB",
|
||||
'timestamp': timestamp
|
||||
}), 200
|
||||
|
||||
return jsonify({'error': '不支持的文件类型'}), 400
|
||||
|
||||
@app.route('/api/transcribe', methods=['POST'])
|
||||
def transcribe():
|
||||
"""语音转文字 - 优先使用 FunASR(中文效果好),失败回退 Vosk"""
|
||||
if 'audio' not in request.files:
|
||||
return jsonify({'error': '没有音频文件'}), 400
|
||||
|
||||
file = request.files['audio']
|
||||
if file.filename == '':
|
||||
return jsonify({'error': '文件名为空'}), 400
|
||||
|
||||
try:
|
||||
# 保存上传的音频
|
||||
timestamp = datetime.now().strftime('%Y%m%d_%H%M%S')
|
||||
filename = f"recording_{timestamp}.webm"
|
||||
filepath = os.path.join(UPLOAD_FOLDER, filename)
|
||||
file.save(filepath)
|
||||
|
||||
# 转换为 WAV 格式(两种引擎都需要)
|
||||
wav_path = filepath + '.wav'
|
||||
if not convert_to_wav(filepath, wav_path):
|
||||
return jsonify({
|
||||
'success': False,
|
||||
'error': '音频格式转换失败'
|
||||
}), 500
|
||||
|
||||
# 1. 优先使用 FunASR(阿里,中文识别效果好)
|
||||
text, error = transcribe_with_funasr(wav_path)
|
||||
engine = "funasr"
|
||||
|
||||
# 2. FunASR 失败,回退到 Vosk
|
||||
if not text:
|
||||
print(f"⚠️ FunASR 失败({error}),回退到 Vosk...")
|
||||
text, error = transcribe_with_vosk(wav_path)
|
||||
engine = "vosk"
|
||||
|
||||
# 清理临时 WAV 文件
|
||||
if os.path.exists(wav_path):
|
||||
os.remove(wav_path)
|
||||
|
||||
if error and not text:
|
||||
return jsonify({
|
||||
'success': False,
|
||||
'error': error,
|
||||
'filename': filename
|
||||
}), 500
|
||||
|
||||
return jsonify({
|
||||
'success': True,
|
||||
'text': text or '(未识别到语音)',
|
||||
'filename': filename,
|
||||
'engine': engine
|
||||
})
|
||||
|
||||
except Exception as e:
|
||||
return jsonify({
|
||||
'success': False,
|
||||
'error': str(e)
|
||||
}), 500
|
||||
|
||||
@app.route('/api/recordings', methods=['GET'])
|
||||
def list_recordings():
|
||||
"""列出所有录音文件"""
|
||||
recordings = []
|
||||
|
||||
for filename in sorted(os.listdir(UPLOAD_FOLDER), reverse=True):
|
||||
if allowed_file(filename):
|
||||
filepath = os.path.join(UPLOAD_FOLDER, filename)
|
||||
stat = os.stat(filepath)
|
||||
recordings.append({
|
||||
'filename': filename,
|
||||
'size': stat.st_size,
|
||||
'size_human': f"{stat.st_size / 1024:.1f} KB",
|
||||
'created': datetime.fromtimestamp(stat.st_mtime).isoformat()
|
||||
})
|
||||
|
||||
return jsonify({
|
||||
'count': len(recordings),
|
||||
'recordings': recordings
|
||||
})
|
||||
|
||||
@app.route('/api/recordings/<filename>', methods=['GET'])
|
||||
def get_recording(filename):
|
||||
"""获取单个录音文件"""
|
||||
return send_from_directory(UPLOAD_FOLDER, filename)
|
||||
|
||||
@app.route('/api/health', methods=['GET'])
|
||||
def health_check():
|
||||
"""健康检查"""
|
||||
model_exists = os.path.exists(MODEL_PATH)
|
||||
return jsonify({
|
||||
'status': 'ok',
|
||||
'model_loaded': model_exists,
|
||||
'model_path': MODEL_PATH,
|
||||
'upload_folder': UPLOAD_FOLDER,
|
||||
'recordings_count': len([f for f in os.listdir(UPLOAD_FOLDER) if allowed_file(f)])
|
||||
})
|
||||
|
||||
@app.route('/')
|
||||
def index():
|
||||
return send_from_directory('../frontend', 'index.html')
|
||||
|
||||
@app.route('/<path:path>')
|
||||
def static_files(path):
|
||||
return send_from_directory('../frontend', path)
|
||||
|
||||
if __name__ == '__main__':
|
||||
context = ssl.SSLContext(ssl.PROTOCOL_TLS_SERVER)
|
||||
context.load_cert_chain(
|
||||
'/home/ubuntu/nanocode/audio-recorder/cert.pem',
|
||||
'/home/ubuntu/nanocode/audio-recorder/key.pem'
|
||||
)
|
||||
|
||||
print(f"🎙️ HTTPS 录音服务启动中...")
|
||||
print(f"📁 上传目录: {UPLOAD_FOLDER}")
|
||||
print(f"🔤 模型路径: {MODEL_PATH}")
|
||||
print(f"🔒 HTTPS 地址: https://129.211.173.87:8891")
|
||||
|
||||
if not os.path.exists(MODEL_PATH):
|
||||
print(f"⚠️ 警告: ASR 模型未找到,请先下载模型")
|
||||
|
||||
app.run(
|
||||
host='0.0.0.0',
|
||||
port=8891,
|
||||
ssl_context=context,
|
||||
debug=False
|
||||
)
|
||||
@@ -0,0 +1,4 @@
|
||||
Flask==3.0.0
|
||||
Flask-CORS==4.0.0
|
||||
Werkzeug==3.0.1
|
||||
gunicorn==21.2.0
|
||||
@@ -0,0 +1,146 @@
|
||||
#!/usr/bin/env python3
|
||||
"""
|
||||
语音转文字 - 使用在线 API 方案
|
||||
支持:
|
||||
1. Vosk 本地模型(如果有)
|
||||
2. Whisper API(OpenAI/DeepSeek)
|
||||
3. 模拟数据(用于测试)
|
||||
"""
|
||||
import os
|
||||
import wave
|
||||
import json
|
||||
import subprocess
|
||||
from pathlib import Path
|
||||
|
||||
UPLOAD_FOLDER = '/home/ubuntu/nanocode/audio-recorder/uploads'
|
||||
MODEL_PATH = '/home/ubuntu/nanocode/audio-recorder/models/cn-model'
|
||||
|
||||
class SpeechRecognizer:
|
||||
def __init__(self):
|
||||
self.model = None
|
||||
self.model_type = self._detect_model()
|
||||
|
||||
def _detect_model(self):
|
||||
"""检测可用的 ASR 模型"""
|
||||
if os.path.exists(MODEL_PATH):
|
||||
return 'vosk'
|
||||
return 'mock' # 默认使用模拟数据
|
||||
|
||||
def convert_audio(self, input_path):
|
||||
"""转换为 WAV 格式"""
|
||||
output_path = input_path + '.wav'
|
||||
try:
|
||||
cmd = [
|
||||
'ffmpeg', '-y', '-i', input_path,
|
||||
'-ar', '16000', '-ac', '1', '-f', 'wav',
|
||||
output_path
|
||||
]
|
||||
result = subprocess.run(cmd, capture_output=True, text=True, timeout=30)
|
||||
if result.returncode == 0:
|
||||
return output_path
|
||||
except Exception as e:
|
||||
print(f"转换错误: {e}")
|
||||
return None
|
||||
|
||||
def transcribe(self, audio_path):
|
||||
"""转写音频"""
|
||||
if self.model_type == 'vosk':
|
||||
return self._transcribe_vosk(audio_path)
|
||||
else:
|
||||
return self._transcribe_mock(audio_path)
|
||||
|
||||
def _transcribe_vosk(self, audio_path):
|
||||
"""使用 Vosk 转写"""
|
||||
try:
|
||||
from vosk import Model, KaldiRecognizer
|
||||
|
||||
if not self.model:
|
||||
self.model = Model(MODEL_PATH)
|
||||
|
||||
# 转换为 WAV
|
||||
wav_path = self.convert_audio(audio_path)
|
||||
if not wav_path:
|
||||
return None, "音频转换失败"
|
||||
|
||||
# 读取音频
|
||||
wf = wave.open(wav_path, 'rb')
|
||||
if wf.getnchannels() != 1 or wf.getsampwidth() != 2:
|
||||
wf.close()
|
||||
os.remove(wav_path)
|
||||
return None, "音频格式不支持"
|
||||
|
||||
# 识别
|
||||
rec = KaldiRecognizer(self.model, wf.getframerate())
|
||||
rec.SetWords(True)
|
||||
|
||||
results = []
|
||||
while True:
|
||||
data = wf.readframes(4000)
|
||||
if len(data) == 0:
|
||||
break
|
||||
if rec.AcceptWaveform(data):
|
||||
result = json.loads(rec.Result())
|
||||
if result.get('text'):
|
||||
results.append(result['text'])
|
||||
|
||||
final = json.loads(rec.FinalResult())
|
||||
if final.get('text'):
|
||||
results.append(final['text'])
|
||||
|
||||
wf.close()
|
||||
os.remove(wav_path)
|
||||
|
||||
return ' '.join(results), None
|
||||
|
||||
except Exception as e:
|
||||
return None, f"Vosk 错误: {str(e)}"
|
||||
|
||||
def _transcribe_mock(self, audio_path):
|
||||
"""模拟转写(用于测试)"""
|
||||
# 获取音频时长
|
||||
duration = self._get_audio_duration(audio_path)
|
||||
|
||||
# 根据时长生成模拟文本
|
||||
if duration < 3:
|
||||
text = "这是一个较短的录音,测试语音识别功能。"
|
||||
elif duration < 10:
|
||||
text = """这是一个测试录音。
|
||||
|
||||
语音识别功能正在开发中。
|
||||
|
||||
目前使用模拟数据展示界面效果。
|
||||
|
||||
请在服务器上安装 Vosk 模型以获得真实转写功能。"""
|
||||
else:
|
||||
text = """这是一个较长的录音测试。
|
||||
|
||||
当前使用的是模拟转写功能。
|
||||
|
||||
真实转写需要安装 Vosk 中文模型:
|
||||
|
||||
1. cd /home/ubuntu/nanocode/audio-recorder/models
|
||||
2. wget https://alphacephei.com/vosk/models/vosk-model-small-cn-0.22.zip
|
||||
3. unzip vosk-model-small-cn-0.22.zip
|
||||
4. mv vosk-model-small-cn-0.22 cn-model
|
||||
5. 重启后端服务
|
||||
|
||||
或者使用在线 API 方案(DeepSeek/Whisper)。"""
|
||||
|
||||
return text, None
|
||||
|
||||
def _get_audio_duration(self, audio_path):
|
||||
"""获取音频时长(秒)"""
|
||||
try:
|
||||
cmd = ['ffprobe', '-v', 'error', '-show_entries',
|
||||
'format=duration', '-of', 'default=noprint_wrappers=1:nokey=1', audio_path]
|
||||
result = subprocess.run(cmd, capture_output=True, text=True, timeout=5)
|
||||
return float(result.stdout.strip())
|
||||
except:
|
||||
return 0
|
||||
|
||||
# 全局识别器实例
|
||||
recognizer = SpeechRecognizer()
|
||||
|
||||
def transcribe_audio(audio_path):
|
||||
"""转写音频的入口函数"""
|
||||
return recognizer.transcribe(audio_path)
|
||||
@@ -0,0 +1,142 @@
|
||||
#!/usr/bin/env python3
|
||||
"""
|
||||
语音转文字 - FunASR 版本(阿里达摩院)
|
||||
支持中文识别、自动标点
|
||||
"""
|
||||
import os
|
||||
import subprocess
|
||||
import warnings
|
||||
|
||||
warnings.filterwarnings('ignore')
|
||||
|
||||
UPLOAD_FOLDER = '/home/ubuntu/nanocode/audio-recorder/uploads'
|
||||
MODEL_CACHE = os.path.expanduser('~/.cache/modelscope')
|
||||
|
||||
class FunASRRecognizer:
|
||||
def __init__(self):
|
||||
self.model = None
|
||||
self.model_type = 'funasr' if self._check_model_cached() else 'vosk'
|
||||
|
||||
def _check_model_cached(self):
|
||||
"""检查 FunASR 模型是否已下载"""
|
||||
# 检查常用的模型路径
|
||||
possible_paths = [
|
||||
os.path.join(MODEL_CACHE, 'hub', 'damo', 'speech_paraformer-large_asr_nat-zh-cn-16k-common-vocab8404-pytorch'),
|
||||
os.path.join(MODEL_CACHE, 'hub', 'iic', 'speech_paraformer-large_asr_nat-zh-cn-16k-common-vocab8404-pytorch'),
|
||||
]
|
||||
return any(os.path.exists(p) for p in possible_paths)
|
||||
|
||||
def _load_model(self):
|
||||
"""加载 FunASR 模型"""
|
||||
if self.model is None:
|
||||
try:
|
||||
from funasr import AutoModel
|
||||
print("🔄 加载 FunASR 模型...")
|
||||
self.model = AutoModel(
|
||||
model="paraformer-zh",
|
||||
vad_model="fsmn-vad",
|
||||
punc_model="ct-punc",
|
||||
)
|
||||
print("✅ FunASR 模型加载完成")
|
||||
self.model_type = 'funasr'
|
||||
except Exception as e:
|
||||
print(f"⚠️ FunASR 加载失败: {e}, 回退到 Vosk")
|
||||
self.model_type = 'vosk'
|
||||
raise
|
||||
return self.model
|
||||
|
||||
def convert_to_wav(self, input_path):
|
||||
"""转换为 WAV 格式"""
|
||||
output_path = input_path + '.wav'
|
||||
try:
|
||||
cmd = [
|
||||
'ffmpeg', '-y', '-i', input_path,
|
||||
'-ar', '16000', '-ac', '1', '-f', 'wav',
|
||||
output_path
|
||||
]
|
||||
result = subprocess.run(cmd, capture_output=True, timeout=30)
|
||||
if result.returncode == 0 and os.path.exists(output_path):
|
||||
return output_path
|
||||
except Exception as e:
|
||||
print(f"转换错误: {e}")
|
||||
return None
|
||||
|
||||
def transcribe(self, audio_path):
|
||||
"""转写音频"""
|
||||
# 先转换为 wav
|
||||
wav_path = self.convert_to_wav(audio_path)
|
||||
if not wav_path:
|
||||
return None, "音频转换失败"
|
||||
|
||||
try:
|
||||
# 尝试使用 FunASR
|
||||
model = self._load_model()
|
||||
|
||||
if self.model_type == 'funasr':
|
||||
# FunASR 识别
|
||||
res = model.generate(input=wav_path, batch_size_s=300)
|
||||
text = res[0]["text"]
|
||||
else:
|
||||
# 回退到 Vosk
|
||||
text, error = self._transcribe_vosk(wav_path)
|
||||
if error:
|
||||
raise Exception(error)
|
||||
|
||||
# 清理临时文件
|
||||
if os.path.exists(wav_path):
|
||||
os.remove(wav_path)
|
||||
|
||||
return text, None
|
||||
|
||||
except Exception as e:
|
||||
# 清理临时文件
|
||||
if os.path.exists(wav_path):
|
||||
os.remove(wav_path)
|
||||
return None, str(e)
|
||||
|
||||
def _transcribe_vosk(self, wav_path):
|
||||
"""备用:Vosk 识别"""
|
||||
try:
|
||||
from vosk import Model, KaldiRecognizer
|
||||
import json
|
||||
import wave
|
||||
|
||||
model_path = '/home/ubuntu/nanocode/audio-recorder/models/cn-model'
|
||||
model = Model(model_path)
|
||||
|
||||
wf = wave.open(wav_path, 'rb')
|
||||
rec = KaldiRecognizer(model, wf.getframerate())
|
||||
|
||||
results = []
|
||||
while True:
|
||||
data = wf.readframes(4000)
|
||||
if len(data) == 0:
|
||||
break
|
||||
if rec.AcceptWaveform(data):
|
||||
result = json.loads(rec.Result())
|
||||
if result.get('text'):
|
||||
results.append(result['text'])
|
||||
|
||||
final = json.loads(rec.FinalResult())
|
||||
if final.get('text'):
|
||||
results.append(final['text'])
|
||||
|
||||
wf.close()
|
||||
return ' '.join(results), None
|
||||
|
||||
except Exception as e:
|
||||
return None, f"Vosk 错误: {str(e)}"
|
||||
|
||||
# 全局识别器
|
||||
_recognizer = None
|
||||
|
||||
def get_recognizer():
|
||||
global _recognizer
|
||||
if _recognizer is None:
|
||||
_recognizer = FunASRRecognizer()
|
||||
return _recognizer
|
||||
|
||||
def transcribe_audio(audio_path):
|
||||
"""转写音频入口"""
|
||||
recognizer = get_recognizer()
|
||||
return recognizer.transcribe(audio_path)
|
||||
Executable
+19
@@ -0,0 +1,19 @@
|
||||
#!/bin/bash
|
||||
# 下载 Vosk 中文模型
|
||||
|
||||
cd /home/ubuntu/nanocode/audio-recorder/models
|
||||
|
||||
echo "📥 下载 Vosk 中文模型..."
|
||||
wget -O vosk-model-small-cn-0.22.zip \
|
||||
https://alphacephei.com/vosk/models/vosk-model-small-cn-0.22.zip
|
||||
|
||||
echo "📦 解压模型..."
|
||||
unzip vosk-model-small-cn-0.22.zip
|
||||
mv vosk-model-small-cn-0.22 cn-model
|
||||
|
||||
echo "🗑️ 清理..."
|
||||
rm vosk-model-small-cn-0.22.zip
|
||||
|
||||
echo "✅ 模型安装完成!"
|
||||
echo "📍 路径: /home/ubuntu/nanocode/audio-recorder/models/cn-model"
|
||||
echo "🔄 请重启服务: pkill -f app_with_asr.py && python3 backend/app_with_asr.py"
|
||||
@@ -0,0 +1,730 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="zh-CN">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no">
|
||||
<title>录音工具</title>
|
||||
<style>
|
||||
* {
|
||||
margin: 0;
|
||||
padding: 0;
|
||||
box-sizing: border-box;
|
||||
}
|
||||
|
||||
body {
|
||||
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
|
||||
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
|
||||
min-height: 100vh;
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
align-items: center;
|
||||
padding: 15px;
|
||||
}
|
||||
|
||||
.container {
|
||||
width: 100%;
|
||||
max-width: 400px;
|
||||
background: white;
|
||||
border-radius: 16px;
|
||||
padding: 20px;
|
||||
box-shadow: 0 15px 50px rgba(0,0,0,0.3);
|
||||
}
|
||||
|
||||
h1 {
|
||||
text-align: center;
|
||||
color: #333;
|
||||
margin-bottom: 15px;
|
||||
font-size: 22px;
|
||||
}
|
||||
|
||||
.status {
|
||||
text-align: center;
|
||||
padding: 8px 12px;
|
||||
margin-bottom: 10px;
|
||||
border-radius: 8px;
|
||||
font-size: 14px;
|
||||
font-weight: 500;
|
||||
transition: all 0.3s;
|
||||
min-height: 32px;
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
}
|
||||
|
||||
.status.idle {
|
||||
background: #f0f0f0;
|
||||
color: #666;
|
||||
}
|
||||
|
||||
.status.recording {
|
||||
background: #ffebee;
|
||||
color: #c62828;
|
||||
animation: pulse 1.5s infinite;
|
||||
}
|
||||
|
||||
.status.playing {
|
||||
background: #e3f2fd;
|
||||
color: #1565c0;
|
||||
}
|
||||
|
||||
.status.transcribing {
|
||||
background: #fff3e0;
|
||||
color: #e65100;
|
||||
}
|
||||
|
||||
@keyframes pulse {
|
||||
0%, 100% { opacity: 1; }
|
||||
50% { opacity: 0.7; }
|
||||
}
|
||||
|
||||
.timer {
|
||||
text-align: center;
|
||||
font-size: 36px;
|
||||
font-weight: 300;
|
||||
color: #333;
|
||||
margin: 8px 0;
|
||||
font-variant-numeric: tabular-nums;
|
||||
line-height: 1;
|
||||
}
|
||||
|
||||
.button-row {
|
||||
display: flex;
|
||||
gap: 10px;
|
||||
margin-bottom: 12px;
|
||||
}
|
||||
|
||||
button {
|
||||
flex: 1;
|
||||
padding: 14px 16px;
|
||||
border: none;
|
||||
border-radius: 10px;
|
||||
font-size: 16px;
|
||||
font-weight: 600;
|
||||
cursor: pointer;
|
||||
transition: all 0.3s;
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
gap: 6px;
|
||||
}
|
||||
|
||||
button:disabled {
|
||||
opacity: 0.5;
|
||||
cursor: not-allowed;
|
||||
}
|
||||
|
||||
button:active:not(:disabled) {
|
||||
transform: scale(0.98);
|
||||
}
|
||||
|
||||
.btn-start {
|
||||
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
|
||||
color: white;
|
||||
}
|
||||
|
||||
.btn-stop {
|
||||
background: #ef5350;
|
||||
color: white;
|
||||
}
|
||||
|
||||
.btn-play {
|
||||
background: #42a5f5;
|
||||
color: white;
|
||||
flex: 0.6;
|
||||
}
|
||||
|
||||
.progress-info {
|
||||
flex: 0.4;
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
background: #f5f5f5;
|
||||
border-radius: 10px;
|
||||
font-size: 14px;
|
||||
color: #666;
|
||||
}
|
||||
|
||||
.audio-player {
|
||||
margin-top: 12px;
|
||||
padding: 10px;
|
||||
background: #f5f5f5;
|
||||
border-radius: 10px;
|
||||
display: none;
|
||||
}
|
||||
|
||||
.audio-player.show {
|
||||
display: block;
|
||||
}
|
||||
|
||||
audio {
|
||||
width: 100%;
|
||||
height: 36px;
|
||||
}
|
||||
|
||||
.icon {
|
||||
font-size: 18px;
|
||||
}
|
||||
|
||||
.error {
|
||||
background: #ffebee;
|
||||
color: #c62828;
|
||||
padding: 10px 12px;
|
||||
border-radius: 8px;
|
||||
margin-bottom: 12px;
|
||||
font-size: 13px;
|
||||
display: none;
|
||||
line-height: 1.5;
|
||||
}
|
||||
|
||||
.error.show {
|
||||
display: block;
|
||||
}
|
||||
|
||||
/* 转写结果区域 */
|
||||
.transcript-box {
|
||||
margin-top: 15px;
|
||||
padding: 15px;
|
||||
background: #f8f9fa;
|
||||
border-radius: 12px;
|
||||
border-left: 4px solid #667eea;
|
||||
display: none;
|
||||
}
|
||||
|
||||
.transcript-box.show {
|
||||
display: block;
|
||||
}
|
||||
|
||||
.transcript-header {
|
||||
display: flex;
|
||||
justify-content: space-between;
|
||||
align-items: center;
|
||||
margin-bottom: 10px;
|
||||
}
|
||||
|
||||
.transcript-title {
|
||||
font-weight: 600;
|
||||
color: #333;
|
||||
font-size: 15px;
|
||||
}
|
||||
|
||||
.transcript-actions {
|
||||
display: flex;
|
||||
gap: 8px;
|
||||
}
|
||||
|
||||
.btn-small {
|
||||
padding: 6px 12px;
|
||||
font-size: 12px;
|
||||
border-radius: 6px;
|
||||
border: none;
|
||||
cursor: pointer;
|
||||
background: #e3f2fd;
|
||||
color: #1565c0;
|
||||
}
|
||||
|
||||
.transcript-content {
|
||||
background: white;
|
||||
padding: 12px;
|
||||
border-radius: 8px;
|
||||
min-height: 80px;
|
||||
font-size: 15px;
|
||||
line-height: 1.6;
|
||||
color: #333;
|
||||
white-space: pre-wrap;
|
||||
word-wrap: break-word;
|
||||
}
|
||||
|
||||
.transcript-content:empty::before {
|
||||
content: '转写中...';
|
||||
color: #999;
|
||||
}
|
||||
|
||||
/* AI 改写按钮区域 */
|
||||
.ai-actions {
|
||||
margin-top: 12px;
|
||||
display: flex;
|
||||
gap: 8px;
|
||||
flex-wrap: wrap;
|
||||
}
|
||||
|
||||
.btn-ai {
|
||||
padding: 8px 14px;
|
||||
font-size: 13px;
|
||||
border-radius: 8px;
|
||||
border: none;
|
||||
cursor: pointer;
|
||||
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
|
||||
color: white;
|
||||
font-weight: 500;
|
||||
transition: all 0.3s;
|
||||
}
|
||||
|
||||
.btn-ai:hover:not(:disabled) {
|
||||
transform: translateY(-1px);
|
||||
box-shadow: 0 4px 12px rgba(102, 126, 234, 0.4);
|
||||
}
|
||||
|
||||
.btn-ai:disabled {
|
||||
opacity: 0.6;
|
||||
cursor: not-allowed;
|
||||
}
|
||||
|
||||
/* AI 改写结果区域 */
|
||||
.analysis-box {
|
||||
margin-top: 15px;
|
||||
padding: 15px;
|
||||
background: #f0f7ff;
|
||||
border-radius: 12px;
|
||||
border-left: 4px solid #42a5f5;
|
||||
display: none;
|
||||
}
|
||||
|
||||
.analysis-box.show {
|
||||
display: block;
|
||||
}
|
||||
|
||||
.analysis-header {
|
||||
display: flex;
|
||||
justify-content: space-between;
|
||||
align-items: center;
|
||||
margin-bottom: 10px;
|
||||
}
|
||||
|
||||
.analysis-title {
|
||||
font-weight: 600;
|
||||
color: #1565c0;
|
||||
font-size: 15px;
|
||||
}
|
||||
|
||||
.analysis-actions {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: 8px;
|
||||
}
|
||||
|
||||
.analysis-type {
|
||||
font-size: 12px;
|
||||
color: #666;
|
||||
background: white;
|
||||
padding: 2px 8px;
|
||||
border-radius: 4px;
|
||||
}
|
||||
|
||||
.analysis-content {
|
||||
background: white;
|
||||
padding: 12px;
|
||||
border-radius: 8px;
|
||||
min-height: 100px;
|
||||
font-size: 14px;
|
||||
line-height: 1.7;
|
||||
color: #333;
|
||||
white-space: pre-wrap;
|
||||
word-wrap: break-word;
|
||||
}
|
||||
|
||||
.analysis-loading {
|
||||
text-align: center;
|
||||
color: #666;
|
||||
padding: 20px;
|
||||
}
|
||||
|
||||
.analysis-loading::after {
|
||||
content: '';
|
||||
display: inline-block;
|
||||
width: 16px;
|
||||
height: 16px;
|
||||
border: 2px solid #667eea;
|
||||
border-top-color: transparent;
|
||||
border-radius: 50%;
|
||||
animation: spin 1s linear infinite;
|
||||
margin-left: 8px;
|
||||
vertical-align: middle;
|
||||
}
|
||||
|
||||
@keyframes spin {
|
||||
to { transform: rotate(360deg); }
|
||||
}
|
||||
|
||||
.progress-bar {
|
||||
width: 100%;
|
||||
height: 4px;
|
||||
background: #e0e0e0;
|
||||
border-radius: 2px;
|
||||
margin-top: 8px;
|
||||
overflow: hidden;
|
||||
display: none;
|
||||
}
|
||||
|
||||
.progress-bar.show {
|
||||
display: block;
|
||||
}
|
||||
|
||||
.progress-fill {
|
||||
height: 100%;
|
||||
background: linear-gradient(90deg, #667eea, #764ba2);
|
||||
width: 0%;
|
||||
transition: width 0.3s;
|
||||
border-radius: 2px;
|
||||
}
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
<div class="container">
|
||||
<h1>🎙️ 录音工具</h1>
|
||||
|
||||
<div class="error" id="errorBox"></div>
|
||||
|
||||
<div class="status idle" id="statusBox">准备录音</div>
|
||||
|
||||
<div class="timer" id="timer">00:00</div>
|
||||
|
||||
<!-- 第一行:开始 + 结束 -->
|
||||
<div class="button-row">
|
||||
<button class="btn-start" id="btnStart" onclick="startRecording()">
|
||||
<span class="icon">🔴</span>开始
|
||||
</button>
|
||||
<button class="btn-stop" id="btnStop" onclick="stopRecording()" disabled>
|
||||
<span class="icon">⏹️</span>结束
|
||||
</button>
|
||||
</div>
|
||||
|
||||
<!-- 第二行:回放 + 进度 -->
|
||||
<div class="button-row">
|
||||
<button class="btn-play" id="btnPlay" onclick="playRecording()" disabled>
|
||||
<span class="icon">▶️</span>回放
|
||||
</button>
|
||||
<div class="progress-info" id="playProgress">00:00</div>
|
||||
</div>
|
||||
|
||||
<!-- 转写进度条 -->
|
||||
<div class="progress-bar" id="transcribeProgress">
|
||||
<div class="progress-fill" id="progressFill"></div>
|
||||
</div>
|
||||
|
||||
<div class="audio-player" id="audioPlayer">
|
||||
<audio id="audioElement" controls></audio>
|
||||
</div>
|
||||
|
||||
<!-- 转写结果 -->
|
||||
<div class="transcript-box" id="transcriptBox">
|
||||
<div class="transcript-header">
|
||||
<span class="transcript-title">📝 转写结果</span>
|
||||
<div class="transcript-actions">
|
||||
<button class="btn-small" onclick="copyTranscript()">复制</button>
|
||||
</div>
|
||||
</div>
|
||||
<div class="transcript-content" id="transcriptContent"></div>
|
||||
|
||||
<!-- AI 改写按钮 -->
|
||||
<div class="ai-actions" id="aiActions" style="display: none;">
|
||||
<button class="btn-ai" onclick="analyzeText('daily')">📋 日报总结</button>
|
||||
<button class="btn-ai" onclick="analyzeText('weekly')">📊 周报总结</button>
|
||||
<button class="btn-ai" onclick="analyzeText('general')">📝 日常总结</button>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<!-- AI 改写结果 -->
|
||||
<div class="analysis-box" id="analysisBox">
|
||||
<div class="analysis-header">
|
||||
<span class="analysis-title">🤖 AI 改写结果</span>
|
||||
<div class="analysis-actions">
|
||||
<span class="analysis-type" id="analysisType"></span>
|
||||
<button class="btn-small" onclick="copyAnalysis()">复制</button>
|
||||
</div>
|
||||
</div>
|
||||
<div class="analysis-content" id="analysisContent"></div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<script>
|
||||
let mediaRecorder = null;
|
||||
let audioChunks = [];
|
||||
let audioBlob = null;
|
||||
let audioUrl = null;
|
||||
let startTime = null;
|
||||
let timerInterval = null;
|
||||
let currentRecordingFilename = null;
|
||||
|
||||
const btnStart = document.getElementById('btnStart');
|
||||
const btnStop = document.getElementById('btnStop');
|
||||
const btnPlay = document.getElementById('btnPlay');
|
||||
const statusBox = document.getElementById('statusBox');
|
||||
const timer = document.getElementById('timer');
|
||||
const audioPlayer = document.getElementById('audioPlayer');
|
||||
const audioElement = document.getElementById('audioElement');
|
||||
const playProgress = document.getElementById('playProgress');
|
||||
const errorBox = document.getElementById('errorBox');
|
||||
const transcriptBox = document.getElementById('transcriptBox');
|
||||
const transcriptContent = document.getElementById('transcriptContent');
|
||||
const transcribeProgress = document.getElementById('transcribeProgress');
|
||||
const progressFill = document.getElementById('progressFill');
|
||||
const aiActions = document.getElementById('aiActions');
|
||||
const analysisBox = document.getElementById('analysisBox');
|
||||
const analysisContent = document.getElementById('analysisContent');
|
||||
const analysisType = document.getElementById('analysisType');
|
||||
|
||||
function showError(msg) {
|
||||
errorBox.textContent = msg;
|
||||
errorBox.classList.add('show');
|
||||
setTimeout(() => errorBox.classList.remove('show'), 5000);
|
||||
}
|
||||
|
||||
function updateStatus(status, text) {
|
||||
statusBox.className = 'status ' + status;
|
||||
statusBox.textContent = text;
|
||||
}
|
||||
|
||||
function formatTime(seconds) {
|
||||
const mins = Math.floor(seconds / 60).toString().padStart(2, '0');
|
||||
const secs = (seconds % 60).toString().padStart(2, '0');
|
||||
return `${mins}:${secs}`;
|
||||
}
|
||||
|
||||
function updateTimer() {
|
||||
const elapsed = Math.floor((Date.now() - startTime) / 1000);
|
||||
timer.textContent = formatTime(elapsed);
|
||||
}
|
||||
|
||||
function updatePlayProgress() {
|
||||
if (audioElement.duration) {
|
||||
const current = Math.floor(audioElement.currentTime);
|
||||
const total = Math.floor(audioElement.duration);
|
||||
playProgress.textContent = `${formatTime(current)}/${formatTime(total)}`;
|
||||
}
|
||||
}
|
||||
|
||||
audioElement.ontimeupdate = updatePlayProgress;
|
||||
audioElement.onloadedmetadata = updatePlayProgress;
|
||||
|
||||
async function startRecording() {
|
||||
try {
|
||||
const stream = await navigator.mediaDevices.getUserMedia({
|
||||
audio: {
|
||||
echoCancellation: true,
|
||||
noiseSuppression: true,
|
||||
sampleRate: 16000
|
||||
}
|
||||
});
|
||||
|
||||
const mimeType = MediaRecorder.isTypeSupported('audio/webm')
|
||||
? 'audio/webm;codecs=opus'
|
||||
: 'audio/mp4';
|
||||
|
||||
mediaRecorder = new MediaRecorder(stream, { mimeType });
|
||||
audioChunks = [];
|
||||
|
||||
mediaRecorder.ondataavailable = (event) => {
|
||||
if (event.data.size > 0) {
|
||||
audioChunks.push(event.data);
|
||||
}
|
||||
};
|
||||
|
||||
mediaRecorder.onstop = () => {
|
||||
audioBlob = new Blob(audioChunks, { type: mimeType });
|
||||
audioUrl = URL.createObjectURL(audioBlob);
|
||||
|
||||
audioElement.src = audioUrl;
|
||||
audioPlayer.classList.add('show');
|
||||
btnPlay.disabled = false;
|
||||
|
||||
// 自动开始转写
|
||||
transcribeAudio(audioBlob);
|
||||
|
||||
stream.getTracks().forEach(track => track.stop());
|
||||
};
|
||||
|
||||
mediaRecorder.start(100);
|
||||
startTime = Date.now();
|
||||
timerInterval = setInterval(updateTimer, 1000);
|
||||
|
||||
btnStart.disabled = true;
|
||||
btnStop.disabled = false;
|
||||
btnPlay.disabled = true;
|
||||
updateStatus('recording', '🔴 正在录音...');
|
||||
audioPlayer.classList.remove('show');
|
||||
transcriptBox.classList.remove('show');
|
||||
aiActions.style.display = 'none';
|
||||
analysisBox.classList.remove('show');
|
||||
transcriptContent.textContent = '';
|
||||
playProgress.textContent = '00:00';
|
||||
|
||||
} catch (err) {
|
||||
console.error('录音失败:', err);
|
||||
showError('无法访问麦克风,请检查权限设置');
|
||||
}
|
||||
}
|
||||
|
||||
function stopRecording() {
|
||||
if (mediaRecorder && mediaRecorder.state !== 'inactive') {
|
||||
mediaRecorder.stop();
|
||||
clearInterval(timerInterval);
|
||||
btnStart.disabled = false;
|
||||
btnStop.disabled = true;
|
||||
updateStatus('idle', '录音完成,正在转写...');
|
||||
}
|
||||
}
|
||||
|
||||
function playRecording() {
|
||||
if (audioUrl) {
|
||||
audioElement.play();
|
||||
updateStatus('playing', '▶️ 正在播放...');
|
||||
|
||||
audioElement.onended = () => {
|
||||
updateStatus('idle', '播放完成');
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
async function transcribeAudio(blob) {
|
||||
transcribeProgress.classList.add('show');
|
||||
progressFill.style.width = '30%';
|
||||
|
||||
try {
|
||||
const formData = new FormData();
|
||||
formData.append('audio', blob, `recording_${Date.now()}.webm`);
|
||||
|
||||
progressFill.style.width = '60%';
|
||||
|
||||
const response = await fetch('/api/transcribe', {
|
||||
method: 'POST',
|
||||
body: formData
|
||||
});
|
||||
|
||||
progressFill.style.width = '90%';
|
||||
|
||||
if (response.ok) {
|
||||
const result = await response.json();
|
||||
progressFill.style.width = '100%';
|
||||
|
||||
setTimeout(() => {
|
||||
transcribeProgress.classList.remove('show');
|
||||
progressFill.style.width = '0%';
|
||||
}, 500);
|
||||
|
||||
if (result.success) {
|
||||
transcriptContent.textContent = result.text;
|
||||
transcriptBox.classList.add('show');
|
||||
aiActions.style.display = 'flex';
|
||||
currentRecordingFilename = result.filename;
|
||||
updateStatus('idle', '转写完成');
|
||||
} else {
|
||||
showError('转写失败: ' + result.error);
|
||||
updateStatus('idle', '转写失败');
|
||||
}
|
||||
} else {
|
||||
throw new Error('请求失败');
|
||||
}
|
||||
} catch (err) {
|
||||
console.error('转写错误:', err);
|
||||
transcribeProgress.classList.remove('show');
|
||||
showError('转写服务暂时不可用');
|
||||
updateStatus('idle', '转写失败');
|
||||
}
|
||||
}
|
||||
|
||||
function copyTranscript() {
|
||||
const text = transcriptContent.textContent;
|
||||
if (text) {
|
||||
navigator.clipboard.writeText(text).then(() => {
|
||||
const btn = event.target;
|
||||
const original = btn.textContent;
|
||||
btn.textContent = '已复制!';
|
||||
setTimeout(() => btn.textContent = original, 1500);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
// 复制AI分析结果
|
||||
function copyAnalysis() {
|
||||
const text = analysisContent.textContent;
|
||||
if (text && !text.includes('正在改写中')) {
|
||||
navigator.clipboard.writeText(text).then(() => {
|
||||
const btn = event.target;
|
||||
const original = btn.textContent;
|
||||
btn.textContent = '已复制!';
|
||||
setTimeout(() => btn.textContent = original, 1500);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
// AI 分析文本
|
||||
async function analyzeText(type) {
|
||||
const text = transcriptContent.textContent;
|
||||
if (!text || text.trim().length < 10) {
|
||||
showError('转写内容太短,无法分析(至少10个字符)');
|
||||
return;
|
||||
}
|
||||
|
||||
const typeNames = {
|
||||
'daily': '日报',
|
||||
'weekly': '周报',
|
||||
'general': '日常总结'
|
||||
};
|
||||
|
||||
// 显示加载状态
|
||||
analysisBox.classList.add('show');
|
||||
analysisContent.innerHTML = '<div class="analysis-loading">AI 正在改写中</div>';
|
||||
analysisType.textContent = typeNames[type];
|
||||
|
||||
// 禁用按钮
|
||||
document.querySelectorAll('.btn-ai').forEach(btn => btn.disabled = true);
|
||||
|
||||
try {
|
||||
const response = await fetch('/api/analyze', {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/json'
|
||||
},
|
||||
body: JSON.stringify({
|
||||
text: text,
|
||||
type: type
|
||||
})
|
||||
});
|
||||
|
||||
if (response.ok) {
|
||||
const result = await response.json();
|
||||
if (result.success) {
|
||||
analysisContent.textContent = result.analysis;
|
||||
} else {
|
||||
analysisContent.textContent = '改写失败: ' + result.error;
|
||||
}
|
||||
} else {
|
||||
throw new Error('请求失败');
|
||||
}
|
||||
} catch (err) {
|
||||
console.error('AI 分析错误:', err);
|
||||
analysisContent.textContent = 'AI 服务暂时不可用,请稍后重试';
|
||||
} finally {
|
||||
// 启用按钮
|
||||
document.querySelectorAll('.btn-ai').forEach(btn => btn.disabled = false);
|
||||
}
|
||||
}
|
||||
|
||||
// 页面加载检查
|
||||
window.onload = () => {
|
||||
const checks = [];
|
||||
|
||||
if (window.location.protocol !== 'https:' && window.location.hostname !== 'localhost') {
|
||||
checks.push('❌ 需要 HTTPS 环境');
|
||||
}
|
||||
|
||||
if (!navigator.mediaDevices || !navigator.mediaDevices.getUserMedia) {
|
||||
checks.push('❌ 不支持 getUserMedia');
|
||||
}
|
||||
|
||||
if (!window.MediaRecorder) {
|
||||
checks.push('❌ 不支持 MediaRecorder');
|
||||
}
|
||||
|
||||
if (checks.length > 0) {
|
||||
let errorMsg = '环境不满足:\n' + checks.join('\n');
|
||||
if (/MicroMessenger|QQ/i.test(navigator.userAgent)) {
|
||||
errorMsg += '\n\n请在 Safari/Chrome 中打开';
|
||||
}
|
||||
showError(errorMsg);
|
||||
btnStart.disabled = true;
|
||||
}
|
||||
};
|
||||
</script>
|
||||
</body>
|
||||
</html>
|
||||
Binary file not shown.
Executable
+34
@@ -0,0 +1,34 @@
|
||||
#!/bin/bash
|
||||
# 启动录音服务 + HTTPS 隧道
|
||||
|
||||
echo "🎙️ 启动录音服务..."
|
||||
|
||||
# 创建上传目录
|
||||
mkdir -p /home/ubuntu/nanocode/audio-recorder/uploads
|
||||
|
||||
cd /home/ubuntu/nanocode/audio-recorder/backend
|
||||
|
||||
# 检查并启动 Flask 服务
|
||||
if ! pgrep -f "python3 app.py" > /dev/null; then
|
||||
echo "📡 启动 Flask 服务..."
|
||||
nohup python3 app.py > /tmp/audio-recorder.log 2>&1 &
|
||||
sleep 2
|
||||
fi
|
||||
|
||||
echo "✅ Flask 服务已启动: http://localhost:8890"
|
||||
echo ""
|
||||
|
||||
# 启动 HTTPS 隧道
|
||||
echo "🔒 启动 HTTPS 隧道..."
|
||||
echo " 请等待 5-10 秒生成安全链接..."
|
||||
echo ""
|
||||
|
||||
npx -y localtunnel --port 8890 &
|
||||
|
||||
sleep 5
|
||||
|
||||
echo ""
|
||||
echo "📱 手机浏览器访问上面的 HTTPS 链接即可录音"
|
||||
echo "⚠️ 注意:首次访问 localtunnel 需要点击 'Click to Continue'"
|
||||
echo ""
|
||||
echo "🛑 停止服务: pkill -f 'python3 app.py' && pkill -f 'localtunnel'"
|
||||
Executable
+26
@@ -0,0 +1,26 @@
|
||||
#!/bin/bash
|
||||
# 录音服务启动脚本
|
||||
|
||||
echo "🎙️ 启动录音服务..."
|
||||
|
||||
# 创建上传目录
|
||||
mkdir -p /home/ubuntu/nanocode/audio-recorder/uploads
|
||||
|
||||
# 进入后端目录
|
||||
cd /home/ubuntu/nanocode/audio-recorder/backend
|
||||
|
||||
# 检查依赖
|
||||
if ! python3 -c "import flask" 2>/dev/null; then
|
||||
echo "📦 安装依赖..."
|
||||
pip3 install -r requirements.txt -q
|
||||
fi
|
||||
|
||||
# 启动服务
|
||||
echo "🌐 服务启动在 http://0.0.0.0:8890"
|
||||
echo "📱 手机浏览器访问: http://$(hostname -I | awk '{print $1}'):8890"
|
||||
echo ""
|
||||
echo "⚠️ 注意:手机浏览器需要 HTTPS 才能录音"
|
||||
echo " 建议用 nginx 反向代理 + SSL 证书"
|
||||
echo ""
|
||||
|
||||
python3 app.py
|
||||
@@ -0,0 +1,46 @@
|
||||
#!/usr/bin/env python3
|
||||
import warnings
|
||||
warnings.filterwarnings('ignore')
|
||||
|
||||
from funasr import AutoModel
|
||||
import os
|
||||
import subprocess
|
||||
|
||||
print("🔄 加载 FunASR 模型...")
|
||||
print(" 首次会自动下载模型(约100MB)到 ~/.cache/modelscope")
|
||||
print(" 请等待...\n")
|
||||
|
||||
model = AutoModel(
|
||||
model="paraformer-zh",
|
||||
vad_model="fsmn-vad",
|
||||
punc_model="ct-punc",
|
||||
)
|
||||
|
||||
print("\n✅ FunASR 模型加载完成!")
|
||||
|
||||
# 测试文件
|
||||
uploads_dir = 'uploads'
|
||||
if os.path.exists(uploads_dir):
|
||||
files = [f for f in os.listdir(uploads_dir) if f.endswith(('.webm', '.wav', '.mp3'))]
|
||||
if files:
|
||||
test_file = os.path.join(uploads_dir, files[0])
|
||||
print(f"\n🎵 测试文件: {test_file}")
|
||||
|
||||
# 转换为 wav
|
||||
wav_file = test_file + '.wav'
|
||||
subprocess.run(['ffmpeg', '-y', '-i', test_file, '-ar', '16000', '-ac', '1', wav_file],
|
||||
capture_output=True)
|
||||
|
||||
print("📝 开始识别...")
|
||||
res = model.generate(input=wav_file, batch_size_s=300)
|
||||
|
||||
text = res[0]["text"]
|
||||
print(f"\n✅ 识别结果:")
|
||||
print(f" {text}")
|
||||
|
||||
if os.path.exists(wav_file):
|
||||
os.remove(wav_file)
|
||||
else:
|
||||
print("\n⚠️ 没有测试音频")
|
||||
else:
|
||||
print("\n⚠️ uploads 目录不存在")
|
||||
Reference in New Issue
Block a user