-
Notifications
You must be signed in to change notification settings - Fork 0
/
bot.py
79 lines (75 loc) · 3.38 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
import openai
from aiogram import Bot, types
from aiogram.dispatcher import Dispatcher
from aiogram.utils import executor
from aiogram.types import InputFile
from aiogram.dispatcher.filters import ChatTypeFilter, filters
import re
from io import BytesIO
import aiohttp
#Python 3.11.2: aiogram 2.25.1, aiohttp 3.8.4, aiosignal 1.3.1, openai 0.26.5
token = "enter here your tokens"
openai.api_key = "enter here your api-key from openai"
bot = Bot(token)
dp = Dispatcher(bot)
@dp.message_handler(chat_type=[types.ChatType.SUPERGROUP, types.ChatType.GROUP])
async def send(message: types.Message):
if "bot" in message.text.lower() or "бот" in message.text.lower():
responce = openai.Completion.create(
model="text-davinci-003",
prompt=message.text,
temperature=0.5,
max_tokens=3000,
top_p=1,
frequency_penalty=0,
presence_penalty=0,
stop=["Стоп"],
)
await message.answer(responce['choices'][0]['text'])
if "сгенерируй" in message.text.lower() or "генерируй" in message.text.lower() or "ganerate" in message.text.lower():
size = '256x256' if "smallSize" in message.text else "512x512" if "mediumSize" in message.text else "1024x1024"
responce = openai.Image.create(
prompt=message.text,
size=size,
)
image_url = responce['data'][0]['url']
async with aiohttp.ClientSession() as session:
async with session.get(image_url) as resp:
content = await resp.content.read()
await message.answer_photo(InputFile(BytesIO(content), filename="generated_image.png"), caption="Запрос: " + message.text + ", " + size)
@dp.message_handler(ChatTypeFilter(types.ChatType.PRIVATE))
async def private(message: types.Message):
if "сгенерируй" in message.text.lower() or "генерируй" in message.text.lower() or "ganerate" in message.text.lower():
try:
size = '256x256' if "smallSize" in message.text else "512x512" if "mediumSize" in message.text else "1024x1024"
responce = openai.Image.create(
prompt=message.text,
size=size,
)
image_url = responce['data'][0]['url']
async with aiohttp.ClientSession() as session:
async with session.get(image_url) as resp:
content = await resp.content.read()
await message.answer_photo(InputFile(BytesIO(content), filename="generated_image.png"), caption="Запрос: " + message.text + ", " + size)
return
except openai.error.OpenAIError as error:
print(f"OpenAI API error: {error}")
responce = f"OpenAI API error: {error}"
await message.answer(responce)
try:
responce = openai.Completion.create(
model="text-davinci-003",
prompt=message.text,
temperature=0.5,
max_tokens=4000,
top_p=1,
frequency_penalty=0,
presence_penalty=0,
stop=["Стоп"],
)
await message.answer(responce['choices'][0]['text'])
except openai.error.OpenAIError as error:
print(f"OpenAI API error: {error}")
responce = f"OpenAI API error: {error}"
await message.answer(responce)
executor.start_polling(dp, skip_updates=True)