Upload 2 files
Browse files- app.py +94 -0
- requirements.txt +6 -0
app.py
ADDED
@@ -0,0 +1,94 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import whisper
|
3 |
+
from docx import Document
|
4 |
+
import os
|
5 |
+
import ssl
|
6 |
+
import shutil
|
7 |
+
|
8 |
+
# 设置缓存文件夹路径
|
9 |
+
CACHE_DIR = "transcription_cache"
|
10 |
+
|
11 |
+
# 确保缓存目录存在
|
12 |
+
if not os.path.exists(CACHE_DIR):
|
13 |
+
os.makedirs(CACHE_DIR)
|
14 |
+
|
15 |
+
# 禁用SSL证书验证(仅限临时使用,如果模型已本地化可移除)
|
16 |
+
ssl._create_default_https_context = ssl._create_unverified_context
|
17 |
+
|
18 |
+
# 加载本地模型(确保模型文件路径正确)
|
19 |
+
model_path = "small.pt" # 请确保模型已下载到此路径
|
20 |
+
model = whisper.load_model(model_path)
|
21 |
+
|
22 |
+
def format_time(seconds):
|
23 |
+
"""格式化秒数为分钟:秒钟的形式"""
|
24 |
+
mins, secs = divmod(int(seconds), 60)
|
25 |
+
return f"{mins:02}:{secs:02}"
|
26 |
+
|
27 |
+
def transcribe_and_generate_docx(audio_path, cache_subdir):
|
28 |
+
audio = whisper.load_audio(audio_path)
|
29 |
+
result = model.transcribe(audio, language=None) # 自动检测语言
|
30 |
+
segments = result["segments"]
|
31 |
+
|
32 |
+
# 构建文档
|
33 |
+
doc = Document()
|
34 |
+
doc.add_heading("语音转录结果", level=1)
|
35 |
+
|
36 |
+
transcript_preview = []
|
37 |
+
for i, segment in enumerate(segments):
|
38 |
+
start = segment['start']
|
39 |
+
end = segment['end']
|
40 |
+
text = segment['text'].strip()
|
41 |
+
|
42 |
+
timestamp = f"[{format_time(start)} - {format_time(end)}]"
|
43 |
+
paragraph_text = f"{timestamp} {text}"
|
44 |
+
|
45 |
+
doc.add_paragraph(paragraph_text)
|
46 |
+
transcript_preview.append(paragraph_text)
|
47 |
+
|
48 |
+
output_filename = os.path.join(cache_subdir, f"transcript_{os.path.basename(audio_path)}.docx")
|
49 |
+
doc.save(output_filename)
|
50 |
+
|
51 |
+
return output_filename, '\n\n'.join(transcript_preview)
|
52 |
+
|
53 |
+
# Streamlit界面定义
|
54 |
+
st.title("语音转录系统")
|
55 |
+
st.write("上传音频文件进行语音转录并生成Word文档。支持多种语言自动检测及时间戳。")
|
56 |
+
|
57 |
+
uploaded_file = st.file_uploader("选择一个音频文件", type=['mp3', 'wav','mp4', 'aac','m4a'])
|
58 |
+
|
59 |
+
if uploaded_file is not None:
|
60 |
+
# 创建一个新的子目录来存储本次任务的文件
|
61 |
+
cache_subdir = os.path.join(CACHE_DIR, str(len(os.listdir(CACHE_DIR)) + 1))
|
62 |
+
os.makedirs(cache_subdir)
|
63 |
+
|
64 |
+
# 保存上传的音频文件到缓存目录
|
65 |
+
temp_audio_path = os.path.join(cache_subdir, uploaded_file.name)
|
66 |
+
with open(temp_audio_path, "wb") as f:
|
67 |
+
f.write(uploaded_file.getvalue())
|
68 |
+
|
69 |
+
# 执行转录
|
70 |
+
output_filename, transcript_preview = transcribe_and_generate_docx(temp_audio_path, cache_subdir)
|
71 |
+
|
72 |
+
# 提供下载链接
|
73 |
+
st.download_button(
|
74 |
+
label="下载DOCX文件",
|
75 |
+
data=open(output_filename, "rb").read(),
|
76 |
+
file_name=output_filename,
|
77 |
+
mime="application/vnd.openxmlformats-officedocument.wordprocessingml.document",
|
78 |
+
)
|
79 |
+
|
80 |
+
# 显示转录预览
|
81 |
+
st.text_area("转录结果预览", value=transcript_preview, height=400)
|
82 |
+
|
83 |
+
# 展示历史记录
|
84 |
+
st.subheader("历史记录")
|
85 |
+
for idx, task_dir in enumerate(sorted(os.listdir(CACHE_DIR), key=lambda x: int(x))):
|
86 |
+
st.markdown(f"### 任务 {idx + 1}")
|
87 |
+
audio_files = [f for f in os.listdir(os.path.join(CACHE_DIR, task_dir)) if f.endswith('.mp3')]
|
88 |
+
docx_files = [f for f in os.listdir(os.path.join(CACHE_DIR, task_dir)) if f.endswith('.docx')]
|
89 |
+
|
90 |
+
if audio_files:
|
91 |
+
st.audio(os.path.join(CACHE_DIR, task_dir, audio_files[0]), format='audio/mp3')
|
92 |
+
if docx_files:
|
93 |
+
with open(os.path.join(CACHE_DIR, task_dir, docx_files[0]), "rb") as file_content:
|
94 |
+
st.download_button(label=f"下载任务 {idx + 1} 的 DOCX 文件", data=file_content, file_name=docx_files[0])
|
requirements.txt
ADDED
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
openai-whisper
|
2 |
+
python-docx
|
3 |
+
librosa
|
4 |
+
soundfile
|
5 |
+
scipy
|
6 |
+
numpy
|