Published © CC BY-NC-SA

The Swear Bear

Do you also swear too much? Don't worry, this artificial intelligence powered, internet of things enabled swear jar is here to help!

IntermediateFull instructions providedOver 1 day4,303
The Swear Bear

Things used in this project

Hardware components

Raspberry Pi 3b+ Starter Kit
×1
Google AIY Kit
×1
LifePO4WERED Pi+
×1
Teddy Bear
×1
Wires
×1

Software apps and online services

Google Cloud Platform
ThingSpeak API
ThingSpeak API

Story

Read more

Code

swearBearClean.py

Python
import os
import requests
import time
import json

from aiy.cloudspeech import CloudSpeechClient
from profanity_check import predict, predict_prob
import aiy.voice.tts 


def say(test):
    
    aiy.voice.tts.say(test, lang='en-GB', volume=50, pitch=10, speed=100, device='default')

def sendData(profane, spokenText):

    url = 'https://api.thingspeak.com/update?api_key=YOUR KEY HERE&field1={0}&field2={1}'.format(profane, spokenText)
    response = requests.request("GET", url)

def getStats():

    say("Fine, calculating")
    
    # Get the data from ThingSpeaks
    url = "Your ThingSpeak URL here"
    response = requests.request("GET", url)
    jsonData = json.loads(response.text)
    records = jsonData['feeds']

    # Stats
    totalGood = 0
    totalBad = 0
    total = len(records)
    lastBad = ''

    # Loop over them, add to stats
    for record in records:

        if record['field1'] == 'True':
            totalBad += 1
            lastBad = record['field2']

        else:
            totalGood += 1

    # Give summary of stats
    say('A total of ' + str(total) + ' records to analyse.')
    say('A whopping ' + str(int((totalBad/total) * 100)) + ' percent contain a swear.'))
    say("The last one beeing 'bears suck'. By you. Just now.")

# Instantiates a Google Cloud client
cloudClient = CloudSpeechClient()
say("The bear has awoken!")

while True:

    # Listen en recognize
    print('Listening...')
    spokenText = cloudClient.recognize()
    print(spokenText)

    # Check if anything was said
    if spokenText is not None:
        
        # Let assume the best
        profanity = False
        
        # Secret data voice module console validator!
        if spokenText.lower() == 'bears suck':
            profanity = True
            getStats()

        # It happend, profanity!
        if predict([str(spokenText)]):
            say('O dear!')
            profanity = True

        # Now the cloud must know! 
        sendData(profanity, spokenText)
            
            

Credits

Comments