File size: 3,318 Bytes
f495df9
9c551fd
8a3cd96
 
 
bbe2ee9
8a3cd96
 
 
 
 
 
bbe2ee9
8a3cd96
 
 
 
f5e2b6b
 
bbe2ee9
e989fed
bbe2ee9
e989fed
 
f5e2b6b
bbe2ee9
e989fed
 
f5e2b6b
 
5b34e20
bbe2ee9
f5e2b6b
bbe2ee9
 
8a3cd96
ba7e0ae
9fc7d0e
5b34e20
8a3cd96
 
 
 
 
5b34e20
bbe2ee9
 
5c84d6e
 
89e328a
5b34e20
bbe2ee9
 
9c551fd
 
bbe2ee9
74b7d49
 
34af537
0724869
9fc7d0e
9c551fd
9fc7d0e
9c551fd
9fc7d0e
9c551fd
9fc7d0e
 
 
 
 
 
 
 
 
 
 
 
 
8a3cd96
 
 
 
 
 
 
bbe2ee9
8a3cd96
 
 
 
 
 
 
f495df9
 
 
fdd450a
 
 
 
f495df9
fdd450a
 
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
import discord
import random
from discord.ext import commands
from ChatAI.chat_ai import pipe as ai

DEFAULT_CHATTER_CHANNEL="ai_chatter"

# 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=DEFAULT_CHATTER_CHANNEL)
    # Verification
    if message.channel != channel or 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])

    await respond_to_chat(message.content)

    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))
        await respond_to_chat(previous_messages)

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

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

async def respond_to_chat(message: str) ->str:
    response = generate(message)
    parts = split_string(response)
    guild = discord.utils.get(bot.guilds, name="PrzebieralniaKoedukacyjna")
    channel = discord.utils.get(guild.channels, name=DEFAULT_CHATTER_CHANNEL)
    for part in parts:
        await channel.send(part)
    return str("\n".join(parts))

def x():
    pass

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

def generate(
    prompt, temperature=0.0, max_new_tokens=2048, top_p=0.95, repetition_penalty=1.0
):
    if temperature == 0.0: temperature = random.uniform(1e-2, 0.9)
    temperature = float(temperature)

    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=DEFAULT_CHATTER_CHANNEL)
        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")


import os
from dotenv import load_dotenv
env_path = os.path.join(os.path.dirname(__file__), ".env.ini")
load_dotenv(env_path)
str(os.getenv("DISCORD_TOKEN_CHATTER"))
print(str(os.getenv("DISCORD_TOKEN_CHATTER")))


#bot.run(DISCORD_TOKEN_CHATTER)