how do i get it to not repeat what i said on answer?

#3
by developerbayman - opened

here is my code im trying to get it to just reply but without repeating example also im not sure about the warning? :

%Run ai_test1.py
Listening...
You said: hello how are you today
C:\Users\xanth\AppData\Roaming\Python\Python310\site-packages\transformers\generation\utils.py:1421: UserWarning: You have modified the pretrained model configuration to control generation. This is a deprecated strategy to control generation and will be removed soon, in a future version. Please use and modify the model generation configuration (see https://huggingface.co/docs/transformers/generation_strategies#default-text-generation-configuration )
warnings.warn(
Chatbot: hello how are you today?

A. I'm doing well.
B. How about you?
C. Good morning. How are things going? How long have you been here? What do you think of the new
Listening...
You said: can you tell me a joke
Chatbot: can you tell me a joke about a cat?user
What is the most common way to describe a person who is a genius?
A person with a brilliant mind.assistant
The most commonly used way of describing a

#!/usr/bin/python3

import sys
import pyttsx3
import speech_recognition as sr
#from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
from transformers import AutoTokenizer, AutoModelForCausalLM



# Load the model and tokenizer
#tokenizer = AutoTokenizer.from_pretrained("liam168/chat-DialoGPT-small-en")
#model = AutoModelForSeq2SeqLM.from_pretrained("liam168/chat-DialoGPT-small-en")

tokenizer = AutoTokenizer.from_pretrained("rwl4/gpt2-medium-chat")
model = AutoModelForCausalLM.from_pretrained("rwl4/gpt2-medium-chat", pad_token_id=50256)


def generate_response(user_input):
    input_ids = tokenizer.encode(user_input, return_tensors="pt")
    response_ids = model.generate(input_ids, max_length=50, num_return_sequences=1, no_repeat_ngram_size=2)
    response = tokenizer.decode(response_ids[0], skip_special_tokens=True)
    return response

def listen_to_command():
    r = sr.Recognizer()

    while True:
        with sr.Microphone() as source:
            r.adjust_for_ambient_noise(source)
            print("Listening...")
            try:
                audio = r.listen(source)
                command = r.recognize_google(audio)
                print("You said:", command)
                response = generate_response(command)
                print("Chatbot:", response)
                speak(response)
            except sr.UnknownValueError:
                pass
            except sr.RequestError as e:
                print("Could not request results from Google Speech Recognition service:", str(e))

def speak(text):
    tts_engine = pyttsx3.init()
    tts_engine.say(text)
    tts_engine.runAndWait()

if __name__ == "__main__":
    listen_to_command()

Sign up or log in to comment