-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp1.py
176 lines (157 loc) · 7.23 KB
/
app1.py
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
#===============================================================================================================
# streamlit based frontend ui with authentication (same as app.py only used streamlit for ui instead of flask)
#===============================================================================================================
import json
import hashlib
import streamlit as st
import subprocess
import os
from playsound import playsound
import speech_recognition as sr
from gtts import gTTS
LANGUAGE = "en"
def SpeakText(command, langinp=LANGUAGE):
"""
Text to Speech using GTTS
Args:
command (str): Text to speak
langinp (str, optional): Output language. Defaults to "en".
"""
if langinp == "": langinp = "en"
tts = gTTS(text=command, lang=langinp)
tts.save("~tempfile01.mp3")
playsound("~tempfile01.mp3")
print(command)
os.remove("~tempfile01.mp3")
def speech_to_text():
"""
Speech to text
Returns:
str: Returns transcripted text
"""
r = sr.Recognizer()
try:
with sr.Microphone() as source2:
r.adjust_for_ambient_noise(source2, duration=0.2)
audio2 = r.listen(source2)
MyText = r.recognize_google(audio2)
print("You said: "+MyText)
return MyText
except sr.RequestError as e:
print("Could not request results; {0}".format(e))
return None
except sr.UnknownValueError:
print("unknown error occured")
return None
# Function to hash the password
def hash_password(password):
return hashlib.sha256(password.encode()).hexdigest()
# Function to check if the entered credentials match the registered user
def authenticate_user(username, password):
with open('users.json', 'r') as file:
users = json.load(file)
for user in users:
if user["username"] == username and user["hashed_password"] == hash_password(password):
return True
return False
# Function to register a new user
def register_user(username, password, email, email_password):
new_user = {
"username": username,
"hashed_password": hash_password(password),
"email": email,
"email_password": email_password
}
with open('users.json', 'r+') as file:
users = json.load(file)
users.append(new_user)
file.seek(0)
json.dump(users, file, indent=4)
# Main function for the Streamlit web app
def main():
# Define session state
if 'authenticated' not in st.session_state:
st.session_state.authenticated = False
if 'username' not in st.session_state:
st.session_state.username = None
st.title("Voice-based Email System")
# Display registration form if not authenticated
if not st.session_state.authenticated:
st.sidebar.title("User Registration")
new_username = st.sidebar.text_input("Enter New Username:")
new_password = st.sidebar.text_input("Enter New Password:", type="password")
new_email = st.sidebar.text_input("Enter Email Address:")
new_email_password = st.sidebar.text_input("Enter Email Password:", type="password")
if st.sidebar.button("Register"):
register_user(new_username, new_password, new_email, new_email_password)
st.sidebar.success("Registration Successful!")
SpeakText("Registration is successful. Please use our voice based login by clicking on the login button below to use the application")
if not st.session_state.authenticated:
st.header("Login")
# Text input for username and password
username = st.text_input("Enter Username:", key="uname")
password = st.text_input("Enter Password:", type="password", key="auth_pword")
if st.button("Login", key="login"):
if authenticate_user(username, password):
st.session_state.authenticated = True
st.session_state.username = username
st.success("Login Successful!")
# Voice login option
st.header("Voice based login")
if st.button("Login with Voice"):
SpeakText("Please say your username")
username_voice = speech_to_text()
if username_voice:
username_voice = username_voice.lower().strip().replace(" ", "")
st.write("You entered: "+username_voice)
SpeakText("You entered: "+username_voice)
SpeakText("Please say your password")
st.write("Authenticating user Please wait")
SpeakText("Authenticating user Please wait")
password_voice = speech_to_text()
if password_voice:
password_voice = password_voice.lower().strip().replace(" ", "")
if authenticate_user(username_voice, password_voice):
st.session_state.authenticated = True
st.session_state.username = username_voice
st.success("Login Successful!")
SpeakText("Login is successful")
SpeakText("Welcome "+ st.session_state.username)
SpeakText("Click on start model button to use the email services")
else:
st.error("Login failed. Please try again with your registered id.")
SpeakText("Login failed. Please try again with your registered id.")
else:
st.error("Password not recognized. Please try again.")
SpeakText("Password not recognized. Please try again.")
else:
st.error("Username not recognized. Please try again.")
SpeakText("Username not recognized. Please try again.")
# Display welcome message and other content if authenticated
if st.session_state.authenticated:
st.write(f"Welcome {st.session_state.username}!")
if st.button("Start ML Model", key="start_model"):
# Pass email and email_password to speechtext2.py
email = None
email_password = None
with open('users.json', 'r') as file:
users = json.load(file)
for user in users:
if user["username"] == st.session_state.username:
email = user["email"]
email_password = user["email_password"]
break
if email and email_password:
subprocess.run(["python", "speechtext2.py", email, email_password,"en"])
st.write("ML Model is starting...")
SpeakText("Model is starting please wait")
else:
st.error("User's email or email password not found.")
SpeakText("User's email or email password not found.")
if st.button("Logout", key="logout"):
st.session_state.authenticated = False
st.session_state.username = None
st.success("Logout Successful!")
SpeakText("You have successfully loged out")
if __name__ == "__main__":
main()