File size: 4,174 Bytes
c4dbe7e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import streamlit as st
import pywhatkit
import speech_recognition as sr
import pyttsx3
import os
import webbrowser
import datetime
import wikipedia

st.markdown(
    """
    <style>
    .centered {
        display: flex;
        flex-direction: column;
        align-items: center;
        justify-content: center;
        height: 90vh;
    }
    .btn-primary {
        background-color: #FF5722;
        color: white;
        font-weight: bold;
        padding: 10px 10px;
        border-radius: 5px;
        border: none;
        cursor: pointer;
    }
    .title {
        text-align: center;
        color: #FF5722;
        font-size: 500px;
        margin-bottom: 40px;
    }
    .footer {
        text-align: center;
        margin-top: 50px;
        color: #888888;
    }
    </style>
    """,
    unsafe_allow_html=True
)


# ask the question you want to ask
def speak(text):
    engine = pyttsx3.init()
    engine.setProperty("rate", 150)  # You can adjust the speech rate (words per minute)
    engine.say(text)
    engine.runAndWait()


# your speech is an audio
def get_audio():
    recognizer = sr.Recognizer()
    with sr.Microphone() as source:
        st.info("Listening... Say something!")
        audio = recognizer.listen(source)
    try:
        text = recognizer.recognize_google(audio)
        st.write(f"You said: {text}")
        speak("You said " + text)
        # if you tell shutdown the system
        if text == "shutdown":
            shutdown_PC()
        return text
    except sr.UnknownValueError:
        st.warning("Sorry, I couldn't understand your speech. Please try again.")
        return ""
    except sr.RequestError:
        st.error("Sorry, there was an issue with the speech recognition service. Please try again later.")
        return ""


# user-defined function to shutdown the system
def shutdown_PC():
    os.system("shutdown /s /t 1")


# searches the text and displays the result
def search_and_display(text):
    pywhatkit.search(text)
    st.write(f"Searching for : {text} ...")
    speak("Searching for " + text)


# opens the provided URL in a web browser
def open_website(url):
    webbrowser.open(url)
    st.write(f"Opening website: {url}")
    speak("Opening website")


# gets the current date and time
def get_date_time():
    now = datetime.datetime.now()
    date = now.strftime("%A, %d %B %Y")
    time = now.strftime("%I:%M %p")
    st.write(f"Today is {date}")
    st.write(f"The current time is {time}")
    speak(f"Today is {date} and the current time is {time}")


# searches Wikipedia for the provided query and displays the summary
def search_wikipedia(query):
    try:
        result = wikipedia.summary(query, sentences=2)
        st.write(f"Wikipedia Summary for '{query}':")
        st.write(result)
        speak(f"According to Wikipedia, {result}")
    except wikipedia.exceptions.PageError:
        st.error("Sorry, no Wikipedia page found for the provided query.")
    except wikipedia.exceptions.DisambiguationError:
        st.error("Multiple Wikipedia pages found for the provided query. Please be more specific.")


# Streamlit app
st.title("Personal VoiceAssistant using Python")

col1, col2, col3 = st.columns(3)

with col2:
    if st.button("Start Listening", key="start"):
        text = get_audio()
        if text:
            search_and_display(text)

with st.expander("Additional Features"):
    selected_feature = st.selectbox("Select a feature", ("Open Website", "Get Date and Time", "Search Wikipedia"))
    if selected_feature == "Open Website":
        wesite_url = st.text_input("Enter the website URL")
        if st.button("Open Website"):
            open_website(wesite_url)
    elif selected_feature == "Get Date and Time":
        if st.button("Get Date and Time"):
            get_date_time()
    elif selected_feature == "Search Wikipedia":
        wikipedia_query = st.text_input("Enter your Wikipedia search query")
        if st.button("Search Wikipedia"):
            search_wikipedia(wikipedia_query)
st.markdown("---")

# Footer
st.markdown(
    """
    <div class="footer">
        Made with ❤️ by Anirudh
    </div>
    """,
    unsafe_allow_html=True
)