Radosław Wolnik commited on
Commit
60a5741
·
1 Parent(s): 6fc31be
Files changed (1) hide show
  1. app.py +20 -22
app.py CHANGED
@@ -1,55 +1,48 @@
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(previous_messages)
40
 
41
- message_counts[message.channel.id] = 0 # Reset the counter
42
-
43
- await bot.process_commands(str(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"""
@@ -72,7 +65,12 @@ def generate(
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():
@@ -81,7 +79,7 @@ async def on_ready():
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:
 
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
  @bot.event
16
  async def on_message(message):
17
  guild = message.guild # Get the guild (server) the message is from
18
+ channel = discord.utils.get(guild.text_channels, name="ai_chatter")
19
 
20
  # Verification
21
+ if message.channel != channel: return;
22
+ if message.author.bot: return;
23
+
24
  # Ensure tracking exists for this channel
25
  if message.channel.id not in message_counts:
26
  message_counts[message.channel.id] = 0
 
27
  # Increment message count
28
  message_counts[message.channel.id] += 1
29
  print(message_counts[message.channel.id])
30
 
 
 
31
  messages = []
32
+ if message_counts[message.channel.id] >= 1: # Check if the count reaches 10
33
+ async for message in channel.history(limit=4):
34
  messages.append(message.content)
35
 
36
  previous_messages = ("\n".join(messages))
37
+ response = generate(previous_messages, temperature=0.9, max_new_tokens=256, top_p=0.95, repetition_penalty=1.0)
38
 
39
+ part = split_string(response)
40
+ for part in part:
41
+ await channel.send(part)
42
 
43
+ message_counts[message.channel.id] = 0 # Reset the counter
 
 
 
 
 
 
44
 
45
+ await bot.process_commands(message) # Ensure commands still work
46
 
47
  def split_string(text: str) -> list[str]:
48
  """Helper function to split text into chunks"""
 
65
  seed=42,
66
  )
67
 
68
+ stream = ai.text_generation(prompt, **generate_kwargs, stream=False, details=True, return_full_text=True)
69
+ output = ""
70
+ for response in stream:
71
+ output += response.token.text
72
+ yield output
73
+ return output
74
 
75
  @bot.event
76
  async def on_ready():
 
79
  guild = discord.utils.get(bot.guilds, name="PrzebieralniaKoedukacyjna")
80
  if guild:
81
  # Get the channel by name
82
+ channel = discord.utils.get(guild.channels, name="ai_chatter")
83
  if channel:
84
  print(f"Channel found: {channel.name} (ID: {channel.id})")
85
  else: