-
Notifications
You must be signed in to change notification settings - Fork 0
/
discord chat bot base.py
144 lines (115 loc) · 5.04 KB
/
discord chat bot base.py
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
import discord
import torch
import re
import datetime
from transformers import T5Tokenizer, T5ForConditionalGeneration, RobertaForSequenceClassification, RobertaTokenizer
from discord.ext import commands
# Set up the Discord client
client = discord.Client(intents=discord.Intents.default())
# Check for GPU availability and set up the device accordingly
if torch.cuda.is_available():
device = torch.device("cuda")
else:
device = torch.device("cpu")
# Set up the T5 model and tokenizer
tokenizer = T5Tokenizer.from_pretrained('bigscience/T0_3B')
model = T5ForConditionalGeneration.from_pretrained('bigscience/T0_3B').to(device)
# Set up the Roberta model and tokenizer for Swear And Hate Speech Detection
hate_speech_model = RobertaForSequenceClassification.from_pretrained('facebook/roberta-hate-speech-dynabench-r4-target')
hate_speech_tokenizer = RobertaTokenizer.from_pretrained('facebook/roberta-hate-speech-dynabench-r4-target')
# Set up the model generation parameters
max_length = 2048
temperature = 1000
repetition_penalty = 1.0
num_beams = 5
no_repeat_ngram_size = 3
early_stopping = True
# Set up chat log file
chat_log_file = 'chat_log.txt'
# Load conversation history from file
conversation_history = []
try:
with open(chat_log_file, 'r', encoding='utf-8') as f:
for line in f:
conversation_history.append(line.strip())
except FileNotFoundError:
pass
# Function to detect hate speech
def detect_hate_speech(input_text):
# Tokenize the input text
input_ids = hate_speech_tokenizer.encode(input_text, return_tensors="pt").to(device)
# Set the attention mask to 1 for all input tokens
attention_mask = torch.ones_like(input_ids)
# Classify the input text using Roberta
outputs = hate_speech_model(input_ids=input_ids, attention_mask=attention_mask)
# Get the predicted label
predicted_label = outputs.logits.argmax().item()
return predicted_label
# Function to generate a response from the chatbot
async def generate_response(user, input_text):
# Remove the user's mention from the input text
input_text = re.sub(r"<@\!?\d+>", "", input_text).strip()
# Check if the input text contains hate speech
hate_speech_label = detect_hate_speech(input_text)
if hate_speech_label == 1:
response = "I'm sorry, I cannot respond to this message as it contains hate speech or swear words."
return f"{user.mention}: {response}"
# Add the user's mention to the context
context = f"{user.name}: "
# Tokenize the input text and context
input_ids = tokenizer.encode(f"{context}{input_text}\n", return_tensors="pt").to(device)
# Set the attention mask to 1 for all input tokens
attention_mask = torch.ones_like(input_ids)
# Generate a response using T5
output = model.generate(
input_ids=input_ids,
attention_mask=attention_mask,
max_length=max_length,
temperature=temperature,
repetition_penalty=repetition_penalty,
num_beams=num_beams,
no_repeat_ngram_size=no_repeat_ngram_size,
early_stopping=early_stopping,
use_cache=True
)
# Decode the response and remove the bot mention
response = tokenizer.decode(output[0], skip_special_tokens=True).replace(client.user.mention, '').split('\n')[0]
# If the response is empty, return a default message
if not response:
response = "I'm sorry, I didn't understand that."
# Save the conversation history to the chat log file and add it to the conversation history list
now = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
with open(chat_log_file, 'a', encoding='utf-8') as f:
f.write(f"{now} - {user.name}: {input_text}\n")
f.write(f"{now} - Bot: {response}\n")
conversation_history.append(f"{now} - {user.name}: {input_text}")
conversation_history.append(f"{now} - Bot: {response}")
return f"{user.mention}: {response}"
# Function to handle incoming messages
async def handle_message(message):
# Ignore messages from the bot itself
if message.author == client.user:
return
# Check if the bot is mentioned in the message
if client.user.mentioned_in(message):
# Generate a response
response = await generate_response(message.author, message.content)
# Send the response to the channel
await message.channel.send(response)
# Event handler for bot startup
@client.event
async def on_ready():
print(f"Logged in as {client.user}")
# Set the bot's activity and status
activity = discord.Activity(
type=discord.ActivityType.playing,
name="English Chat Bot | Practice English"
)
await client.change_presence(activity=activity, status=discord.Status.online)
# Function to handle incoming messages
@client.event
async def on_message(message):
# Handle the message
await handle_message(message)
# Run the bot with your Discord bot token
client.run("Your Discord Bot Token Here")