Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,59 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import speech_recognition as sr
|
2 |
+
import pyttsx3
|
3 |
+
import json
|
4 |
+
|
5 |
+
# Load user credentials (Replace with database if needed)
|
6 |
+
USER_CREDENTIALS = {
|
7 |
+
"john_doe": "hello123",
|
8 |
+
"alice_smith": "welcome456"
|
9 |
+
}
|
10 |
+
|
11 |
+
# Initialize Text-to-Speech Engine
|
12 |
+
engine = pyttsx3.init()
|
13 |
+
engine.setProperty('rate', 150)
|
14 |
+
|
15 |
+
def speak(text):
|
16 |
+
"""Convert text to speech."""
|
17 |
+
engine.say(text)
|
18 |
+
engine.runAndWait()
|
19 |
+
|
20 |
+
def recognize_speech():
|
21 |
+
"""Capture and recognize speech input."""
|
22 |
+
recognizer = sr.Recognizer()
|
23 |
+
with sr.Microphone() as source:
|
24 |
+
speak("Please say your username and password.")
|
25 |
+
print("Listening...")
|
26 |
+
recognizer.adjust_for_ambient_noise(source)
|
27 |
+
try:
|
28 |
+
audio = recognizer.listen(source)
|
29 |
+
text = recognizer.recognize_google(audio)
|
30 |
+
print(f"Recognized: {text}")
|
31 |
+
return text.lower()
|
32 |
+
except sr.UnknownValueError:
|
33 |
+
speak("Sorry, I didn't catch that. Please try again.")
|
34 |
+
return None
|
35 |
+
except sr.RequestError:
|
36 |
+
speak("Speech service is unavailable. Please check your internet connection.")
|
37 |
+
return None
|
38 |
+
|
39 |
+
def authenticate():
|
40 |
+
"""Authenticate user based on voice input."""
|
41 |
+
speech_text = recognize_speech()
|
42 |
+
if not speech_text:
|
43 |
+
return False
|
44 |
+
|
45 |
+
# Extract username and password
|
46 |
+
for username, password in USER_CREDENTIALS.items():
|
47 |
+
if username in speech_text and password in speech_text:
|
48 |
+
speak(f"Welcome, {username}. You are now logged in.")
|
49 |
+
return True
|
50 |
+
|
51 |
+
speak("Authentication failed. Please try again.")
|
52 |
+
return False
|
53 |
+
|
54 |
+
if __name__ == "__main__":
|
55 |
+
speak("Welcome to the voice login system.")
|
56 |
+
if authenticate():
|
57 |
+
print("Login Successful!")
|
58 |
+
else:
|
59 |
+
print("Login Failed!")
|