Spaces:
Sleeping
Sleeping
File size: 5,143 Bytes
48b403b 11d2e2e 48b403b 11d2e2e 48b403b | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 | import streamlit as st
import whisper
import tempfile
import os
import re
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
# =========================
# PAGE CONFIG
# =========================
st.set_page_config(page_title="Speech to Text Translator", page_icon="ποΈ", layout="centered")
# =========================
# UI STYLE
# =========================
st.markdown("""
<style>
html, body, [data-testid="stAppViewContainer"] {
background: radial-gradient(circle at 20% 20%, #1e293b, #020617 70%);
color: white;
font-family: 'Inter', sans-serif;
}
.title {
text-align: center;
font-size: 40px;
font-weight: 700;
}
.subtitle {
text-align: center;
color: #cbd5e1;
margin-bottom: 22px;
}
[data-testid="stFileUploader"] {
border-radius: 16px;
border: 1px dashed rgba(255,255,255,0.25);
}
.result-box {
background: rgba(16,185,129,0.12);
border-radius: 16px;
padding: 16px;
border: 1px solid rgba(16,185,129,0.35);
}
</style>
""", unsafe_allow_html=True)
# =========================
# HEADER
# =========================
st.markdown('<div class="title"> Speech to Text Translator</div>', unsafe_allow_html=True)
st.markdown('<div class="subtitle">Transcribe speech or translate into any language</div>', unsafe_allow_html=True)
# =========================
# TEXT UTILS
# =========================
def split_text(text, max_len=200):
sentences = re.split(r'(?<=[.!?γοΌοΌ])', text)
chunks = []
cur = ""
for s in sentences:
if len(cur) + len(s) < max_len:
cur += " " + s
else:
chunks.append(cur.strip())
cur = s
if cur:
chunks.append(cur.strip())
return chunks
# =========================
# MODEL CACHE
# =========================
@st.cache_resource
def load_models():
whisper_model = whisper.load_model("base")
model_name = "facebook/nllb-200-distilled-600M"
tokenizer = AutoTokenizer.from_pretrained(model_name)
nllb_model = AutoModelForSeq2SeqLM.from_pretrained(model_name)
return whisper_model, tokenizer, nllb_model
whisper_model, tokenizer, nllb_model = load_models()
# =========================
# LANGUAGE MAP
# =========================
LANG_CODE = {
"Arabic":"arb_Arab","Assamese":"asm_Beng","Awadhi":"awa_Deva",
"Bengali":"ben_Beng","Bhojpuri":"bho_Deva","Chinese":"zho_Hans",
"English":"eng_Latn","French":"fra_Latn","German":"deu_Latn",
"Hindi":"hin_Deva","Japanese":"jpn_Jpan","Korean":"kor_Hang",
"Maithili":"mai_Deva","Marathi":"mar_Deva","Persian":"pes_Arab",
"Punjabi":"pan_Guru","Russian":"rus_Cyrl","Sanskrit":"san_Deva",
"Spanish":"spa_Latn","Tamil":"tam_Taml","Telugu":"tel_Telu",
"Urdu":"urd_Arab","Vietnamese":"vie_Latn"
}
# =========================
# OPTIONS
# =========================
col1, col2 = st.columns(2)
with col1:
transcribe = st.checkbox("π Transcribe")
with col2:
translate = st.checkbox("π Translate", value=True)
target_lang = None
if translate:
target_lang = st.selectbox("Translate into", sorted(LANG_CODE.keys()))
# =========================
# UPLOAD
# =========================
audio_file = st.file_uploader(
"Upload audio (MP3, WAV, M4A, MP4)",
type=["mp3","wav","m4a","mp4"]
)
if audio_file:
st.audio(audio_file)
# =========================
# PROCESS
# =========================
if st.button(" Process Audio") and audio_file:
with st.spinner("Processing audio..."):
# Save uploaded audio
with tempfile.NamedTemporaryFile(delete=False, suffix=".mp3") as tmp:
tmp.write(audio_file.read())
tmp_path = tmp.name
# ---------- TRANSCRIBE ----------
result = whisper_model.transcribe(tmp_path, fp16=False)
detected_lang = result["language"]
text = result["text"]
st.success(f"Detected language: {detected_lang}")
output_text = text
# ---------- TRANSLATE ----------
if translate and target_lang:
tgt_code = LANG_CODE[target_lang]
chunks = split_text(text)
translated_parts = []
for chunk in chunks:
inputs = tokenizer(chunk, return_tensors="pt")
tokens = nllb_model.generate(
**inputs,
forced_bos_token_id=tokenizer.convert_tokens_to_ids(tgt_code),
max_length=256,
num_beams=4,
no_repeat_ngram_size=3,
repetition_penalty=1.2,
early_stopping=True
)
translated = tokenizer.batch_decode(tokens, skip_special_tokens=True)[0]
translated_parts.append(translated)
output_text = " ".join(translated_parts)
# ================= OUTPUT =================
st.markdown("### π Output Text")
st.markdown(f'<div class="result-box">{output_text}</div>', unsafe_allow_html=True)
st.download_button(
"β¬ Download Text",
output_text,
file_name="translated_text.txt"
)
os.remove(tmp_path) |