Download pages/7_tts_script.py from PerrinT/office: direct link, hf CLI and curl.
- Browser
- Download file 2.43 kB
-
https://huggingface.co/spaces/PerrinT/office/resolve/main/pages/7_tts_script.py
- Command line
-
hf download hf://spaces/PerrinT/office/pages/7_tts_script.py
-
curl -L -o 7_tts_script.py https://huggingface.co/spaces/PerrinT/office/resolve/main/pages/7_tts_script.py
2.43 kB
| import streamlit as st | |
| import sys | |
| sys.path.append('..') | |
| from utils.tts_utils import generate_tts_script | |
| st.set_page_config(page_title="语音脚本", page_icon="🎙️", layout="wide") | |
| st.title("🎙️ 文本转语音脚本") | |
| st.markdown("---") | |
| st.markdown("输入文本,自动生成带停顿、语速、强调标记的朗读脚本,可直接用于TTS工具") | |
| # 输入区域 | |
| tab1, tab2 = st.tabs(["手动输入", "上传文件"]) | |
| text = "" | |
| with tab1: | |
| text = st.text_area( | |
| "粘贴文本内容", | |
| height=300, | |
| placeholder="在这里粘贴需要转换为语音脚本的文本...", | |
| key="tts_text" | |
| ) | |
| with tab2: | |
| uploaded = st.file_uploader("上传文本文件", type=['txt', 'md'], key="tts_file") | |
| if uploaded: | |
| text = uploaded.read().decode('utf-8') | |
| st.text_area("文件内容预览", text[:500] + "..." if len(text) > 500 else text, height=100, key="tts_preview") | |
| # 设置选项 | |
| st.subheader("脚本设置") | |
| col1, col2 = st.columns(2) | |
| with col1: | |
| speed = st.selectbox("默认语速", ["慢速", "正常", "快速"], index=1, key="speed") | |
| add_pauses = st.checkbox("自动添加停顿标记", value=True, key="pauses") | |
| with col2: | |
| add_emphasis = st.checkbox("自动标记数字和强调", value=True, key="emphasis") | |
| output_format = st.selectbox("输出格式", ["纯文本", "带标记文本", "SSML"], key="format") | |
| # 生成 | |
| if st.button("生成语音脚本", key="gen_tts"): | |
| if not text.strip(): | |
| st.warning("请输入或上传文本内容") | |
| else: | |
| speed_map = {"慢速": "slow", "正常": "normal", "快速": "fast"} | |
| result = generate_tts_script( | |
| text, | |
| speed=speed_map[speed], | |
| add_pauses=add_pauses, | |
| add_emphasis=add_emphasis | |
| ) | |
| st.subheader("生成结果") | |
| st.code(result, language="text") | |
| # 统计信息 | |
| line_count = len(result.strip().split("\n")) | |
| char_count = len(result) | |
| st.caption(f"共 {line_count} 行,{char_count} 字符") | |
| col1, col2 = st.columns(2) | |
| with col1: | |
| st.download_button( | |
| "下载脚本", result, "tts_script.txt", "text/plain" | |
| ) | |
| with col2: | |
| # 估算朗读时间(中文约每分钟200字) | |
| est_minutes = char_count / 200 | |
| st.info(f"预计朗读时长:约 {est_minutes:.1f} 分钟") | |