Browse Source

上传音频接口拆分

root 6 months ago
parent
commit
aa015bed5f
4 changed files with 112 additions and 78 deletions
  1. BIN
      app/__pycache__/__init__.cpython-310.pyc
  2. 23 3
      app/routes.py
  3. 71 0
      app/services/file_service.py
  4. 18 75
      app/templates/index.html

BIN
app/__pycache__/__init__.cpython-310.pyc


+ 23 - 3
app/routes.py

@@ -1,13 +1,33 @@
 # routes.py
-from flask import Blueprint, render_template
-# from app.services.example_service import get_user_data  # 导入 service 中的函数
+from flask import Blueprint, render_template, request, jsonify
+
+
+from app.services.file_service import parse_file  # 导入 service 中的函数
 
 main_bp = Blueprint('main', __name__)
 
+
 @main_bp.route('/')
 def index():
     return render_template('index.html')
 
+
 @main_bp.route("/hello")
 def hello():
-    return "Hello, World!"
+    return "Hello, World!"
+
+# 文件上传
+
+
+@main_bp.route('/upload', methods=['POST'])
+def upload_file():
+    if 'file' not in request.files:
+        return jsonify({"error": "No file part in the request"}), 400
+
+    file = request.files['file']
+    if file.filename == '':
+        return jsonify({"error": "No file selected for uploading"}), 400
+
+    # 保存文件并解析
+    res_obj = parse_file(file)
+    return jsonify(res_obj), 200

+ 71 - 0
app/services/file_service.py

@@ -0,0 +1,71 @@
+
+import os
+import uuid
+import re
+
+from pypinyin import lazy_pinyin
+from funasr import AutoModel
+
+target_word = "抱坡"
+# 模型1
+model = AutoModel(model="E:\\yuyin_model\\Voice_translation", model_revision="v2.0.4",
+                  vad_model="E:\\yuyin_model\\Endpoint_detection", vad_model_revision="v2.0.4",
+                  punc_model="E:\\yuyin_model\\Ct_punc", punc_model_revision="v2.0.4",
+                  use_cuda=True, use_fast=True,
+                  )
+
+# 替换同音字
+
+
+def replace_word(text, target_word):
+    words = re.findall(r'\b\w+\b', text)
+    for word in words:
+        if is_same_pinyin(word, target_word):
+            text = text.replace(word, target_word)
+    return text
+
+# 判断拼音是否相同
+
+
+def is_same_pinyin(word1, word2):
+    return lazy_pinyin(word1) == lazy_pinyin(word2)
+
+
+def parse_file(file):
+    # 文件保存路径
+    UPLOAD_FOLDER = 'data/audio'
+    os.makedirs(UPLOAD_FOLDER, exist_ok=True)
+
+    # 生成UUID文件名
+    file_ext = os.path.splitext(file.filename)[1]
+    filename = f"{uuid.uuid4()}{file_ext}"
+
+    # 保存文件
+    file_path = os.path.join(UPLOAD_FOLDER, filename)
+    file.save(file_path)
+
+    # 语音转文字模型1
+    res = model.generate(file_path,
+                         batch_size_s=30, hotword='test')
+    texts = [item['text'] for item in res]
+    msg = ' '.join(texts)
+    # print(msg)
+    # 语音转文字模型2
+    # res = inference_pipeline(file_path)
+    # # print(res)
+    # texts = [item['text'] for item in res]
+    # # print(texts)
+    # msg = ' '.join(texts)
+
+    # msg = vocal_text(file_path)
+    os.remove(file_path)
+    msg = replace_word(msg, target_word)
+    words_to_replace = ["爆破", "爆坡", "高坡"]
+    for word in words_to_replace:
+        msg = msg.replace(word, "抱坡")
+    print(msg)
+    return {"msg": "上传成功",
+            "code": 200,
+            "filename": filename,
+            "voiceMsg": msg
+            }

+ 18 - 75
app/templates/index.html

@@ -1,83 +1,26 @@
 <!DOCTYPE html>
-<html lang="zh-CN">
+<html lang="zh">
 <head>
     <meta charset="UTF-8">
     <meta name="viewport" content="width=device-width, initial-scale=1.0">
-    <title>语音识别示例</title>
+    <title>四维AI</title>
+    <style>
+        body {
+            display: flex;
+            justify-content: center;
+            align-items: center;
+            height: 100vh;
+            margin: 0;
+            background-color: #f0f0f0;
+            font-family: Arial, sans-serif;
+        }
+        h1 {
+            font-size: 100px; /* 字体大小可以根据需要调整 */
+            color: #333;
+        }
+    </style>
 </head>
 <body>
-
-<h2>语音识别示例</h2>
-<p>点击下方按钮开始语音输入:</p>
-
-<!-- 语音输入按钮 -->
-<button id="start-btn">开始语音输入</button>
-
-<!-- 显示识别结果 -->
-<p>识别结果: <span id="result"></span></p>
-
-<script>
-    // 检查浏览器是否支持SpeechRecognition
-    const SpeechRecognition = window.SpeechRecognition || window.webkitSpeechRecognition;
-    if (!SpeechRecognition) {
-        alert('你的浏览器不支持语音识别功能,请使用支持的浏览器,例如Google Chrome。');
-    } else {
-        const recognition = new SpeechRecognition();
-        recognition.lang = 'zh-CN';  // 设置识别语言为中文
-        recognition.interimResults = false;  // 不返回临时结果
-        recognition.maxAlternatives = 1;  // 返回的识别结果数量
-
-        const startBtn = document.getElementById('start-btn');
-        const resultSpan = document.getElementById('result');
-
-        startBtn.onclick = () => {
-            recognition.start();  // 开始语音识别
-        };
-
-        recognition.onstart = () => {
-            console.log('开始语音识别...');
-            startBtn.disabled = true;
-            startBtn.textContent = '正在识别...';
-        };
-
-        recognition.onresult = (event) => {
-            const transcript = event.results[0][0].transcript;
-            console.log('识别结果:', transcript);
-            resultSpan.textContent = transcript;
-            sendToBackend(transcript);  // 将识别结果发送到后台
-        };
-
-        recognition.onerror = (event) => {
-            console.error('识别错误:', event.error);
-            alert('语音识别出错: ' + event.error);
-        };
-
-        recognition.onend = () => {
-            console.log('语音识别结束');
-            startBtn.disabled = false;
-            startBtn.textContent = '开始语音输入';
-        };
-
-        function sendToBackend(transcript) {
-            fetch('http://127.0.0.1:5000/msg', {
-                method: 'POST',
-                headers: {
-                    'Content-Type': 'application/json',
-                    'Access-Control-Allow-Origin':'*'
-                },
-                body: JSON.stringify({ msg: transcript }),
-                // mode: 'no-cors'
-            })
-            .then(response => response.json())
-            .then(data => {
-                console.log('服务器响应:', data);
-            })
-            .catch((error) => {
-                console.error('发送到后台时发生错误:', error);
-            });
-        }
-    }
-</script>
-
+    <h1>四维AI</h1>
 </body>
 </html>