Spaces:
Runtime error
Runtime error
Upload 2 files
Browse files- Speach-to-translated-speach.py +105 -0
- requirements.txt +5 -0
Speach-to-translated-speach.py
ADDED
@@ -0,0 +1,105 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import speech_recognition as sr
|
3 |
+
from googletrans import Translator
|
4 |
+
from cryptography.fernet import Fernet
|
5 |
+
import pyttsx3
|
6 |
+
|
7 |
+
# Function to encrypt the spoken words
|
8 |
+
def encrypt(text, key):
|
9 |
+
cipher = Fernet(key)
|
10 |
+
encrypted_text = cipher.encrypt(text.encode())
|
11 |
+
return encrypted_text
|
12 |
+
|
13 |
+
# Function to translate spoken words and speak the result
|
14 |
+
def translate_and_encrypt(source_language, target_language, encrypt_text):
|
15 |
+
# Initialize the speech recognition module
|
16 |
+
recognizer = sr.Recognizer()
|
17 |
+
|
18 |
+
# Use the default microphone as the audio source
|
19 |
+
with sr.Microphone() as source:
|
20 |
+
st.write("Say something:")
|
21 |
+
audio = recognizer.listen(source)
|
22 |
+
|
23 |
+
# Google Web Speech API for speech-to-text conversion
|
24 |
+
try:
|
25 |
+
spoken_text = recognizer.recognize_google(audio, language=source_language)
|
26 |
+
st.write("You said:", spoken_text)
|
27 |
+
|
28 |
+
# Translate spoken words to the selected target language
|
29 |
+
translator = Translator()
|
30 |
+
translated_text = translator.translate(spoken_text, dest=target_language).text
|
31 |
+
st.write("Translated text:", translated_text)
|
32 |
+
|
33 |
+
# Speak the translated text
|
34 |
+
speak(translated_text, target_language)
|
35 |
+
|
36 |
+
# Encryption key for security
|
37 |
+
encryption_key = Fernet.generate_key()
|
38 |
+
|
39 |
+
# Encrypt the translated text if the user chooses encryption
|
40 |
+
if encrypt_text:
|
41 |
+
encrypted_text = encrypt(translated_text, encryption_key)
|
42 |
+
st.write("Encrypted text:", encrypted_text)
|
43 |
+
else:
|
44 |
+
encrypted_text = None
|
45 |
+
|
46 |
+
# Store the translated text and encryption status in session state
|
47 |
+
if 'translation_history' not in st.session_state:
|
48 |
+
st.session_state.translation_history = []
|
49 |
+
|
50 |
+
st.session_state.translation_history.append({
|
51 |
+
'spoken_text': spoken_text,
|
52 |
+
'translated_text': translated_text,
|
53 |
+
'encrypted_text': encrypted_text
|
54 |
+
})
|
55 |
+
|
56 |
+
except sr.UnknownValueError:
|
57 |
+
st.write("Sorry, could not understand audio.")
|
58 |
+
except sr.RequestError as e:
|
59 |
+
st.write(f"Could not request results from Google Web Speech API; {e}")
|
60 |
+
|
61 |
+
def speak(text, language):
|
62 |
+
# Initialize the text-to-speech engine with SAPI5 voices
|
63 |
+
engine = pyttsx3.init()
|
64 |
+
voices = engine.getProperty('voices')
|
65 |
+
|
66 |
+
if language == "hindi":
|
67 |
+
engine.setProperty('voice', voices[0].id) # Select Hindi voice
|
68 |
+
elif language == "urdu":
|
69 |
+
engine.setProperty('voice', voices[1].id) # Select Urdu voice
|
70 |
+
|
71 |
+
engine.say(text)
|
72 |
+
engine.runAndWait()
|
73 |
+
|
74 |
+
# Streamlit UI
|
75 |
+
st.title("Speech Translation and Encryption")
|
76 |
+
|
77 |
+
# Language selection
|
78 |
+
language_options = ["en", "es", "fr", "de", "hindi", "urdu"]
|
79 |
+
language_names = ["English", "Spanish", "French", "German", "Hindi", "Urdu"]
|
80 |
+
|
81 |
+
source_language = st.selectbox("Select Source Language:", language_names, index=0)
|
82 |
+
target_language = st.selectbox("Select Target Language:", language_names, index=1)
|
83 |
+
|
84 |
+
# Checkbox for encryption
|
85 |
+
encrypt_text = st.checkbox("Encrypt Text")
|
86 |
+
|
87 |
+
# Button to trigger translation and encryption
|
88 |
+
if st.button("Translate and Encrypt"):
|
89 |
+
translate_and_encrypt(source_language, target_language, encrypt_text)
|
90 |
+
|
91 |
+
# Navigation to the Translation History tab
|
92 |
+
if 'translation_history' in st.session_state and st.session_state.translation_history:
|
93 |
+
st.title("Translation History")
|
94 |
+
st.table(st.session_state.translation_history)
|
95 |
+
|
96 |
+
# Organize the UI in tabs
|
97 |
+
tabs = ["Translation History"]
|
98 |
+
selected_tab = st.sidebar.radio("Select Tab", tabs)
|
99 |
+
|
100 |
+
if selected_tab == "Translation History":
|
101 |
+
if 'translation_history' in st.session_state and st.session_state.translation_history:
|
102 |
+
st.title("Translation History")
|
103 |
+
st.table(st.session_state.translation_history)
|
104 |
+
else:
|
105 |
+
st.write("No translation history yet.")
|
requirements.txt
ADDED
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
streamlit
|
2 |
+
SpeechRecognition
|
3 |
+
googletrans
|
4 |
+
cryptography
|
5 |
+
pyttsx3
|