-
Notifications
You must be signed in to change notification settings - Fork 8
/
Voice_Asistant.py
203 lines (175 loc) · 5.87 KB
/
Voice_Asistant.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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
# !pip install pyttsx3
# !pip install SpeechRecognition
# !pip install pyaudio
# !pip install pywhatkit
# !pip install yfinance
# !pip install pyjokes
# !pip install git+https://github.com/openai/whisper.git
# !pip install gradio
import pyttsx3
import speech_recognition as sr
import pyaudio
import webbrowser
import datetime
import pywhatkit
import os
import yfinance as yf
import pyjokes
from youtubesearchpython import VideosSearch
import pyaudio
import wave
import whisper
import gradio as gr
import time
import warnings
import torch
DEVICE = "cuda" if torch.cuda.is_available() else "cpu"
model = whisper.load_model("medium.en", device=device)
def transform():
#speaking('Talk')
r = sr.Recognizer()
with sr.Microphone() as source:
r.pause_threshold = 0.5
said = r.listen(source)
try:
q = r.recognize_google(said, language="en")
return q
except sr.UnknownValueError:
print('Sorry I did not understand')
return "I am waiting"
except sr.RequestError:
print('Sorry the service is down')
return "I am waiting"
except:
return "I am waiting"
def speaking(message):
engine = pyttsx3.init()
engine.say(message)
engine.runAndWait()
def whisper_ai(model,device):
audio = whisper.load_audio('voice.wav')
audio = whisper.pad_or_trim(audio)
mel = whisper.log_mel_spectrogram(audio).to(model.device)
_, probs = model.detect_language(mel)
options = whisper.DecodingOptions(language="en", without_timestamps=True, fp16 = False)
result = whisper.decode(model, mel, options)
result = model.transcribe("voice.wav")
return result['text']
def query_day():
day = datetime.date.today()
# print(day)
weekday = day.weekday()
# print(weekday)
mapping = {
0:'Monday',1:'Tuesday',2:'Wednesday',3:'Thursday',4:'Friday',5:'Saturday',6:'Sunday'
}
try:
speaking(f'Today is {mapping[weekday]}')
except:
pass
def query_time():
time = datetime.datetime.now().strftime("%H:%M:%S")
speaking(f"It is {time[1]} o'clock and {time[3:5]} minutes")
def whatsup():
speaking("""Hi, my name is James. How can i help you""")
def Record_audio():
CHUNK = 1024
FORMAT = pyaudio.paInt16
CHANNELS = 1
RATE = 44100
RECORD_SECONDS = 8
WAVE_OUTPUT_FILENAME = "voice.wav"
p = pyaudio.PyAudio()
stream = p.open(format=FORMAT,
channels=CHANNELS,
rate=RATE,
input=True,
frames_per_buffer=CHUNK)
print("* recording")
frames = []
for i in range(0, int(RATE / CHUNK * RECORD_SECONDS)):
data = stream.read(CHUNK)
frames.append(data)
print("* done recording")
stream.stop_stream()
stream.close()
p.terminate()
wf = wave.open(WAVE_OUTPUT_FILENAME, 'wb')
wf.setnchannels(CHANNELS)
wf.setsampwidth(p.get_sample_size(FORMAT))
wf.setframerate(RATE)
wf.writeframes(b''.join(frames))
wf.close()
def querying():
whatsup()
start = True
while(start):
s = transform().lower()
if "james"in s:
Record_audio()
q = whisper_ai(model,DEVICE)
print(q)
if "start youtube" in q:
speaking("Starting youtube. Just a second")
webbrowser.open("https://www.youtube.com")
continue
elif "start webbrowser" in q:
speaking("opening browser")
webbrowser.open("https://www.google.com")
continue
elif "what day is it" in q:
query_day()
continue
elif "what time is it" in q:
query_time()
continue
elif "shut down" in q or 'shutdown' in q:
speaking("ok I am shuting down")
break
elif "from wikipedia" in q:
speaking("checking wikipedia")
q = q.replace("wikipedia", "")
result = wikipedia.summary(q,sentences=2)
speaking("found on wikipedia")
speaking(result)
continue
elif "your name" in q:
speaking("I am David. Your Voice Asistant")
continue
elif "search web" in q:
q = q.replace("search web","")
pywhatkit.search(q)
speaking("this is what i found")
continue
elif "play" in q:
q = q.replace("play","")
speaking(f"playing {q}")
pywhatkit.playonyt(q)
continue
elif "joke" in q:
speaking(pyjokes.get_joke())
continue
elif "stock price" in q:
search = q.split("of")[-1].strip()
lookup = {"apple":"AAPL",
"amazon":"AMZN",
"google":"GOOGL"}
try:
stock = lookup[search]
stock = yf.Ticker(stock)
currentprice = stock.info["regularMarketPrice"]
speaking(f"found it, the price for {search} is {currentprice}")
continue
except:
speaking(f"sorry I have no data for {search}")
continue
elif 'play on youtube' in q:
search = q.split('youtube')[-1].strip()
videoSearch = VideosSearch(search, limit=1)
videoSearch = VideosSearch.result()
video_link = videosSearch['result'][0]['link']
video_name = videosSearch['result'][0]['title']
print(f'Playing {video_name}')
webbrowser.open(video_link)
continue
querying()