created app.py file
Browse files
app.py
ADDED
@@ -0,0 +1,153 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import pywhatkit
|
3 |
+
import speech_recognition as sr
|
4 |
+
import pyttsx3
|
5 |
+
import os
|
6 |
+
import webbrowser
|
7 |
+
import datetime
|
8 |
+
import wikipedia
|
9 |
+
|
10 |
+
st.markdown(
|
11 |
+
"""
|
12 |
+
<style>
|
13 |
+
.centered {
|
14 |
+
display: flex;
|
15 |
+
flex-direction: column;
|
16 |
+
align-items: center;
|
17 |
+
justify-content: center;
|
18 |
+
height: 90vh;
|
19 |
+
}
|
20 |
+
.btn-primary {
|
21 |
+
background-color: #FF5722;
|
22 |
+
color: white;
|
23 |
+
font-weight: bold;
|
24 |
+
padding: 10px 10px;
|
25 |
+
border-radius: 5px;
|
26 |
+
border: none;
|
27 |
+
cursor: pointer;
|
28 |
+
}
|
29 |
+
.title {
|
30 |
+
text-align: center;
|
31 |
+
color: #FF5722;
|
32 |
+
font-size: 500px;
|
33 |
+
margin-bottom: 40px;
|
34 |
+
}
|
35 |
+
.footer {
|
36 |
+
text-align: center;
|
37 |
+
margin-top: 50px;
|
38 |
+
color: #888888;
|
39 |
+
}
|
40 |
+
</style>
|
41 |
+
""",
|
42 |
+
unsafe_allow_html=True
|
43 |
+
)
|
44 |
+
|
45 |
+
|
46 |
+
# ask the question you want to ask
|
47 |
+
def speak(text):
|
48 |
+
engine = pyttsx3.init()
|
49 |
+
engine.setProperty("rate", 150) # You can adjust the speech rate (words per minute)
|
50 |
+
engine.say(text)
|
51 |
+
engine.runAndWait()
|
52 |
+
|
53 |
+
|
54 |
+
# your speech is an audio
|
55 |
+
def get_audio():
|
56 |
+
recognizer = sr.Recognizer()
|
57 |
+
with sr.Microphone() as source:
|
58 |
+
st.info("Listening... Say something!")
|
59 |
+
audio = recognizer.listen(source)
|
60 |
+
try:
|
61 |
+
text = recognizer.recognize_google(audio)
|
62 |
+
st.write(f"You said: {text}")
|
63 |
+
speak("You said " + text)
|
64 |
+
# if you tell shutdown the system
|
65 |
+
if text == "shutdown":
|
66 |
+
shutdown_PC()
|
67 |
+
return text
|
68 |
+
except sr.UnknownValueError:
|
69 |
+
st.warning("Sorry, I couldn't understand your speech. Please try again.")
|
70 |
+
return ""
|
71 |
+
except sr.RequestError:
|
72 |
+
st.error("Sorry, there was an issue with the speech recognition service. Please try again later.")
|
73 |
+
return ""
|
74 |
+
|
75 |
+
|
76 |
+
# user-defined function to shutdown the system
|
77 |
+
def shutdown_PC():
|
78 |
+
os.system("shutdown /s /t 1")
|
79 |
+
|
80 |
+
|
81 |
+
# searches the text and displays the result
|
82 |
+
def search_and_display(text):
|
83 |
+
pywhatkit.search(text)
|
84 |
+
st.write(f"Searching for : {text} ...")
|
85 |
+
speak("Searching for " + text)
|
86 |
+
|
87 |
+
|
88 |
+
# opens the provided URL in a web browser
|
89 |
+
def open_website(url):
|
90 |
+
webbrowser.open(url)
|
91 |
+
st.write(f"Opening website: {url}")
|
92 |
+
speak("Opening website")
|
93 |
+
|
94 |
+
|
95 |
+
# gets the current date and time
|
96 |
+
def get_date_time():
|
97 |
+
now = datetime.datetime.now()
|
98 |
+
date = now.strftime("%A, %d %B %Y")
|
99 |
+
time = now.strftime("%I:%M %p")
|
100 |
+
st.write(f"Today is {date}")
|
101 |
+
st.write(f"The current time is {time}")
|
102 |
+
speak(f"Today is {date} and the current time is {time}")
|
103 |
+
|
104 |
+
|
105 |
+
# searches Wikipedia for the provided query and displays the summary
|
106 |
+
def search_wikipedia(query):
|
107 |
+
try:
|
108 |
+
result = wikipedia.summary(query, sentences=2)
|
109 |
+
st.write(f"Wikipedia Summary for '{query}':")
|
110 |
+
st.write(result)
|
111 |
+
speak(f"According to Wikipedia, {result}")
|
112 |
+
except wikipedia.exceptions.PageError:
|
113 |
+
st.error("Sorry, no Wikipedia page found for the provided query.")
|
114 |
+
except wikipedia.exceptions.DisambiguationError:
|
115 |
+
st.error("Multiple Wikipedia pages found for the provided query. Please be more specific.")
|
116 |
+
|
117 |
+
|
118 |
+
# Streamlit app
|
119 |
+
st.title("Personal VoiceAssistant using Python")
|
120 |
+
|
121 |
+
col1, col2, col3 = st.columns(3)
|
122 |
+
|
123 |
+
with col2:
|
124 |
+
if st.button("Start Listening", key="start"):
|
125 |
+
text = get_audio()
|
126 |
+
if text:
|
127 |
+
search_and_display(text)
|
128 |
+
|
129 |
+
with st.expander("Additional Features"):
|
130 |
+
selected_feature = st.selectbox("Select a feature", ("Open Website", "Get Date and Time", "Search Wikipedia"))
|
131 |
+
if selected_feature == "Open Website":
|
132 |
+
wesite_url = st.text_input("Enter the website URL")
|
133 |
+
if st.button("Open Website"):
|
134 |
+
open_website(wesite_url)
|
135 |
+
elif selected_feature == "Get Date and Time":
|
136 |
+
if st.button("Get Date and Time"):
|
137 |
+
get_date_time()
|
138 |
+
elif selected_feature == "Search Wikipedia":
|
139 |
+
wikipedia_query = st.text_input("Enter your Wikipedia search query")
|
140 |
+
if st.button("Search Wikipedia"):
|
141 |
+
search_wikipedia(wikipedia_query)
|
142 |
+
st.markdown("---")
|
143 |
+
|
144 |
+
# Footer
|
145 |
+
st.markdown(
|
146 |
+
"""
|
147 |
+
<div class="footer">
|
148 |
+
Made with ❤️ by Anirudh
|
149 |
+
</div>
|
150 |
+
""",
|
151 |
+
unsafe_allow_html=True
|
152 |
+
)
|
153 |
+
|