File size: 1,327 Bytes
14c64e4
aa4e6cf
14c64e4
 
 
 
58358ed
14c64e4
 
 
 
 
 
 
 
 
aa4e6cf
 
 
 
 
 
 
 
14c64e4
 
 
 
 
095cdd1
14c64e4
 
 
 
 
 
 
 
 
 
 
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
# audio.py
from faster_whisper import WhisperModel
import os
import tempfile
from text import analyze_text  # ✅ 导入文字分析模块

def analyze_audio(file_path, lang=None):
    output = ""

    # 创建临时文件副本
    with tempfile.NamedTemporaryFile(suffix=".mp3", delete=False) as tmp:
        tmp_path = tmp.name
        with open(file_path, "rb") as f:
            tmp.write(f.read())

    try:
        # ✅ 使用 faster-whisper 的 tiny 模型,适合 CPU 环境
        model = WhisperModel("tiny", device="cpu", compute_type="int8")

        # ✅ 自动语言识别 & 转录
        segments, info = model.transcribe(tmp_path, beam_size=5)

        # 拼接所有转录文字
        transcribed_text = "".join([seg.text for seg in segments]).strip()

        if transcribed_text:
            output += f"🔊 Whisper 语音识别完成:\n{transcribed_text}\n\n"
            
            # 2️⃣ 调用文字分析
            analysis = analyze_text(transcribed_text,lang)
            output += "📊 文本分析结果:\n" + analysis
        else:
            output += "⚠️ 没有识别到有效语音内容。"
            
    except Exception as e:
        output += f"❌ 音频处理失败: {str(e)}"

    finally:
        os.remove(tmp_path)  # 清理临时文件

    return output