Spaces:
Sleeping
Sleeping
Michael Hu
commited on
Commit
·
cd1309d
1
Parent(s):
71acd53
initial check in
Browse files- LICENSE +21 -0
- app.py +118 -0
- pyproject.toml +60 -0
- utils/stt.py +51 -0
- utils/translation.py +45 -0
- utils/tts.py +46 -0
LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
MIT License
|
| 2 |
+
|
| 3 |
+
Copyright (c) 2025 Michael
|
| 4 |
+
|
| 5 |
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
| 6 |
+
of this software and associated documentation files (the "Software"), to deal
|
| 7 |
+
in the Software without restriction, including without limitation the rights
|
| 8 |
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
| 9 |
+
copies of the Software, and to permit persons to whom the Software is
|
| 10 |
+
furnished to do so, subject to the following conditions:
|
| 11 |
+
|
| 12 |
+
The above copyright notice and this permission notice shall be included in all
|
| 13 |
+
copies or substantial portions of the Software.
|
| 14 |
+
|
| 15 |
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
| 16 |
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
| 17 |
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
| 18 |
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
| 19 |
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
| 20 |
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
| 21 |
+
SOFTWARE.
|
app.py
ADDED
|
@@ -0,0 +1,118 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
Main entry point for the Audio Translation Web Application
|
| 3 |
+
Handles file upload, processing pipeline, and UI rendering
|
| 4 |
+
"""
|
| 5 |
+
|
| 6 |
+
import streamlit as st
|
| 7 |
+
import os
|
| 8 |
+
import time
|
| 9 |
+
from dotenv import load_dotenv
|
| 10 |
+
from utils.stt import transcribe_audio
|
| 11 |
+
from utils.translation import translate_text
|
| 12 |
+
from utils.tts import generate_speech
|
| 13 |
+
|
| 14 |
+
# Initialize environment configurations
|
| 15 |
+
load_dotenv()
|
| 16 |
+
os.makedirs("temp/uploads", exist_ok=True)
|
| 17 |
+
os.makedirs("temp/outputs", exist_ok=True)
|
| 18 |
+
|
| 19 |
+
def configure_page():
|
| 20 |
+
"""Set up Streamlit page configuration"""
|
| 21 |
+
st.set_page_config(
|
| 22 |
+
page_title="Audio Translator",
|
| 23 |
+
page_icon="🎧",
|
| 24 |
+
layout="wide",
|
| 25 |
+
initial_sidebar_state="expanded"
|
| 26 |
+
)
|
| 27 |
+
st.markdown("""
|
| 28 |
+
<style>
|
| 29 |
+
.reportview-container {margin-top: -2em;}
|
| 30 |
+
#MainMenu {visibility: hidden;}
|
| 31 |
+
.stDeployButton {display:none;}
|
| 32 |
+
</style>
|
| 33 |
+
""", unsafe_allow_html=True)
|
| 34 |
+
|
| 35 |
+
def handle_file_processing(upload_path):
|
| 36 |
+
"""
|
| 37 |
+
Execute the complete processing pipeline:
|
| 38 |
+
1. Speech-to-Text (STT)
|
| 39 |
+
2. Machine Translation
|
| 40 |
+
3. Text-to-Speech (TTS)
|
| 41 |
+
"""
|
| 42 |
+
progress_bar = st.progress(0)
|
| 43 |
+
status_text = st.empty()
|
| 44 |
+
|
| 45 |
+
try:
|
| 46 |
+
# STT Phase
|
| 47 |
+
status_text.markdown("🔍 **Performing Speech Recognition...**")
|
| 48 |
+
english_text = transcribe_audio(upload_path)
|
| 49 |
+
progress_bar.progress(30)
|
| 50 |
+
|
| 51 |
+
# Translation Phase
|
| 52 |
+
status_text.markdown("🌐 **Translating Content...**")
|
| 53 |
+
chinese_text = translate_text(english_text)
|
| 54 |
+
progress_bar.progress(60)
|
| 55 |
+
|
| 56 |
+
# TTS Phase
|
| 57 |
+
status_text.markdown("🎵 **Generating Chinese Speech...**")
|
| 58 |
+
output_path = generate_speech(chinese_text)
|
| 59 |
+
progress_bar.progress(100)
|
| 60 |
+
|
| 61 |
+
# Display results
|
| 62 |
+
status_text.success("✅ Processing Complete!")
|
| 63 |
+
return english_text, chinese_text, output_path
|
| 64 |
+
|
| 65 |
+
except Exception as e:
|
| 66 |
+
status_text.error(f"❌ Processing Failed: {str(e)}")
|
| 67 |
+
st.exception(e)
|
| 68 |
+
raise
|
| 69 |
+
|
| 70 |
+
def render_results(english_text, chinese_text, output_path):
|
| 71 |
+
"""Display processing results in organized columns"""
|
| 72 |
+
st.divider()
|
| 73 |
+
|
| 74 |
+
col1, col2 = st.columns([2, 1])
|
| 75 |
+
with col1:
|
| 76 |
+
st.subheader("Recognition Results")
|
| 77 |
+
st.code(english_text, language="text")
|
| 78 |
+
|
| 79 |
+
st.subheader("Translation Results")
|
| 80 |
+
st.code(chinese_text, language="text")
|
| 81 |
+
|
| 82 |
+
with col2:
|
| 83 |
+
st.subheader("Audio Output")
|
| 84 |
+
st.audio(output_path)
|
| 85 |
+
with open(output_path, "rb") as f:
|
| 86 |
+
st.download_button(
|
| 87 |
+
label="Download Audio",
|
| 88 |
+
data=f,
|
| 89 |
+
file_name="translated_audio.wav",
|
| 90 |
+
mime="audio/wav"
|
| 91 |
+
)
|
| 92 |
+
|
| 93 |
+
def main():
|
| 94 |
+
"""Main application workflow"""
|
| 95 |
+
configure_page()
|
| 96 |
+
st.title("🎧 High-Quality Audio Translation System")
|
| 97 |
+
st.markdown("Upload English Audio → Get Chinese Speech Output")
|
| 98 |
+
|
| 99 |
+
# File uploader widget
|
| 100 |
+
uploaded_file = st.file_uploader(
|
| 101 |
+
"Select Audio File (MP3/WAV)",
|
| 102 |
+
type=["mp3", "wav"],
|
| 103 |
+
accept_multiple_files=False
|
| 104 |
+
)
|
| 105 |
+
|
| 106 |
+
if uploaded_file:
|
| 107 |
+
# Save uploaded file
|
| 108 |
+
upload_path = os.path.join("temp/uploads", uploaded_file.name)
|
| 109 |
+
with open(upload_path, "wb") as f:
|
| 110 |
+
f.write(uploaded_file.getbuffer())
|
| 111 |
+
|
| 112 |
+
# Execute processing pipeline
|
| 113 |
+
results = handle_file_processing(upload_path)
|
| 114 |
+
if results:
|
| 115 |
+
render_results(*results)
|
| 116 |
+
|
| 117 |
+
if __name__ == "__main__":
|
| 118 |
+
main()
|
pyproject.toml
ADDED
|
@@ -0,0 +1,60 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
[tool.poetry]
|
| 2 |
+
name = "teaching-assistant"
|
| 3 |
+
version = "0.1.0"
|
| 4 |
+
description = "High-quality audio translation web application"
|
| 5 |
+
authors = ["Your Name <[email protected]>"]
|
| 6 |
+
license = "MIT"
|
| 7 |
+
keywords = ["nlp", "translation", "speech-processing"]
|
| 8 |
+
|
| 9 |
+
[tool.poetry.dependencies]
|
| 10 |
+
python = "^3.9"
|
| 11 |
+
|
| 12 |
+
# Core dependencies
|
| 13 |
+
streamlit = "^1.31.1"
|
| 14 |
+
pydub = "^0.25.1"
|
| 15 |
+
python-dotenv = "^1.0.0"
|
| 16 |
+
nltk = "^3.8.1" # Text segmentation
|
| 17 |
+
librosa = "^0.10.1" # Advanced audio processing
|
| 18 |
+
soundfile = "^0.12.1" # Audio file I/O
|
| 19 |
+
ffmpeg-python = "^0.2.0" # FFmpeg integration
|
| 20 |
+
|
| 21 |
+
# Machine learning frameworks
|
| 22 |
+
torch = { version = "^2.2.1", source = "pytorch" }
|
| 23 |
+
transformers = { version = "^4.38.2", extras = ["audio"] }
|
| 24 |
+
|
| 25 |
+
# Text-to-speech engine
|
| 26 |
+
TTS = "^0.21.0"
|
| 27 |
+
|
| 28 |
+
# Platform-specific dependencies
|
| 29 |
+
torchaudio = { version = "^2.2.1", source = "pytorch", optional = true }
|
| 30 |
+
|
| 31 |
+
[tool.poetry.group.dev.dependencies]
|
| 32 |
+
black = "^24.3.0"
|
| 33 |
+
flake8 = "^6.1.0"
|
| 34 |
+
mypy = "^1.8.0"
|
| 35 |
+
pytest = "^8.0.2"
|
| 36 |
+
|
| 37 |
+
[[tool.poetry.source]]
|
| 38 |
+
name = "pytorch"
|
| 39 |
+
url = "https://download.pytorch.org/whl/cpu"
|
| 40 |
+
priority = "primary"
|
| 41 |
+
|
| 42 |
+
[build-system]
|
| 43 |
+
requires = ["poetry-core>=1.3.2"]
|
| 44 |
+
build-backend = "poetry.core.masonry.api"
|
| 45 |
+
|
| 46 |
+
[tool.poetry.extras]
|
| 47 |
+
gpu = ["torchaudio"]
|
| 48 |
+
|
| 49 |
+
[tool.poetry.scripts]
|
| 50 |
+
start = "app:main"
|
| 51 |
+
|
| 52 |
+
[project.urls]
|
| 53 |
+
Documentation = "https://github.com/yourusername/audio-translator/wiki"
|
| 54 |
+
Issue-Tracker = "https://github.com/yourusername/audio-translator/issues"
|
| 55 |
+
|
| 56 |
+
# Configuration notes:
|
| 57 |
+
# 1. Torch dependencies are sourced from PyTorch's official repository
|
| 58 |
+
# 2. Transformers include audio processing extras
|
| 59 |
+
# 3. GPU support can be enabled via: poetry install --extras "gpu"
|
| 60 |
+
# 4. Platform-specific dependencies are handled through optional groups
|
utils/stt.py
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
Speech Recognition Module using Whisper Large-v3
|
| 3 |
+
Handles audio preprocessing and transcription
|
| 4 |
+
"""
|
| 5 |
+
|
| 6 |
+
import torch
|
| 7 |
+
from transformers import AutoModelForSpeechSeq2Seq, AutoProcessor
|
| 8 |
+
from pydub import AudioSegment
|
| 9 |
+
|
| 10 |
+
def transcribe_audio(audio_path):
|
| 11 |
+
"""
|
| 12 |
+
Convert audio file to text using Whisper ASR model
|
| 13 |
+
Args:
|
| 14 |
+
audio_path: Path to input audio file
|
| 15 |
+
Returns:
|
| 16 |
+
Transcribed English text
|
| 17 |
+
"""
|
| 18 |
+
# Configure hardware settings
|
| 19 |
+
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 20 |
+
|
| 21 |
+
# Convert to proper audio format
|
| 22 |
+
audio = AudioSegment.from_file(audio_path)
|
| 23 |
+
processed_audio = audio.set_frame_rate(16000).set_channels(1)
|
| 24 |
+
wav_path = audio_path.replace(".mp3", ".wav")
|
| 25 |
+
processed_audio.export(wav_path, format="wav")
|
| 26 |
+
|
| 27 |
+
# Initialize ASR model
|
| 28 |
+
model = AutoModelForSpeechSeq2Seq.from_pretrained(
|
| 29 |
+
"openai/whisper-large-v3",
|
| 30 |
+
torch_dtype=torch.float32,
|
| 31 |
+
low_cpu_mem_usage=True,
|
| 32 |
+
use_safetensors=True
|
| 33 |
+
).to(device)
|
| 34 |
+
|
| 35 |
+
processor = AutoProcessor.from_pretrained("openai/whisper-large-v3")
|
| 36 |
+
|
| 37 |
+
# Process audio input
|
| 38 |
+
inputs = processor(
|
| 39 |
+
wav_path,
|
| 40 |
+
sampling_rate=16000,
|
| 41 |
+
return_tensors="pt",
|
| 42 |
+
truncation=True,
|
| 43 |
+
chunk_length_s=30,
|
| 44 |
+
stride_length_s=5
|
| 45 |
+
).to(device)
|
| 46 |
+
|
| 47 |
+
# Generate transcription
|
| 48 |
+
with torch.no_grad():
|
| 49 |
+
outputs = model.generate(**inputs, language="en", task="transcribe")
|
| 50 |
+
|
| 51 |
+
return processor.batch_decode(outputs, skip_special_tokens=True)[0]
|
utils/translation.py
ADDED
|
@@ -0,0 +1,45 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
Text Translation Module using NLLB-3.3B model
|
| 3 |
+
Handles text segmentation and batch translation
|
| 4 |
+
"""
|
| 5 |
+
|
| 6 |
+
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
|
| 7 |
+
|
| 8 |
+
def translate_text(text):
|
| 9 |
+
"""
|
| 10 |
+
Translate English text to Simplified Chinese
|
| 11 |
+
Args:
|
| 12 |
+
text: Input English text
|
| 13 |
+
Returns:
|
| 14 |
+
Translated Chinese text
|
| 15 |
+
"""
|
| 16 |
+
# Initialize translation model
|
| 17 |
+
tokenizer = AutoTokenizer.from_pretrained("facebook/nllb-200-3.3B")
|
| 18 |
+
model = AutoModelForSeq2SeqLM.from_pretrained("facebook/nllb-200-3.3B")
|
| 19 |
+
|
| 20 |
+
# Split long text into manageable chunks
|
| 21 |
+
max_chunk_length = 1000
|
| 22 |
+
text_chunks = [
|
| 23 |
+
text[i:i+max_chunk_length]
|
| 24 |
+
for i in range(0, len(text), max_chunk_length)
|
| 25 |
+
]
|
| 26 |
+
|
| 27 |
+
translated_chunks = []
|
| 28 |
+
for chunk in text_chunks:
|
| 29 |
+
# Prepare model inputs
|
| 30 |
+
inputs = tokenizer(
|
| 31 |
+
chunk,
|
| 32 |
+
return_tensors="pt",
|
| 33 |
+
max_length=1024,
|
| 34 |
+
truncation=True
|
| 35 |
+
)
|
| 36 |
+
|
| 37 |
+
# Generate translation
|
| 38 |
+
outputs = model.generate(
|
| 39 |
+
**inputs,
|
| 40 |
+
forced_bos_token_id=tokenizer.lang_code_to_id["zho_Hans"],
|
| 41 |
+
max_new_tokens=1024
|
| 42 |
+
)
|
| 43 |
+
translated_chunks.append(tokenizer.decode(outputs[0], skip_special_tokens=True))
|
| 44 |
+
|
| 45 |
+
return "".join(translated_chunks)
|
utils/tts.py
ADDED
|
@@ -0,0 +1,46 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
Text-to-Speech Module using YourTTS
|
| 3 |
+
Handles speech synthesis and output generation
|
| 4 |
+
"""
|
| 5 |
+
|
| 6 |
+
from TTS.api import TTS
|
| 7 |
+
import os
|
| 8 |
+
import time
|
| 9 |
+
|
| 10 |
+
def generate_speech(text):
|
| 11 |
+
"""
|
| 12 |
+
Convert Chinese text to natural-sounding speech
|
| 13 |
+
Args:
|
| 14 |
+
text: Input Chinese text
|
| 15 |
+
Returns:
|
| 16 |
+
Path to generated audio file
|
| 17 |
+
"""
|
| 18 |
+
# Initialize TTS engine
|
| 19 |
+
tts = TTS(
|
| 20 |
+
model_name="tts_models/multilingual/multi-dataset/your_tts",
|
| 21 |
+
progress_bar=False,
|
| 22 |
+
gpu=False
|
| 23 |
+
)
|
| 24 |
+
|
| 25 |
+
# Create unique output filename
|
| 26 |
+
output_path = os.path.join(
|
| 27 |
+
"temp/outputs",
|
| 28 |
+
f"output_{int(time.time())}.wav"
|
| 29 |
+
)
|
| 30 |
+
|
| 31 |
+
# Use reference voice if available
|
| 32 |
+
ref_voice = (
|
| 33 |
+
"assets/reference_voice.wav"
|
| 34 |
+
if os.path.exists("assets/reference_voice.wav")
|
| 35 |
+
else None
|
| 36 |
+
)
|
| 37 |
+
|
| 38 |
+
# Generate speech output
|
| 39 |
+
tts.tts_to_file(
|
| 40 |
+
text=text,
|
| 41 |
+
speaker_wav=ref_voice,
|
| 42 |
+
language="zh-cn",
|
| 43 |
+
file_path=output_path
|
| 44 |
+
)
|
| 45 |
+
|
| 46 |
+
return output_path
|