Spaces:
Build error
Build error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,57 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
|
| 2 |
+
# app.py
|
| 3 |
+
import streamlit as st
|
| 4 |
+
import torch
|
| 5 |
+
from transformers import Wav2Vec2Processor, Wav2Vec2ForCTC, MarianMTModel, MarianTokenizer
|
| 6 |
+
import soundfile as sf
|
| 7 |
+
import tempfile
|
| 8 |
+
|
| 9 |
+
# Load models and tokenizers
|
| 10 |
+
@st.cache_resource
|
| 11 |
+
def load_models():
|
| 12 |
+
# Load ASR model (Wav2Vec2 for Urdu)
|
| 13 |
+
asr_processor = Wav2Vec2Processor.from_pretrained("m3hrdadfi/wav2vec2-large-xlsr-ur")
|
| 14 |
+
asr_model = Wav2Vec2ForCTC.from_pretrained("m3hrdadfi/wav2vec2-large-xlsr-ur")
|
| 15 |
+
|
| 16 |
+
# Load translation model (Urdu to German)
|
| 17 |
+
translation_tokenizer = MarianTokenizer.from_pretrained("Helsinki-NLP/opus-mt-ur-de")
|
| 18 |
+
translation_model = MarianMTModel.from_pretrained("Helsinki-NLP/opus-mt-ur-de")
|
| 19 |
+
|
| 20 |
+
return asr_processor, asr_model, translation_tokenizer, translation_model
|
| 21 |
+
|
| 22 |
+
asr_processor, asr_model, translation_tokenizer, translation_model = load_models()
|
| 23 |
+
|
| 24 |
+
# Streamlit App UI
|
| 25 |
+
st.title("Real-Time Urdu to German Voice Translator")
|
| 26 |
+
st.markdown("Upload an Urdu audio file, and the app will translate it to German.")
|
| 27 |
+
|
| 28 |
+
uploaded_file = st.file_uploader("Upload an audio file (in .wav format)", type=["wav"])
|
| 29 |
+
|
| 30 |
+
if uploaded_file is not None:
|
| 31 |
+
with tempfile.NamedTemporaryFile(delete=False) as temp_file:
|
| 32 |
+
temp_file.write(uploaded_file.read())
|
| 33 |
+
temp_file_path = temp_file.name
|
| 34 |
+
|
| 35 |
+
# Load audio file
|
| 36 |
+
audio_input, sample_rate = sf.read(temp_file_path)
|
| 37 |
+
|
| 38 |
+
# Ensure proper sampling rate
|
| 39 |
+
if sample_rate != 16000:
|
| 40 |
+
st.error("Please upload a .wav file with a sampling rate of 16kHz.")
|
| 41 |
+
else:
|
| 42 |
+
st.info("Processing the audio...")
|
| 43 |
+
|
| 44 |
+
# Convert speech to text (ASR)
|
| 45 |
+
input_values = asr_processor(audio_input, return_tensors="pt", sampling_rate=16000).input_values
|
| 46 |
+
with torch.no_grad():
|
| 47 |
+
logits = asr_model(input_values).logits
|
| 48 |
+
predicted_ids = torch.argmax(logits, dim=-1)
|
| 49 |
+
transcription = asr_processor.batch_decode(predicted_ids)[0]
|
| 50 |
+
|
| 51 |
+
st.text(f"Transcribed Urdu Text: {transcription}")
|
| 52 |
+
|
| 53 |
+
# Translate Urdu text to German
|
| 54 |
+
translated = translation_model.generate(**translation_tokenizer(transcription, return_tensors="pt", padding=True))
|
| 55 |
+
german_translation = translation_tokenizer.decode(translated[0], skip_special_tokens=True)
|
| 56 |
+
|
| 57 |
+
st.success(f"Translated German Text: {german_translation}")
|