Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,34 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import discord
|
2 |
+
from discord.ext import commands
|
3 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM
|
4 |
+
import torch
|
5 |
+
|
6 |
+
# Настройка бота
|
7 |
+
intents = discord.Intents.default()
|
8 |
+
intents.message_content = True
|
9 |
+
bot = commands.Bot(command_prefix='!', intents=intents)
|
10 |
+
|
11 |
+
# Загрузка модели и токенизатора
|
12 |
+
model_name = "Qwen/Qwen2-72B"
|
13 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
14 |
+
model = AutoModelForCausalLM.from_pretrained(model_name)
|
15 |
+
|
16 |
+
@bot.event
|
17 |
+
async def on_ready():
|
18 |
+
print(f'{bot.user} has connected to Discord!')
|
19 |
+
|
20 |
+
@bot.command(name='AI')
|
21 |
+
async def ai_response(ctx, *, question):
|
22 |
+
# Подготовка входных данных
|
23 |
+
inputs = tokenizer.encode(question, return_tensors='pt')
|
24 |
+
|
25 |
+
# Генерация ответа
|
26 |
+
with torch.no_grad():
|
27 |
+
outputs = model.generate(inputs, max_length=100, num_return_sequences=1)
|
28 |
+
|
29 |
+
# Декодирование и отправка ответа
|
30 |
+
response = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
31 |
+
await ctx.send(response)
|
32 |
+
|
33 |
+
# Запуск бота
|
34 |
+
bot.run('YOUR_BOT_TOKEN')
|