Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
@@ -1,57 +1,71 @@
|
|
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 |
-
|
13 |
-
|
14 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
15 |
|
16 |
-
|
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 |
-
|
|
|
|
|
21 |
|
|
|
|
|
22 |
asr_processor, asr_model, translation_tokenizer, translation_model = load_models()
|
23 |
|
24 |
-
#
|
|
|
|
|
25 |
st.title("Real-Time Urdu to German Voice Translator")
|
26 |
-
st.markdown("Upload an Urdu audio file, and the app will translate it
|
27 |
|
28 |
-
|
|
|
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 |
-
|
36 |
-
|
|
|
|
|
|
|
|
|
|
|
37 |
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
|
|
43 |
|
44 |
-
|
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 |
-
|
|
|
|
|
52 |
|
53 |
-
|
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 |
-
|
|
|
|
|
|
|
|
1 |
import streamlit as st
|
2 |
import torch
|
3 |
+
from transformers import Wav2Vec2Processor, Wav2Vec2ForCTC, MarianMTModel, MarianTokenizer, Wav2Vec2CTCTokenizer
|
4 |
import soundfile as sf
|
5 |
import tempfile
|
6 |
+
import numpy as np
|
7 |
|
8 |
# Load models and tokenizers
|
9 |
@st.cache_resource
|
10 |
def load_models():
|
11 |
+
try:
|
12 |
+
# Load Wav2Vec2 for ASR (Multilingual model for Urdu support)
|
13 |
+
# Load the tokenizer directly using Wav2Vec2CTCTokenizer
|
14 |
+
tokenizer = Wav2Vec2CTCTokenizer.from_pretrained("facebook/wav2vec2-large-xlsr-53")
|
15 |
+
# Then, initialize the processor with the tokenizer
|
16 |
+
asr_processor = Wav2Vec2Processor(feature_extractor=asr_processor.feature_extractor, tokenizer=tokenizer)
|
17 |
+
asr_model = Wav2Vec2ForCTC.from_pretrained("facebook/wav2vec2-large-xlsr-53")
|
18 |
+
|
19 |
+
# Load MarianMT for translation (Urdu to German)
|
20 |
+
translation_tokenizer = MarianTokenizer.from_pretrained("Helsinki-NLP/opus-mt-ur-de")
|
21 |
+
translation_model = MarianMTModel.from_pretrained("Helsinki-NLP/opus-mt-ur-de")
|
22 |
|
23 |
+
return asr_processor, asr_model, translation_tokenizer, translation_model
|
|
|
|
|
24 |
|
25 |
+
except Exception as e:
|
26 |
+
st.error(f"Error loading models: {e}")
|
27 |
+
return None, None, None, None
|
28 |
|
29 |
+
|
30 |
+
# Initialize models
|
31 |
asr_processor, asr_model, translation_tokenizer, translation_model = load_models()
|
32 |
|
33 |
+
# ... (rest of your app.py code remains the same)
|
34 |
+
|
35 |
+
# Streamlit app interface
|
36 |
st.title("Real-Time Urdu to German Voice Translator")
|
37 |
+
st.markdown("Upload an Urdu audio file in `.wav` format, and the app will transcribe and translate it.")
|
38 |
|
39 |
+
# File uploader
|
40 |
+
uploaded_file = st.file_uploader("Upload your Urdu audio file (16kHz .wav)", type=["wav"])
|
41 |
|
42 |
if uploaded_file is not None:
|
43 |
with tempfile.NamedTemporaryFile(delete=False) as temp_file:
|
44 |
temp_file.write(uploaded_file.read())
|
45 |
temp_file_path = temp_file.name
|
46 |
|
47 |
+
try:
|
48 |
+
# Load and validate audio file
|
49 |
+
audio_input, sample_rate = sf.read(temp_file_path)
|
50 |
+
if sample_rate != 16000:
|
51 |
+
st.error("Audio file must have a sampling rate of 16kHz.")
|
52 |
+
else:
|
53 |
+
st.info("Processing the audio...")
|
54 |
|
55 |
+
# Step 1: Speech-to-Text (ASR)
|
56 |
+
input_values = asr_processor(audio_input, return_tensors="pt", sampling_rate=16000).input_values
|
57 |
+
with torch.no_grad():
|
58 |
+
logits = asr_model(input_values).logits
|
59 |
+
predicted_ids = torch.argmax(logits, dim=-1)
|
60 |
+
transcription = asr_processor.batch_decode(predicted_ids)[0]
|
61 |
|
62 |
+
st.text(f"Transcribed Urdu Text: {transcription}")
|
|
|
|
|
|
|
|
|
|
|
63 |
|
64 |
+
# Step 2: Translate Text (Urdu to German)
|
65 |
+
translated = translation_model.generate(**translation_tokenizer(transcription, return_tensors="pt", padding=True))
|
66 |
+
german_translation = translation_tokenizer.decode(translated[0], skip_special_tokens=True)
|
67 |
|
68 |
+
st.success(f"Translated German Text: {german_translation}")
|
|
|
|
|
69 |
|
70 |
+
except Exception as e:
|
71 |
+
st.error(f"An error occurred: {e}")
|