Spaces:
Runtime error
Runtime error
<<<<<<< SEARCH | |
import commands | |
from .message_handler import on_message, respond_to_chat | |
from .utils import split_string, generate, generate_response | |
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 = {} | |
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) | |
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) | |
======= | |
from commands import Commands | |
from .message_handler import on_message, respond_to_chat | |
from .bot import Bot | |
from .utils import split_string, generate | |
from ChatAI.chat_ai import ChatAI | |
import logging | |
bot = Commands().bot | |
>>>>>>> REPLACE | |