Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,11 +1,21 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
|
| 3 |
import numpy as np
|
| 4 |
from transformers import pipeline
|
| 5 |
|
| 6 |
-
#
|
| 7 |
-
asr = pipeline("automatic-speech-recognition", model="openai/whisper-
|
| 8 |
-
summarizer = pipeline("summarization", model="
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
|
| 10 |
def split_audio(audio_array, sr, chunk_length_s=30):
|
| 11 |
"""将音频按 chunk_length_s 秒切片"""
|
|
@@ -14,38 +24,58 @@ def split_audio(audio_array, sr, chunk_length_s=30):
|
|
| 14 |
end = min(start + chunk_size, len(audio_array))
|
| 15 |
yield audio_array[start:end]
|
| 16 |
|
| 17 |
-
|
|
|
|
|
|
|
| 18 |
# 1️⃣ 读取音频
|
| 19 |
-
audio_array, samplerate =
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
|
| 21 |
-
# 2️⃣ 分段识别
|
| 22 |
all_texts = []
|
| 23 |
-
for i, chunk in enumerate(
|
| 24 |
res = asr({"array": chunk, "sampling_rate": samplerate})
|
| 25 |
text = res.get("text", "").strip()
|
| 26 |
all_texts.append(text)
|
| 27 |
-
print(f"Segment {i+1} done")
|
| 28 |
|
| 29 |
full_text = " ".join(all_texts)
|
| 30 |
|
| 31 |
-
# 3️⃣
|
| 32 |
-
|
| 33 |
if len(full_text) < 1000:
|
| 34 |
summary = summarizer(full_text, max_length=200, min_length=50, do_sample=False)[0]["summary_text"]
|
| 35 |
else:
|
| 36 |
-
# 分
|
| 37 |
-
chunks = [full_text[i:i+1000] for i in range(0, len(full_text), 1000)]
|
| 38 |
summaries = [summarizer(c, max_length=200, min_length=50, do_sample=False)[0]["summary_text"] for c in chunks]
|
| 39 |
summary = " ".join(summaries)
|
| 40 |
|
|
|
|
|
|
|
| 41 |
return full_text, summary
|
| 42 |
|
|
|
|
|
|
|
| 43 |
demo = gr.Interface(
|
| 44 |
fn=process_long_audio,
|
| 45 |
-
inputs=gr.Audio(type="filepath"),
|
| 46 |
-
outputs=[
|
| 47 |
-
|
| 48 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 49 |
)
|
| 50 |
|
| 51 |
-
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
from pydub import AudioSegment
|
| 3 |
import numpy as np
|
| 4 |
from transformers import pipeline
|
| 5 |
|
| 6 |
+
# 初始化模型
|
| 7 |
+
asr = pipeline("automatic-speech-recognition", model="openai/whisper-small")
|
| 8 |
+
summarizer = pipeline("summarization", model="facebook/bart-large-cnn")
|
| 9 |
+
|
| 10 |
+
|
| 11 |
+
# === 工具函数 ===
|
| 12 |
+
def load_audio_any_format(file_path, target_sr=16000):
|
| 13 |
+
"""使用 pydub 加载任意格式音频并转成 numpy"""
|
| 14 |
+
audio = AudioSegment.from_file(file_path)
|
| 15 |
+
audio = audio.set_frame_rate(target_sr).set_channels(1)
|
| 16 |
+
samples = np.array(audio.get_array_of_samples()).astype(np.float32) / (2 ** 15)
|
| 17 |
+
return samples, target_sr
|
| 18 |
+
|
| 19 |
|
| 20 |
def split_audio(audio_array, sr, chunk_length_s=30):
|
| 21 |
"""将音频按 chunk_length_s 秒切片"""
|
|
|
|
| 24 |
end = min(start + chunk_size, len(audio_array))
|
| 25 |
yield audio_array[start:end]
|
| 26 |
|
| 27 |
+
|
| 28 |
+
# === 主流程 ===
|
| 29 |
+
def process_long_audio(audio_path, progress=gr.Progress(track_tqdm=False)):
|
| 30 |
# 1️⃣ 读取音频
|
| 31 |
+
audio_array, samplerate = load_audio_any_format(audio_path)
|
| 32 |
+
|
| 33 |
+
# 2️⃣ 计算切片
|
| 34 |
+
chunks = list(split_audio(audio_array, samplerate))
|
| 35 |
+
n_chunks = len(chunks)
|
| 36 |
|
|
|
|
| 37 |
all_texts = []
|
| 38 |
+
for i, chunk in enumerate(progress.tqdm(chunks, desc="语音转写中")):
|
| 39 |
res = asr({"array": chunk, "sampling_rate": samplerate})
|
| 40 |
text = res.get("text", "").strip()
|
| 41 |
all_texts.append(text)
|
|
|
|
| 42 |
|
| 43 |
full_text = " ".join(all_texts)
|
| 44 |
|
| 45 |
+
# 3️⃣ 生成摘要
|
| 46 |
+
progress(0.9, desc="生成摘要中...")
|
| 47 |
if len(full_text) < 1000:
|
| 48 |
summary = summarizer(full_text, max_length=200, min_length=50, do_sample=False)[0]["summary_text"]
|
| 49 |
else:
|
| 50 |
+
# 分块摘要后合并
|
| 51 |
+
chunks = [full_text[i:i + 1000] for i in range(0, len(full_text), 1000)]
|
| 52 |
summaries = [summarizer(c, max_length=200, min_length=50, do_sample=False)[0]["summary_text"] for c in chunks]
|
| 53 |
summary = " ".join(summaries)
|
| 54 |
|
| 55 |
+
progress(1.0, desc="完成 ✅")
|
| 56 |
+
|
| 57 |
return full_text, summary
|
| 58 |
|
| 59 |
+
|
| 60 |
+
# === Gradio 界面 ===
|
| 61 |
demo = gr.Interface(
|
| 62 |
fn=process_long_audio,
|
| 63 |
+
inputs=gr.Audio(type="filepath", label="上传面试录音(支持mp3/wav/m4a)"),
|
| 64 |
+
outputs=[
|
| 65 |
+
gr.Textbox(label="完整转写结果", lines=10),
|
| 66 |
+
gr.Textbox(label="面试重点摘要", lines=8)
|
| 67 |
+
],
|
| 68 |
+
title="AI 面试复盘助手 v2 🧠",
|
| 69 |
+
description="""
|
| 70 |
+
上传一段面试录音(推荐 3~30 分钟,支持 mp3 / wav / m4a 格式),
|
| 71 |
+
系统将自动识别语音文字并提炼面试要点摘要。
|
| 72 |
+
|
| 73 |
+
💡 小贴士:
|
| 74 |
+
- 上传清晰录音可提高准确度
|
| 75 |
+
- 若录音过长,系统会分段处理并显示进度
|
| 76 |
+
- 完成后可复制结果进行复盘或二次总结
|
| 77 |
+
"""
|
| 78 |
)
|
| 79 |
|
| 80 |
+
if __name__ == "__main__":
|
| 81 |
+
demo.launch()
|