-
Notifications
You must be signed in to change notification settings - Fork 17
/
chatbot.py
279 lines (219 loc) · 9.02 KB
/
chatbot.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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
import os
from PIL import Image, ImageDraw, ImageFont
from io import BytesIO
import replicate
import openai
from dotenv import load_dotenv
import telebot
import requests
from gtts import gTTS
from pydub import AudioSegment
from celery import Celery
import speech_recognition as sr
load_dotenv()
openai.api_key = os.getenv('OPEN_AI_KEY')
app = Celery('chatbot', broker=os.getenv('CELERY_BROKER_URL'))
TELEGRAM_BOT_TOKEN = os.getenv('TELEGRAM_BOT_TOKEN')
bot = telebot.TeleBot(TELEGRAM_BOT_TOKEN)
model = replicate.models.get("prompthero/openjourney")
version = model.versions.get("9936c2001faa2194a261c01381f90e65261879985476014a0a37a334593a05eb")
SYSTEM_PROMPT = os.getenv('SYSTEM_PROMPT')
# Store the last 10 conversations for each user
conversations = {}
def image_watermark(img_response):
"""
:param img_response: image url
:return: Byte image
"""
img = Image.open(BytesIO(img_response.content))
# Add the watermark to the image
draw = ImageDraw.Draw(img)
watermark_text = "DeadlyAI"
font = ImageFont.truetype("anime.ttf", 20)
# text_size = draw.textsize(watermark_text, font=font)
# Positioning Text
x = 6
y = 6
# Add a shadow border to the text
for offset in range(1, 2):
draw.text((x - offset, y), watermark_text, font=font, fill=(88, 88, 88))
draw.text((x + offset, y), watermark_text, font=font, fill=(88, 88, 88))
draw.text((x, y + offset), watermark_text, font=font, fill=(88, 88, 88))
draw.text((x, y - offset), watermark_text, font=font, fill=(88, 88, 88))
# Applying text on image sonic draw object
draw.text((x, y), watermark_text, font=font, fill=(255, 255, 255))
# Upload the watermarked image to OpenAI and get the URL
img_bytes = BytesIO()
img.save(img_bytes, format="JPEG")
img_bytes = img_bytes.getvalue()
return img_bytes
@app.task
def generate_image_replicate(prompt):
inputs = {
# Input prompt
'prompt': "mdjrny-v4 style " + prompt + " 4k resolution",
# Width of output image. Maximum size is 1024x768 or 768x1024 because
# of memory limits
'width': 512,
# Height of output image. Maximum size is 1024x768 or 768x1024 because
# of memory limits
'height': 512,
# Number of images to output
'num_outputs': 1,
# Number of denoising steps
# Range: 1 to 500
'num_inference_steps': 50,
# Scale for classifier-free guidance
# Range: 1 to 20
'guidance_scale': 6,
# Random seed. Leave blank to randomize the seed
# 'seed': ...,
}
output = version.predict(**inputs)
return output[0]
@app.task
def generate_image(prompt, number=1):
response = openai.Image.create(
prompt=prompt,
n=number,
size="512x512"
)
image_url = response['data']
return image_url
@app.task
def generate_response_chat(message_list):
response = openai.ChatCompletion.create(
model="gpt-3.5-turbo",
messages=[
{"role": "system",
"content": SYSTEM_PROMPT},
] + message_list
)
return response["choices"][0]["message"]["content"].strip()
def conversation_tracking(text_message, user_id):
"""
Make remember all the conversation
:param user_id: telegram user id
:param text_message: text message
:return: str
"""
# Get the last 10 conversations and responses for this user
user_conversations = conversations.get(user_id, {'conversations': [], 'responses': []})
user_messages = user_conversations['conversations'][-9:] + [text_message]
user_responses = user_conversations['responses'][-9:]
# Store the updated conversations and responses for this user
conversations[user_id] = {'conversations': user_messages, 'responses': user_responses}
# Construct the full conversation history in the user:assistant, " format
conversation_history = []
for i in range(min(len(user_messages), len(user_responses))):
conversation_history.append({
"role": "user", "content": user_messages[i]
})
conversation_history.append({
"role": "assistant", "content": user_responses[i]
})
# Add last prompt
conversation_history.append({
"role": "user", "content": text_message
})
# Generate response
task = generate_response_chat.apply_async(args=[conversation_history])
response = task.get()
# Add the response to the user's responses
user_responses.append(response)
# Store the updated conversations and responses for this user
conversations[user_id] = {'conversations': user_messages, 'responses': user_responses}
return response
@bot.message_handler(commands=["start", "help"])
def start(message):
if message.text.startswith("/help"):
bot.reply_to(message, "/image to generate image animation\n/create generate image\n/clear - Clears old "
"conversations\nsend text to get replay\nsend voice to do voice"
"conversation")
else:
bot.reply_to(message, "Just start chatting to the AI or enter /help for other commands")
# Define a function to handle voice messages
@bot.message_handler(content_types=["voice"])
def handle_voice(message):
user_id = message.chat.id
# Download the voice message file from Telegram servers
file_info = bot.get_file(message.voice.file_id)
file = requests.get("https://api.telegram.org/file/bot{0}/{1}".format(
TELEGRAM_BOT_TOKEN, file_info.file_path))
# Save the file to disk
with open("voice_message.ogg", "wb") as f:
f.write(file.content)
# Use pydub to read in the audio file and convert it to WAV format
sound = AudioSegment.from_file("voice_message.ogg", format="ogg")
sound.export("voice_message.wav", format="wav")
# Use SpeechRecognition to transcribe the voice message
r = sr.Recognizer()
with sr.AudioFile("voice_message.wav") as source:
audio_data = r.record(source)
text = r.recognize_google(audio_data)
# Generate response
replay_text = conversation_tracking(text, user_id)
# Send the question text back to the user
# Send the transcribed text back to the user
new_replay_text = "Human: " + text + "\n\n" + "sonic: " + replay_text
bot.reply_to(message, new_replay_text)
# Use Google Text-to-Speech to convert the text to speech
tts = gTTS(replay_text)
tts.save("voice_message.mp3")
# Use pydub to convert the MP3 file to the OGG format
sound = AudioSegment.from_mp3("voice_message.mp3")
sound.export("voice_message_replay.ogg", format="mp3")
# Send the transcribed text back to the user as a voice
voice = open("voice_message_replay.ogg", "rb")
bot.send_voice(message.chat.id, voice)
voice.close()
# Delete the temporary files
os.remove("voice_message.ogg")
os.remove("voice_message.wav")
os.remove("voice_message.mp3")
os.remove("voice_message_replay.ogg")
@bot.message_handler(commands=["create", "image"])
def handle_image(message):
space_markup = ' '
image_footer = '[Website](https://deadlyai.com)'
caption = f"Powered by **[Sonic](https://t.me/sleepomi)" + space_markup + image_footer
if message.text.startswith("/image"):
prompt = message.text.replace("/image", "").strip()
task = generate_image_replicate.apply_async(args=[prompt])
image_url = task.get()
if image_url is not None:
img_response = requests.get(image_url)
img_bytes = image_watermark(img_response)
bot.send_photo(chat_id=message.chat.id, photo=img_bytes, reply_to_message_id=message.message_id,
caption=caption, parse_mode='Markdown')
else:
bot.reply_to(message, "Could not generate image, try again later.")
else:
number = message.text[7:10]
prompt = message.text.replace("/create", "").strip()
try:
numbers = int(number)
except Exception as e:
print(str(e))
numbers = 1
task = generate_image.apply_async(args=[prompt, numbers])
image_url = task.get()
for img in image_url:
if img['url'] is not None:
bot.send_photo(chat_id=message.chat.id, photo=img['url'], reply_to_message_id=message.message_id,
caption=caption, parse_mode='Markdown')
else:
bot.reply_to(message, "Could not generate image, try again later.")
@bot.message_handler(func=lambda message: True)
def echo_message(message):
user_id = message.chat.id
# Handle /clear command
if message.text == '/clear':
conversations[user_id] = {'conversations': [], 'responses': []}
bot.reply_to(message, "Conversations and responses cleared!")
return
response = conversation_tracking(message.text, user_id)
# Reply to message
bot.reply_to(message, response)
if __name__ == "__main__":
bot.polling()