title: Basic Voice Assistance with Python
body: <p>I am a newbie in Python and I am developing a basic Voice Assistance with my teachers on Raspberry Pi 3. The code in this project is collected and fixed by me. However, as a newbie there are many questions I cant answer. Can you help me, please?
  I want to develop an assistance which can search on Google, Play music on YouTube and show out the weather forecast. The AI.py is my main file, Google_Search.py is used for searching and YouTube_Search.py is used for playing music.
  When I ask for searching the AI.py will import the Google_Search.py file to search for my requests. It is similar with YouTube file.</p>
  <p>The problem is the Google_Search.py and YouTube_search.py just keep running and don't stop or allow me to keep working with the AI.py file. I want to run these two files while I still can run with the AI.py file to stop or re-open them again. These are the code, Please help me out. Thanks so much.</p>
  <p><strong>This is AI.py file</strong></p>
  <pre><code>import speech_recognition
  import pyaudio
  import pyttsx3
  import time
  import os
  import webbrowser, sys, imp
  import requests, json
  import vlc
  import pafy
  from datetime import date
  from datetime import datetime
  from googlesearch import search
  
  robot_ear = speech_recognition.Recognizer()
  robot_mouth = pyttsx3.init()
  robot_brain = &quot;&quot;
  
  
  api_key = &quot;82328b3addcd3f984da6c1e74cf4c7c9&quot;
  base_url = &quot;http://api.openweathermap.org/data/2.5/weather?&quot;
  city_name = &quot;Melbourne,au&quot;
  complete_url = base_url + &quot;appid=&quot; + api_key + &quot;&amp;q=&quot; + city_name
  response = requests.get(complete_url)
  x = response.json() 
  
  while True:
  
      with speech_recognition.Microphone() as mic:
          print(&quot;Robot: I'm Listening&quot;)
          audio = robot_ear.adjust_for_ambient_noise(mic)
          audio = robot_ear.listen(mic, phrase_time_limit=3)
          
      print(&quot;Robot: ...&quot;)
  
      try:
          you = robot_ear.recognize_google(audio, language = &quot;en&quot;)
      except:
          you = &quot;&quot;
          
      print(&quot;You: &quot; + you)
      
  
      if you == &quot;&quot;:
          robot_brain = &quot;I can't hear you, please try again&quot;
      elif &quot;hello&quot; in you:
          robot_brain = &quot;Hello Minh&quot;
      
      elif &quot;play some music&quot; in you:
          import YouTube_Search
          robot_brain = &quot;Music is playing&quot;
      elif &quot;stop music&quot; in you:
          os.system(&quot;pkill YouTube_Search&quot;)
          robot_brain = &quot;Music is stoped&quot;
  
      elif &quot;search something&quot; in you:
          robot_brain = &quot;What do you want me to search for?&quot;
          import Google_Search
          imp.reload(Google_Search)
          time.sleep(5)
      elif &quot;stop searching&quot; in you:
          os.system(&quot;pkill chromium&quot;)
          robot_brain = &quot;Google search is closed&quot;
      
      elif &quot;weather today&quot; in you:
          if x[&quot;cod&quot;] != &quot;404&quot;:
              y = x[&quot;main&quot;]
              current_pressure = y[&quot;pressure&quot;]
              current_humidiy = y[&quot;humidity&quot;]
              z = x[&quot;weather&quot;]
              weather_description = z[0][&quot;description&quot;]
              robot_brain = (&quot; Temperature: &quot; +
                      str(current_temperature) + &quot; °F&quot; + 
            &quot;\n atmospheric pressure: &quot; +
                      str(current_pressure) + &quot; hPa&quot; +
            &quot;\n humidity: &quot; +
                      str(current_humidiy) + &quot; %&quot; +
            &quot;\n weather today: &quot; +
                      str(weather_description))
      
      elif &quot;bye&quot; in you:
          robot_brain = &quot;Bye Minh&quot;
          print(&quot;Robot: &quot; + robot_brain)
          robot_mouth.say(robot_brain)
          robot_mouth.runAndWait()
          break
      else:
          robot_brain = &quot;I'm learning&quot;
  
      print(&quot;Robot: &quot; + robot_brain)
      robot_mouth.say(robot_brain)
      robot_mouth.runAndWait()
  </code></pre>
  <p><strong>This is the Google_Search.py file</strong></p>
  <pre><code>import speech_recognition
  import os, sys
  import webbrowser
  robot_brain = &quot;&quot;
  robot_ear = speech_recognition.Recognizer()
  with speech_recognition.Microphone() as mic:
      #print(&quot;Robot: What do you want me to search for?&quot;)
      audio = robot_ear.adjust_for_ambient_noise(mic)
      audio = robot_ear.listen(mic, phrase_time_limit=2)
  
  try:
      you = robot_ear.recognize_google(audio, language = 'en-US')
  except:
      you = &quot;&quot;
       
  search_terms = [you]
              
              # ... construct your list of search terms ...
              
  for term in search_terms:
      url = &quot;https://www.google.com/search?q={}&quot;.format(term)
      webbrowser.open_new_tab(url)
      robot_brain = (&quot;Here is what I found for&quot;) + (&quot; &quot;) + str(you)
          
  print(&quot;Robot: &quot; + robot_brain)
  
  os.system(&quot;pkill Google_Search&quot;)
  </code></pre>
  <p><strong>This is the YouTube_Search.py</strong></p>
  <pre><code>import vlc
  import pafy
  import time
  import urllib.request
  import urllib.parse
  import re
  import speech_recognition
  
  
  robot_ear = speech_recognition.Recognizer()
  with speech_recognition.Microphone() as mic:
      print(&quot;Robot: What do you want me to search for?&quot;)
      audio = robot_ear.adjust_for_ambient_noise(mic)
      audio = robot_ear.listen(mic, phrase_time_limit=2)
  
  try:
      you = robot_ear.recognize_google(audio, language = 'en-US')
  except:
      you = &quot;&quot;
  
  search = you
  query_string = urllib.parse.urlencode({&quot;search_query&quot; : search})
  html_content = urllib.request.urlopen(&quot;http://www.youtube.com/results?search_query=&quot;+query_string)
  search_results = re.findall(r&quot;watch\?v=(\S{11})&quot;, html_content.read().decode())
  #print(&quot;http://www.youtube.com/watch?v=&quot; + search_results[0])
  url = &quot;http://www.youtube.com/watch?v=&quot; + search_results[0]
  
  video = pafy.new(url)
  best = video.getbest()
  playurl = best.url
  
  Instance = vlc.Instance()
  player = Instance.media_player_new()
  Media = Instance.media_new(playurl)
  Media.get_mrl()
  player.set_media(Media)
  player.play()
  </code></pre>