File size: 2,828 Bytes
8a3cd96
 
 
 
 
 
 
 
 
 
 
 
 
 
f5e2b6b
 
e989fed
8a3cd96
e989fed
 
 
f5e2b6b
e989fed
 
f5e2b6b
e989fed
 
f5e2b6b
 
 
9fc7d0e
 
8a3cd96
ba7e0ae
9fc7d0e
 
 
 
56ce1e7
8a3cd96
 
 
 
 
74b7d49
 
 
0724869
9fc7d0e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
72
73
74
75
76
77
78
79
80
81
82
83
import discord
from discord.ext import commands
from ChatAI.chat_ai import pipe as ai


# 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] >= 1: # Check if the count reaches 10
        async for message in channel.history(limit=4):
            messages.append(message.content)

        previous_messages = ("\n".join(messages))
        response = generate(previous_messages, temperature=0.9, max_new_tokens=256, top_p=0.95, repetition_penalty=1.0)
        part = split_string(str(response))
        for part in part:
            await channel.send(part)

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

    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)]

def generate(
    prompt, temperature=0.9, max_new_tokens=256, top_p=0.95, repetition_penalty=1.0
):
    temperature = float(temperature)
    if temperature < 1e-2:
        temperature = 1e-2
    top_p = float(top_p)

    generate_kwargs = dict(
        temperature=temperature,
        max_new_tokens=max_new_tokens,
        top_p=top_p,
        repetition_penalty=repetition_penalty,
        do_sample=True,
        seed=42,
    )

    return ai.text_generation(prompt, **generate_kwargs, stream=False, details=False, return_full_text=True)

@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")