Spaces:
Runtime error
Runtime error
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 = {} | |
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)] | |
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") |