-
Notifications
You must be signed in to change notification settings - Fork 0
/
bot.py
73 lines (56 loc) · 2.1 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
import os
import openai
import aiogram
from aiogram import Bot, types
from aiogram.dispatcher import Dispatcher
from aiogram.utils import executor
import requests
from io import BytesIO
from aiogram import Bot, Dispatcher, types
from aiogram.types import Message
# Set up Telegram bot
bot = Bot(token='5722175251:AAGac_Wtg6oJVLBJNuTLLLl7a8TBLJtsoQY')
dispatcher = Dispatcher(bot)
openai.api_key = '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'
# Handle incoming messages
@dispatcher.message_handler(commands=['start', 'help'])
async def welcome(message: types.Message):
# Send a response message
await bot.send_message(chat_id=message.chat.id, text='Hello! Im GPT chat bot.Ask me something!!')
# Handle incoming messages
@dispatcher.message_handler()
async def handle_message(message: types.Message):
# Send a response message
response = openai.Completion.create(
model="text-davinci-003",
prompt=message.text,
temperature=0.5,
max_tokens=1024,
top_p=1.0,
frequency_penalty=0.0,
presence_penalty=0.0,
)
await bot.send_message(chat_id=message.chat.id, text=response.choices[0].text)
# Define a message handler for the /image command
@dispatcher.message_handler(commands=["image"])
async def generate_image(message: Message):
# Get user input from the message
user_input = " ".join(message.text.split()[1:])
# Generate image using GPT-3 API
response = openai.Completion.create(
engine="davinci",
prompt=user_input + "\nGenerate an image of the above text:",
max_tokens=1024,
n=1,
stop=None,
temperature=0.5
)
# Get image URL from GPT-3 API response
image_url = response.choices[0].text.strip()
# Download image from URL
image_bytes = BytesIO(requests.get(image_url).content)
# Send image to user
await bot.send_photo(chat_id=message.chat.id, photo=image_bytes, caption="Here is your generated image!")
# Start the bot
if __name__ == '__main__':
executor.start_polling(dispatcher)