minimaxir / gpt-2-simple

Python package to easily retrain OpenAI's GPT-2 text-generating model on new texts
Other
3.4k stars 675 forks source link

Response generated in console, but can't retrieve it? #306

Open sneezeparty opened 1 year ago

sneezeparty commented 1 year ago

I'm using the below code to try and set up a simple Discord bot. The bot runs, appears in the correct channel, and so on. When I @tag the bot, it seems to generate a response. In the console window, a response is visible that seems to have been generated by my finetuned model. My dataset is formatted correctly, I think. And again, the confusing part of this is that a response is displayed in the console where I've launched the bot.

However, I get the error "Sorry, I was unable to generate a response" which, according to the code, means that gpt2.generate has returned "None."

Any help would be greatly appreciated. I'm not really a developer, I'm just doing this for fun.

import discord
import gpt_2_simple as gpt2

# Create a TensorFlow session and load the fine-tuned GPT-2 model
sess = gpt2.start_tf_sess()
gpt2.load_gpt2(sess=sess, model_name="D:\git\GPT2\gpt-2-simple\checkpoint\discord-short")

# Replace YOUR_BOT_TOKEN_HERE with your Discord bot token
client = discord.Client(intents=discord.Intents.all(), token='MYKEY')

# This event handler will be called whenever a message is sent in a server that the bot is a member of
@client.event
async def on_message(message):
    # Only generate a response if the bot is @mentioned
    if message.mention_everyone or client.user in message.mentions:
        # Retrieve the last 10 messages from the chat
        history = []
        async for m in message.channel.history(limit=10):
            history.append(m)
        # Reverse the list of messages so that the first message is the oldest
        history = list(reversed(history))
        # Use the messages as context when generating the response
        context = "\n".join([m.content for m in history])
        response = gpt2.generate(sess,
                                 run_name='discord-short',
                                 temperature=0.8,
                                 nsamples=1,
                                 batch_size=1,
                                 top_k=20,
                                 top_p=0.5,
                                 prefix="<|startoftext|>",
                                 truncate="<|endoftext|>",
                                 length=50,
                                 include_prefix=False,
                                 return_as_list=False
                                 )
    # Check if the response is None
        if response is not None:
            # send the response
            await message.channel.send(response)
        else:
            # handle the "none" error
            await message.channel.send("Sorry, I was unable to generate a response.")

# Start the bot
client.run('MYKEY')
Chownie commented 1 year ago

return_as_list=False

I think you want this to be true, else generate doesn't return anything.

sneezeparty commented 1 year ago

return_as_list=False

I think you want this to be true, else generate doesn't return anything.

This fixed the issue. I can't believe myself. Thanks so much.