Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,61 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
from transformers import pipeline
|
3 |
+
import speech_recognition as sr
|
4 |
+
import soundfile as sf
|
5 |
+
|
6 |
+
# Load translation models
|
7 |
+
models = {
|
8 |
+
"Urdu": "Helsinki-NLP/opus-mt-en-ur",
|
9 |
+
"Punjabi": "Helsinki-NLP/opus-mt-en-pu",
|
10 |
+
"Sindhi": "Helsinki-NLP/opus-mt-en-sd",
|
11 |
+
"Bihari": "Helsinki-NLP/opus-mt-en-hi", # Bihari is dialectically close to Hindi
|
12 |
+
"Arabic": "Helsinki-NLP/opus-mt-en-ar",
|
13 |
+
"Chinese": "Helsinki-NLP/opus-mt-en-zh",
|
14 |
+
"French": "Helsinki-NLP/opus-mt-en-fr",
|
15 |
+
"German": "Helsinki-NLP/opus-mt-en-de"
|
16 |
+
}
|
17 |
+
|
18 |
+
# Streamlit UI
|
19 |
+
st.title("Language Translator App")
|
20 |
+
st.write("Convert voice and text from English to various languages.")
|
21 |
+
|
22 |
+
# Input method selection
|
23 |
+
input_method = st.radio("Select input method:", ["Text", "Voice"])
|
24 |
+
|
25 |
+
# Select target language
|
26 |
+
target_language = st.selectbox("Select target language:", list(models.keys()))
|
27 |
+
|
28 |
+
# Load translation pipeline
|
29 |
+
translation_pipeline = pipeline("translation_en_to_" + target_language.lower(), model=models[target_language])
|
30 |
+
|
31 |
+
def translate_text(text):
|
32 |
+
translation = translation_pipeline(text)[0]['translation_text']
|
33 |
+
return translation
|
34 |
+
|
35 |
+
def translate_voice(audio_file):
|
36 |
+
recognizer = sr.Recognizer()
|
37 |
+
with sr.AudioFile(audio_file) as source:
|
38 |
+
audio_data = recognizer.record(source)
|
39 |
+
text = recognizer.recognize_google(audio_data)
|
40 |
+
return translate_text(text)
|
41 |
+
|
42 |
+
# Text translation
|
43 |
+
if input_method == "Text":
|
44 |
+
text_input = st.text_area("Enter English text here:")
|
45 |
+
if st.button("Translate Text"):
|
46 |
+
if text_input:
|
47 |
+
translated_text = translate_text(text_input)
|
48 |
+
st.write(f"**Translated Text ({target_language}):** {translated_text}")
|
49 |
+
else:
|
50 |
+
st.write("Please enter some text to translate.")
|
51 |
+
|
52 |
+
# Voice translation
|
53 |
+
elif input_method == "Voice":
|
54 |
+
st.write("Upload an English voice file (WAV format).")
|
55 |
+
voice_input = st.file_uploader("Upload Voice File", type=["wav"])
|
56 |
+
if st.button("Translate Voice"):
|
57 |
+
if voice_input:
|
58 |
+
translated_voice = translate_voice(voice_input)
|
59 |
+
st.write(f"**Translated Text ({target_language}):** {translated_voice}")
|
60 |
+
else:
|
61 |
+
st.write("Please upload a voice file to translate.")
|