Monday 22 January 2024

A simple Turkish speaking chatbot example (and working)


 

Not: Bazı şirketler telefonla arayan müşterilere otomatik olarak ne istediklerini sorup ona göre yönlendiriyorlar. Aslında bir kaç saatlik basit bir iş. İnternetteki bilgileri kullanarak kolayca yaptım bunu.

# -*- coding: utf-8 -*-

"""

Created on Fri Jan 19 15:43:41 2024

pip install speechRecognition

pip install gTTS

pip install pyaudio

@author: ars

"""

 

import speech_recognition as sr

from gtts import gTTS

import os

import datetime

import numpy as np

import time

 

# Beginning of the AI

class ChatBot():

    def __init__(self, name):

        print("----- starting up", name, "-----")

        self.name = name

        self.text = ""  # Initialize text attribute

       

    def speech_to_text(self):

        recognizer = sr.Recognizer()

        with sr.Microphone() as mic:

             print("dinliyorum...")

             audio = recognizer.listen(mic)

        try:

             self.text = recognizer.recognize_google(audio, language="tr-TR")

             print("me --> ", self.text)

             if "dur" in self.text.lower(): return False

        except:

             print("me -->  ERROR")

   

    def wake_up(self, text):

        return True if self.name in text.lower() else False

 

    @staticmethod

    def text_to_speech(text):

        print("AI --> ", text)

        speaker = gTTS(text=text, lang="tr", slow=False)

        speaker.save("res.mp3")

        mp3_path = os.path.join(os.getcwd(), "res.mp3")

        print(mp3_path)

        statbuf = os.stat("res.mp3")

        mbytes = statbuf.st_size / 1024

        duration = mbytes / 200

        os.system("start res.mp3")  #if you have a macbook->afplay or for windows use->start

        #os.remove("res.mp3")

        time.sleep(int(50*duration))

        os.remove("res.mp3")

       

    @staticmethod

    def action_time():

        return datetime.datetime.now().time().strftime('%H:%M')

    #and run the script after adding the above function to the AI class

 

# Execute the AI

if __name__ == "__main__":

    ai = ChatBot(name="başla")       

 

# Execute the AI

if __name__ == "__main__":

     ai = ChatBot(name="başla")

     while True:

         if ai.speech_to_text() == False:

             break

         # Default response

         res = "Anlayamadım. Tekrarlar mısınız?"

 

        ## wake up

         if ai.wake_up(ai.text) is True:

             res = "Merhaba ben suni zeka, sizin için ne yapabilirim?"

             ai.text = "xxx"

         ## do any action

         elif "saat" in ai.text:

            res = ai.action_time()

         ## respond politely

         elif any(i in ai.text for i in ["teşekkür","sağolun"]):

            res = np.random.choice(

                  ["estağfurullah!","her zaman!",

                   "sorun değil!","güzel!",

                   "İhtiyacınız olursa ben buradayım!","önemli değil!"])

         ai.text_to_speech(res)

         

Sunday 14 January 2024

TRANSFORMER IMPLEMENTATION RESULTS OF A NUMBER SEQUENCER

Results of Number sequencer implementation using transformer algorithm: %success

X random numbers between 1 - 12 with random length between 1 and X. 



Epoch 90/100

55/55 [==============================] - 5s 95ms/step - loss: 0.0621 - masked_accuracy: 0.9817 - val_loss: 0.0733 - val_masked_accuracy: 0.9769

Epoch 91/100

55/55 [==============================] - 5s 100ms/step - loss: 0.0667 - masked_accuracy: 0.9799 - val_loss: 0.0209 - val_masked_accuracy: 0.9923

Epoch 92/100

55/55 [==============================] - 6s 118ms/step - loss: 0.0817 - masked_accuracy: 0.9763 - val_loss: 0.0309 - val_masked_accuracy: 0.9908

Epoch 93/100

55/55 [==============================] - 5s 96ms/step - loss: 0.0706 - masked_accuracy: 0.9782 - val_loss: 0.0502 - val_masked_accuracy: 0.9843

Epoch 94/100

55/55 [==============================] - 5s 98ms/step - loss: 0.0688 - masked_accuracy: 0.9805 - val_loss: 0.0436 - val_masked_accuracy: 0.9868

Epoch 95/100

55/55 [==============================] - 6s 107ms/step - loss: 0.0840 - masked_accuracy: 0.9746 - val_loss: 0.0388 - val_masked_accuracy: 0.9881

Epoch 96/100

55/55 [==============================] - 6s 102ms/step - loss: 0.0503 - masked_accuracy: 0.9853 - val_loss: 0.0591 - val_masked_accuracy: 0.9803

Epoch 97/100

55/55 [==============================] - 6s 110ms/step - loss: 0.0399 - masked_accuracy: 0.9875 - val_loss: 0.0563 - val_masked_accuracy: 0.9826

Epoch 98/100

55/55 [==============================] - 5s 98ms/step - loss: 0.0865 - masked_accuracy: 0.9774 - val_loss: 0.0444 - val_masked_accuracy: 0.9858

Epoch 99/100

55/55 [==============================] - 6s 115ms/step - loss: 0.1183 - masked_accuracy: 0.9689 - val_loss: 0.0819 - val_masked_accuracy: 0.9749

Epoch 100/100

55/55 [==============================] - 5s 96ms/step - loss: 0.0709 - masked_accuracy: 0.9809 - val_loss: 0.0347 - val_masked_accuracy: 0.9905

Test 0:

3 4

== [start] 3 4 [end]

-> [start] 3 4 [end]


Test 1:

2 7 2 4 5 12 1

== [start] 1 2 2 4 5 7 12 [end]

-> [start] 1 2 2 4 5 7 12 [end]


Test 2:

1 3 1 9 3 6

== [start] 1 1 3 3 6 9 [end]

-> [start] 1 1 3 3 6 9 [end]


Test 3:

1

== [start] 1 [end]

-> [start] 1 [end]


Test 4:

12 8 11 8 3 2

== [start] 2 3 8 8 11 12 [end]

-> [start] 2 3 8 8 11 12 [end]


Test 5:

8 3 1

== [start] 1 3 8 [end]

-> [start] 1 3 8 [end]


Test 6:

8 9 4 7 5 4 2 7

== [start] 2 4 4 5 7 7 8 9 [end]

-> [start] 2 4 4 5 7 7 8 9


Test 7:

5

== [start] 5 [end]

-> [start] 5 [end]


Test 8:

2 8 4

== [start] 2 4 8 [end]

-> [start] 2 4 8 [end]


Test 9:

3

== [start] 3 [end]

-> [start] 3 [end]


Test 10:

7 8 11 1

== [start] 1 7 8 11 [end]

-> [start] 1 7 8 11 [end]


Test 11:

6 4

== [start] 4 6 [end]

-> [start] 4 6 [end]


Test 12:

3 12 3 3 3 8 10

== [start] 3 3 3 3 8 10 12 [end]

-> [start] 3 3 3 3 8 10 12 [end]


Test 13:

1 4 4 11 6 7 4 5

== [start] 1 4 4 4 5 6 7 11 [end]

-> [start] 1 4 4 5 5 6 7 11


Test 14:

1 3 8

== [start] 1 3 8 [end]

-> [start] 1 3 8 [end]


Test 15:

4 9 7 3 2

== [start] 2 3 4 7 9 [end]

-> [start] 2 3 4 7 9 [end]


Test 16:

5 2 11

== [start] 2 5 11 [end]

-> [start] 2 5 11 [end]


Test 17:

4 3 9

== [start] 3 4 9 [end]

-> [start] 3 4 9 [end]


Test 18:

3 2

== [start] 2 3 [end]

-> [start] 2 3 [end]


Test 19:

10 9

== [start] 9 10 [end]

-> [start] 9 10 [end]