-
Notifications
You must be signed in to change notification settings - Fork 0
/
discord_bot.py
72 lines (58 loc) · 2.01 KB
/
discord_bot.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
import datetime
import os
import discord
from discord.ext import commands
from discord import app_commands
from dotenv import load_dotenv
import io
from model_loader import resnet_model, transform, LABELS # Import model-related code
import torch
from PIL import Image
import pytz
DEVICE_NAME = "cpu"
device = torch.device(DEVICE_NAME)
# Load environment variables
load_dotenv()
# Discord bot code
TOKEN = os.getenv('DISCORD_TOKEN')
bot = commands.Bot(command_prefix='!', intents=discord.Intents.all())
@bot.event
async def on_ready():
print(f'{bot.user} has connected to Discord!')
try:
synced = await bot.tree.sync()
print(f"Synced {len(synced)} commands")
except Exception as e:
print(e)
@bot.tree.command(name='clear')
@app_commands.describe()
async def clear_messages(ctx):
channel = ctx.channel
messages = []
utc_now = datetime.datetime.utcnow().replace(tzinfo=pytz.utc)
async for message in channel.history(limit=None):
if (utc_now - message.created_at).days < 14:
messages.append(message)
print(f"Deleting {len(messages)} messages")
while messages:
batch = messages[:100]
messages = messages[100:]
await channel.delete_messages(batch)
@bot.tree.command(name="predict")
@app_commands.describe(image="Upload an image to predict")
async def predict(ctx, image: discord.Attachment):
# Check if the attachment is an image
if not image.content_type.startswith('image/'):
await ctx.send("Please upload a valid image.")
return
try:
img = transform(Image.open(io.BytesIO(await image.read())))
img = img[None, ...]
with torch.no_grad():
y_hat = resnet_model.forward(img.to(device))
predicted = torch.argmax(y_hat.data, dim=1)
result = LABELS[predicted.data]
await ctx.response.send_message(f"Type of Tumor is {result}")
except Exception as e:
await ctx.response.send_message(f"An error occurred: {str(e)}")
bot.run(TOKEN)