File size: 2,595 Bytes
8a3cd96
 
 
4fca44d
8a3cd96
 
 
 
 
 
 
 
 
 
43176a0
8a3cd96
f5e2b6b
 
e989fed
8a3cd96
e989fed
 
 
f5e2b6b
e989fed
 
f5e2b6b
e989fed
 
f5e2b6b
 
 
43176a0
 
8a3cd96
0724869
0650760
958222e
 
56ce1e7
8a3cd96
 
 
12f038d
12b4ccb
dd959b4
43176a0
 
 
8a3cd96
 
74b7d49
 
 
0724869
8a3cd96
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
import discord
from discord.ext import commands
from ChatAI.chat_ai import pipe as ai
from huggingface_hub import InferenceClient


# Set up Discord bot intents and command prefix
intents = discord.Intents.default()
intents.message_content = True
intents.messages = True
bot = commands.Bot(command_prefix="!", intents=intents)
# Dictionary to track message count per channel
message_counts = {}


@bot.event
async def on_message(message):
    guild = message.guild  # Get the guild (server) the message is from
    channel = discord.utils.get(guild.text_channels, name="ai_chatter")

    # Verification
    if message.channel != channel: return;
    if message.author.bot: return;

    # Ensure tracking exists for this channel
    if message.channel.id not in message_counts:
        message_counts[message.channel.id] = 0
    # Increment message count
    message_counts[message.channel.id] += 1
    print(message_counts[message.channel.id])

    messages = []
    if message_counts[message.channel.id] >= 10: # Check if the count reaches 10
        async for message in channel.history(limit=10):
            messages.append(message.content)
        previous_messages = ("\n".join(messages))
        response = ai.text_generation(previous_messages, temperature=0.9, max_new_tokens=256, top_p=0.95, repetition_penalty=1.0)
        splited_response = split_string(response)
        for part in splited_response:
            await channel.send(part)

        message_counts[message.channel.id] = 0 # Reset the counter


    response = ai.text_generation(message, temperature=0.9, max_new_tokens=256, top_p=0.95, repetition_penalty=1.0)
    splited_response = split_string(str(response))
    for part in splited_response:
        await channel.send(part)

    await bot.process_commands(message) # Ensure commands still work

def split_string(text: str) -> list[str]:
    """Helper function to split text into chunks"""
    return [text[i:i+3900] for i in range(0, len(text), 3900)]

@bot.event
async def on_ready():
    print(f'Logged in as {bot.user}')  # Logs bot login in console

    guild = discord.utils.get(bot.guilds, name="PrzebieralniaKoedukacyjna")
    if guild:
        # Get the channel by name
        channel = discord.utils.get(guild.channels, name="ai_chatter")
        if channel:
            print(f"Channel found: {channel.name} (ID: {channel.id})")
        else:
            print("Channel not found!")

        await channel.send(f"{bot.user} logged in, runnin on 'huggingface.co/spaces")

bot.run("MTMzODQ4NTY2MzY3MDA3OTQ4OA.GlmK1T.7ZeEiDz7ViY3zvuSqlacVocDMSZ-ln80c09AS4")