# 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