-
-
Save BruceCodesGithub/22dc4c30c1a2b19781dfcb79fc98972f to your computer and use it in GitHub Desktop.
AI Chatbot with microphone input
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
API_KEY=[YOUR RANDOM STUFF API KEY HERE! https://api-info.pgamerx.com/register] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import asyncio # default, used here to run async functions | |
import os # default | |
from difflib import SequenceMatcher # default | |
import click # pip install click , used for command line interface, optional | |
import dotenv # pip install python-dotenv, to open env files | |
import pyttsx3 # pip install pyttsx3 | |
import speech_recognition as sr # pip install speech_recognition | |
import yarsaw # pip install yarsaw, for chatbot | |
def similar(a, b): # to check how similar two strings are | |
return SequenceMatcher(None, a, b).ratio() # 1, 0.7, 0.8, etc | |
dotenv.load_dotenv() # load all the values in env | |
re = sr.Recognizer() | |
engine = pyttsx3.init() | |
class AI(yarsaw.Client): # add these two methods here, optional, but looks good | |
def listen(self): | |
with sr.Microphone() as source: | |
audio = re.listen(source) | |
try: | |
text = re.recognize_google(audio) | |
return text | |
except: | |
return False | |
def say(self, text, system=False): # different voice if its a "system" message | |
if system: | |
engine.setProperty("voice", voices[0].id) | |
print("\n", text, "\n") | |
else: | |
print("AI: ", text) | |
engine.say(text) | |
engine.setProperty("voice", voices[1].id) | |
engine.runAndWait() | |
ai = AI(os.getenv("API_KEY")) # get an API KEY from https://api-info.pgamerx.com/register - used for chatbot | |
voices = engine.getProperty("voices") | |
engine.setProperty("voice", voices[1].id) | |
async def main(): | |
bye = ["goodbye", "bye", "see you later", "see ya", "goodbye"] # if the user says something like this, exit | |
continue_talking = True | |
in_method = "speech" | |
warnings = True | |
while continue_talking: | |
if in_method == "speech": | |
print("Listening...") | |
text = ai.listen() | |
else: | |
text = input(">> ") | |
if text is False: | |
ai.say("Sorry, I didn't catch that.", system=True) | |
if warnings: | |
click.echo( | |
"Switch to text input? [ynd] (y = yes, n = no, d = don't show this again)\n>>", | |
nl=False, | |
) | |
c = click.getchar() | |
click.echo() | |
if c == "y": | |
in_method = "text" | |
ai.say("Switched to text input.", system=True) | |
elif c == "d": | |
warnings = False | |
else: | |
pass | |
elif similar(text, "switch input method") > 0.7: | |
in_method = "text" | |
print("Switched to text input.") | |
ai.say("Switched to text input.") | |
elif similar(text, "tell me a joke") > 0.7: | |
joke = await ai.get_safe_joke() | |
joke = await yarsaw.Utils().format_joke(joke) | |
ai.say(joke) | |
elif text.lower() == "pause": | |
ai.say("Paused conversation. Press enter to continue.", system=True) | |
p = input() | |
else: # else, get a cool API response | |
response = await ai.get_ai_response(text) | |
if in_method == "speech": | |
print(f"You: {text}") | |
ai.say(response.response) | |
continue_talking = text.lower() not in bye | |
asyncio.get_event_loop().run_until_complete(main()) # run it |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment