Spaces:
Runtime error
Runtime error
Radosław Wolnik
commited on
Commit
·
02224a5
1
Parent(s):
232a513
Responding
Browse files
app.py
CHANGED
@@ -1,59 +1,79 @@
|
|
1 |
import discord
|
2 |
from discord.ext import commands
|
3 |
from ChatAI.chat_ai import pipe as ai
|
4 |
-
from huggingface_hub import InferenceClient
|
5 |
|
|
|
6 |
|
7 |
# Set up Discord bot intents and command prefix
|
8 |
intents = discord.Intents.default()
|
9 |
intents.message_content = True
|
10 |
intents.messages = True
|
11 |
bot = commands.Bot(command_prefix="!", intents=intents)
|
|
|
12 |
# Dictionary to track message count per channel
|
13 |
message_counts = {}
|
14 |
|
15 |
-
|
16 |
@bot.event
|
17 |
async def on_message(message):
|
18 |
guild = message.guild # Get the guild (server) the message is from
|
19 |
-
channel = discord.utils.get(guild.text_channels, name=
|
20 |
|
21 |
# Verification
|
22 |
-
if message.channel != channel: return;
|
23 |
-
if message.author.bot: return;
|
24 |
-
|
25 |
# Ensure tracking exists for this channel
|
26 |
if message.channel.id not in message_counts:
|
27 |
message_counts[message.channel.id] = 0
|
|
|
28 |
# Increment message count
|
29 |
message_counts[message.channel.id] += 1
|
30 |
print(message_counts[message.channel.id])
|
31 |
|
|
|
|
|
32 |
messages = []
|
33 |
if message_counts[message.channel.id] >= 10: # Check if the count reaches 10
|
34 |
async for message in channel.history(limit=10):
|
35 |
messages.append(message.content)
|
36 |
-
previous_messages = ("\n".join(messages))
|
37 |
|
38 |
-
|
39 |
-
|
40 |
-
for part in splitted_response:
|
41 |
-
await channel.send(part)
|
42 |
|
43 |
message_counts[message.channel.id] = 0 # Reset the counter
|
44 |
|
|
|
45 |
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
|
|
|
|
|
|
50 |
|
51 |
-
await bot.process_commands(message) # Ensure commands still work
|
52 |
|
53 |
def split_string(text: str) -> list[str]:
|
54 |
"""Helper function to split text into chunks"""
|
55 |
return [text[i:i+3900] for i in range(0, len(text), 3900)]
|
56 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
57 |
@bot.event
|
58 |
async def on_ready():
|
59 |
print(f'Logged in as {bot.user}') # Logs bot login in console
|
@@ -61,7 +81,7 @@ async def on_ready():
|
|
61 |
guild = discord.utils.get(bot.guilds, name="PrzebieralniaKoedukacyjna")
|
62 |
if guild:
|
63 |
# Get the channel by name
|
64 |
-
channel = discord.utils.get(guild.channels, name=
|
65 |
if channel:
|
66 |
print(f"Channel found: {channel.name} (ID: {channel.id})")
|
67 |
else:
|
|
|
1 |
import discord
|
2 |
from discord.ext import commands
|
3 |
from ChatAI.chat_ai import pipe as ai
|
|
|
4 |
|
5 |
+
DEFAULT_CHATTER_CHANNEL="ai_chatter"
|
6 |
|
7 |
# Set up Discord bot intents and command prefix
|
8 |
intents = discord.Intents.default()
|
9 |
intents.message_content = True
|
10 |
intents.messages = True
|
11 |
bot = commands.Bot(command_prefix="!", intents=intents)
|
12 |
+
|
13 |
# Dictionary to track message count per channel
|
14 |
message_counts = {}
|
15 |
|
|
|
16 |
@bot.event
|
17 |
async def on_message(message):
|
18 |
guild = message.guild # Get the guild (server) the message is from
|
19 |
+
channel = discord.utils.get(guild.text_channels, name=DEFAULT_CHATTER_CHANNEL)
|
20 |
|
21 |
# Verification
|
22 |
+
if message.channel != channel or message.author.bot: return;
|
|
|
|
|
23 |
# Ensure tracking exists for this channel
|
24 |
if message.channel.id not in message_counts:
|
25 |
message_counts[message.channel.id] = 0
|
26 |
+
|
27 |
# Increment message count
|
28 |
message_counts[message.channel.id] += 1
|
29 |
print(message_counts[message.channel.id])
|
30 |
|
31 |
+
respond_to_chat(message)
|
32 |
+
|
33 |
messages = []
|
34 |
if message_counts[message.channel.id] >= 10: # Check if the count reaches 10
|
35 |
async for message in channel.history(limit=10):
|
36 |
messages.append(message.content)
|
|
|
37 |
|
38 |
+
previous_messages = ("\n".join(messages))
|
39 |
+
respond_to_chat(message)
|
|
|
|
|
40 |
|
41 |
message_counts[message.channel.id] = 0 # Reset the counter
|
42 |
|
43 |
+
await bot.process_commands(message) # Ensure commands still work
|
44 |
|
45 |
+
def respond_to_chat(message) ->str:
|
46 |
+
response = generate(message)
|
47 |
+
parts = split_string(response)
|
48 |
+
channel = discord.utils.get(message.guild.text_channels, name=DEFAULT_CHATTER_CHANNEL)
|
49 |
+
for part in parts:
|
50 |
+
channel.send(part)
|
51 |
+
return str("\n".join(parts))
|
52 |
|
|
|
53 |
|
54 |
def split_string(text: str) -> list[str]:
|
55 |
"""Helper function to split text into chunks"""
|
56 |
return [text[i:i+3900] for i in range(0, len(text), 3900)]
|
57 |
|
58 |
+
def generate(
|
59 |
+
prompt, temperature=0.9, max_new_tokens=256, top_p=0.95, repetition_penalty=1.0
|
60 |
+
):
|
61 |
+
temperature = float(temperature)
|
62 |
+
if temperature < 1e-2:
|
63 |
+
temperature = 1e-2
|
64 |
+
top_p = float(top_p)
|
65 |
+
|
66 |
+
generate_kwargs = dict(
|
67 |
+
temperature=temperature,
|
68 |
+
max_new_tokens=max_new_tokens,
|
69 |
+
top_p=top_p,
|
70 |
+
repetition_penalty=repetition_penalty,
|
71 |
+
do_sample=True,
|
72 |
+
seed=42,
|
73 |
+
)
|
74 |
+
|
75 |
+
return ai.text_generation(prompt, **generate_kwargs, stream=False, details=False, return_full_text=True)
|
76 |
+
|
77 |
@bot.event
|
78 |
async def on_ready():
|
79 |
print(f'Logged in as {bot.user}') # Logs bot login in console
|
|
|
81 |
guild = discord.utils.get(bot.guilds, name="PrzebieralniaKoedukacyjna")
|
82 |
if guild:
|
83 |
# Get the channel by name
|
84 |
+
channel = discord.utils.get(guild.channels, name=DEFAULT_CHATTER_CHANNEL)
|
85 |
if channel:
|
86 |
print(f"Channel found: {channel.name} (ID: {channel.id})")
|
87 |
else:
|