-
Notifications
You must be signed in to change notification settings - Fork 0
/
bot.py
162 lines (120 loc) · 5.72 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
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
import os
import openai as nagaAPI
import logging
import nest_asyncio
import requests
from PIL import Image
from io import BytesIO
from translator import Translator
from telegram import Update
from telegram.constants import ChatAction
from telegram.ext import Application, CommandHandler, ContextTypes, MessageHandler, filters
# Set API base and key to nova endpoint
nagaAPI.api_base = 'https://api.naga.ac/v1'
nagaAPI.api_key = os.getenv('NAGA_API_KEY')
# Enable multiple loops
nest_asyncio.apply()
# Enable logging
os.makedirs(os.path.dirname('logs/log.txt'), exist_ok=True)
logging.basicConfig(
filename='logs/log.txt',
filemode='w',
format="%(asctime)s - %(name)s - %(levelname)s - %(message)s",
level=logging.INFO
)
logging.getLogger().setLevel(logging.WARNING)
logger = logging.getLogger(__name__)
messages = [{"role": "system", "content": "You are an intelligent assistant."}]
def download_image(url: str, path: str) -> None:
"""Downloads an image and returns it."""
try:
response = requests.get(url)
if response.status_code == 200:
# Make sure the directory exists
os.makedirs(os.path.dirname(path), exist_ok=True)
image = Image.open(BytesIO(response.content))
image.save(fp=path)
else:
raise Exception(f"Failed to download image. Status code: {response.status_code}")
except Exception as e:
print(f"An error occurred: {str(e)}")
raise e
async def fetch_response(message: str) -> str:
"""Connects with the API to find the messages"""
messages.append({"role": "user", "content": message})
response = await nagaAPI.ChatCompletion.acreate(
model="gpt-3.5-turbo", messages=messages
)
return response['choices'][0]['message']['content']
async def pipe(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
"""Drops an iron pipe"""
await context.bot.send_chat_action(chat_id=update.message.chat_id, action=ChatAction.UPLOAD_VIDEO)
pipe_mp4 = open(str('./archive/pipe.mp4'), "rb")
await update.message.reply_video(pipe_mp4)
async def imagine(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
"""Generates an image according to the user's prompt"""
await context.bot.send_chat_action(chat_id=update.message.chat_id, action=ChatAction.UPLOAD_PHOTO)
messages.append({"role": "user", "content": update.message.text})
prompt = update.message.text.replace("/imagine", "").strip()
try:
response = nagaAPI.Image.create(
prompt=prompt,
n=1,
size="1024x1024",
model="sdxl"
)
path = "tmp/image.jpg"
download_image(response['data'][0]['url'], path)
await update.message.reply_photo(photo=open(path, "rb"))
except Exception as e:
print(f"An exception ocurred: {str(e)}")
return await update.message.reply_text(str(e))
async def help(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
"""Tells user what the chat can be used for."""
translator = Translator()
await update.message.reply_text(translator.get_message(update.effective_user.language_code, "help"))
async def start(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
"""Send a message when the command /start is issued."""
message = Translator().get_message(update.effective_user.language_code, "start")
await update.message.reply_text(f"{message}, {update.effective_user.full_name}")
async def restart(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
"""Restarts conversation"""
messages.clear()
messages.append({"role": "system", "content": "You are an intelligent assistant."})
await update.message.reply_photo('./archive/ah shit.jpg')
async def process_voice_message(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
"""Transcribes the audio file to text and contact the Nova API to generate a response."""
await context.bot.send_chat_action(chat_id=update.message.chat_id, action=ChatAction.TYPING)
audio_data = await update.message.voice.get_file()
audio_path = await audio_data.download_to_drive()
audio_file = open(str(audio_path), "rb")
transcription = nagaAPI.Audio.transcribe("whisper-1", audio_file)
response = await fetch_response(transcription['text'])
await update.message.reply_text(response)
async def talk(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
"""
Main conversational function.
Responsible for calling the function that will send the message to chat-gpt and then answering the user.
"""
await context.bot.send_chat_action(chat_id=update.message.chat_id, action=ChatAction.TYPING)
response = await fetch_response(update.message.text)
await update.message.reply_text(response)
def main() -> None:
"""Start the bot."""
token = os.getenv('TELEGRAM_TOKEN')
# Create the Application and pass it your bot's token.
application = Application.builder().token(token).build()
# on different commands - answer in Telegram
application.add_handler(CommandHandler("start", start))
application.add_handler(CommandHandler("restart", restart))
application.add_handler(CommandHandler("help", help))
application.add_handler(CommandHandler("imagine", imagine))
application.add_handler(CommandHandler("pipe", pipe))
# on non command i.e message - echo the message on Telegram
application.add_handler(MessageHandler(filters.TEXT & ~filters.COMMAND, talk))
# audio messages will use speech-to-text
application.add_handler(MessageHandler(filters.VOICE, process_voice_message))
# Run the bot until the user presses Ctrl-C
application.run_polling(allowed_updates=Update.ALL_TYPES)
if __name__ == "__main__":
main()