This repository has been archived by the owner on Dec 6, 2024. It is now read-only.
forked from badgids/OpenKlyde
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathbot.py
108 lines (86 loc) · 3.63 KB
/
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
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
import asyncio
import os
import discord
from discord import app_commands
from discord import Intents
from discord import Client
import logging
import argparse
import config
from process import charutil
import util
from discord import app_commands
from dotenv import load_dotenv
from observer import observer
from model import apiconfig
from interface import main
from process import history
from unittest.mock import patch
from transformers.dynamic_module_utils import get_imports
from transformers import AutoProcessor, AutoModelForCausalLM
import warnings
from huggingface_hub import file_download
from process import controller
# Suppress the specific FutureWarning from huggingface_hub
warnings.filterwarnings("ignore", category=FutureWarning, module="huggingface_hub.file_download")
warnings.filterwarnings("ignore", category=UserWarning, module="torch.utils")
load_dotenv()
discord_token: str | None = os.getenv("DISCORD_TOKEN")
if discord_token is None:
raise RuntimeError("$DISCORD_TOKEN env variable is not set!")
client = config.client
def fixed_get_imports(filename: str | os.PathLike) -> list[str]:
if not str(filename).endswith("modeling_florence2.py"):
return get_imports(filename)
imports = get_imports(filename)
if "flash_attn" in imports:
imports.remove("flash_attn")
return imports
with patch("transformers.dynamic_module_utils.get_imports", fixed_get_imports):
config.florence = AutoModelForCausalLM.from_pretrained("microsoft/Florence-2-base-ft",
trust_remote_code=True,
attn_implementation='sdpa')
config.florence_processor = AutoProcessor.from_pretrained("microsoft/Florence-2-base-ft",
trust_remote_code=True)
tree = app_commands.CommandTree(client)
@client.event
async def on_ready():
# Let owner known in the console that the bot is now running!
print(f'Discord Bot is Loading...')
# Oh right, I have logging...
logging.basicConfig(level=logging.DEBUG)
# Setup the Connection with API
config.text_api = await apiconfig.set_api("text-default.json")
await apiconfig.api_status_check(config.text_api["address"] + config.text_api["model"], headers=config.text_api["headers"])
asyncio.create_task(controller.think())
# Command to Edit Message (You Right Click On It)
edit_message = discord.app_commands.ContextMenu(
name='Edit Message',
callback=main.edit_message_context,
type=discord.AppCommandType.message
)
# Command to Delete Message (You Right Click On It)
delete_message = discord.app_commands.ContextMenu(
name='Delete Message',
callback=main.delete_message_context,
type=discord.AppCommandType.message
)
# Initialize the Commands
tree.add_command(edit_message)
tree.add_command(delete_message)
await tree.sync(guild=None)
print(f'Discord Bot is up and running.')
# This is what a slash command looks like apparently
@tree.command(name="character_list", description="Show a list of available characters!")
async def character_list(interaction: discord.Interaction):
characters = await main.character_info() # Fetch character info asynchronously
await interaction.response.send_message(characters, ephemeral=True) # Send the response
@client.event
async def on_message(message):
if message is None:
return
# Trigger the Observer Behavior (The command that listens to Keyword)
print("Message Get")
await observer.bot_behavior(message, client)
# Run the Bot
client.run(discord_token)