GPT-2 ChatBot

import nltk
from transformers import pipeline
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.linear_model import LogisticRegression
import numpy as np
import spacy

# nltkのセットアップ(初回のみ)
nltk.download('punkt')

# spaCyのセットアップ
nlp = spacy.load("en_core_web_sm")

# サンプルデータ(インテントとそのサンプル文)
training_sentences = [
    "Hello", "Hi", "Hey", "Good morning", "Good evening",
    "How are you?", "What's up?", "How's it going?",
    "Bye", "Goodbye", "See you later", "Take care",
    "Thank you", "Thanks", "I appreciate it",
    "What's your name?", "Who are you?",
    "What can you do?", "Tell me a joke", "Make me laugh",
    "What's the weather like?", "How's the weather?",
    "Book a flight", "I need to book a flight", "Can you book a flight for me?"
]

intents = [
    "greeting", "greeting", "greeting", "greeting", "greeting",
    "how_are_you", "how_are_you", "how_are_you",
    "goodbye", "goodbye", "goodbye", "goodbye",
    "thanks", "thanks", "thanks",
    "name", "name",
    "capabilities", "joke", "joke",
    "weather", "weather",
    "book_flight", "book_flight", "book_flight"
]

# 特徴抽出器と分類器のセットアップ
vectorizer = TfidfVectorizer()
X = vectorizer.fit_transform(training_sentences)
classifier = LogisticRegression()
classifier.fit(X, intents)

# GPT-2 を使用したテキスト生成パイプラインの作成
chatbot = pipeline("text-generation", model="gpt2")

# インテントに基づく応答
responses = {
    "greeting": ["Hello! How can I help you?", "Hi there! What can I do for you?"],
    "how_are_you": ["I'm just a bot, but I'm here to help you!", "I'm fine, thank you! How can I assist you today?"],
    "goodbye": ["Goodbye! Have a great day!", "See you later!"],
    "thanks": ["You're welcome!", "No problem!"],
    "name": ["I am your friendly chatbot.", "I'm an AI created to assist you."],
    "capabilities": ["I can chat with you and help answer your questions!", "I'm here to assist you with various tasks."],
    "joke": ["Why did the scarecrow win an award? Because he was outstanding in his field!"],
    "weather": ["The weather is nice today!", "It's a bit cloudy, but still good."],
    "book_flight": ["Sure, I can help you with that. Where would you like to go?"]
}

# 未知のインテントに対するエラーレスポンス
default_responses = ["I'm not sure I understand. Can you please rephrase?", "Sorry, I don't have an answer for that."]

# インテント認識
def get_intent(user_input):
    X_test = vectorizer.transform([user_input])
    intent = classifier.predict(X_test)[0]
    return intent

# エンティティ認識
def get_entities(user_input):
    doc = nlp(user_input)
    entities = {ent.label_: ent.text for ent in doc.ents}
    return entities

# 応答生成
def get_response(user_input):
    intent = get_intent(user_input)
    entities = get_entities(user_input)
    
    if intent in responses:
        response = np.random.choice(responses[intent])
        if intent == "book_flight" and "GPE" in entities:
            response = f"Sure, I can help you book a flight to {entities['GPE']}. When would you like to travel?"
        return response
    else:
        response = chatbot(user_input, max_length=50, num_return_sequences=1)
        return response[0]['generated_text']

# メイン関数
def main():
    print("Chatbot: Hello! How can I help you today? (Type 'exit' to quit)")
    
    while True:
        user_input = input("You: ")
        if user_input.lower() == 'exit':
            print("Chatbot: Goodbye!")
            break
        response = get_response(user_input)
        print(f"Chatbot: {response}")

if __name__ == "__main__":
    main()

投稿者: chosuke

趣味はゲームやアニメや漫画などです

コメントを残す

メールアドレスが公開されることはありません。 が付いている欄は必須項目です