generated from PeoplePlusAI/New-Working-Group
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmain.py
262 lines (224 loc) · 9.1 KB
/
main.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
import os
import base64
import tempfile
import time
from typing import Union
import asyncio
import logging
import dotenv
from telegram import (
Update,
InlineKeyboardButton,
InlineKeyboardMarkup
)
from telegram.ext import (
ApplicationBuilder,
ContextTypes,
MessageHandler,
CommandHandler,
filters,
CallbackContext,
CallbackQueryHandler,
)
from core.ai import (
chat,
audio_chat,
bhashini_text_chat,
bhashini_audio_chat,
guardrail, moderation_check
)
from utils.redis_utils import set_redis
from utils.openai_utils import (
get_duration_pydub,
get_random_wait_messages
)
dotenv.load_dotenv("ops/.env")
token = os.getenv('TELEGRAM_BOT_TOKEN')
logging.basicConfig(
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
level=logging.INFO
)
# class BotInitializer:
# _instance = None
# run_once = False
# def __new__(cls):
# if cls._instance is None:
# cls._instance = super(BotInitializer, cls).__new__(cls)
# cls.run_once = True
# return cls._instance
async def start(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
# BotInitializer() # To initialize only once
await context.bot.send_message(
chat_id=update.effective_chat.id,
text="Hello I am Mr. Nags, start raising a complaint with me"
)
await relay_handler(update, context)
async def relay_handler(update: Update, context: CallbackContext):
await language_handler(update, context)
async def language_handler(update: Update, context: CallbackContext):
# Handle user's language selection
keyboard = [
[InlineKeyboardButton("English", callback_data='1')],
[InlineKeyboardButton("हिंदी", callback_data='2')],
[InlineKeyboardButton("ਪੰਜਾਬੀ", callback_data='3')],
]
reply_markup = InlineKeyboardMarkup(keyboard)
await context.bot.send_message(
chat_id=update.effective_chat.id,
text="Choose a Language:",
reply_markup=reply_markup
)
async def preferred_language_callback(update: Update, context: CallbackContext):
callback_query = update.callback_query
languages = {"1": "en", "2": "hi", "3": "pa"}
try:
preferred_language = callback_query.data
lang = languages.get(preferred_language)
context.user_data['lang'] = lang
except (AttributeError, ValueError):
lang = 'en'
await context.bot.send_message(
chat_id=update.effective_chat.id,
text="Error getting language! Setting default to English."
)
text_message = ""
if lang == "en":
text_message = "You have chosen English. \nPlease give your complaint now"
elif lang == "hi":
text_message = "आपने हिंदी चुनी है. \nकृपया अभी अपनी शिकायत दर्ज करें।"
elif lang == "pa":
text_message = "ਤੁਸੀਂ ਪੰਜਾਬੀ ਨੂੰ ਚੁਣਿਆ ਹੈ। \ਕਿਰਪਾ ਕਰਕੇ ਹੁਣੇ ਆਪਣੀ ਸ਼ਿਕਾਇਤ ਦਿਓ"
set_redis('lang', lang)
await context.bot.send_message(
chat_id=update.effective_chat.id,
text=text_message
)
async def response_handler(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
await query_handler(update, context)
def check_change_language_query(text):
return text.lower() in ["change language", "set language", "language"]
async def query_handler(update: Update, context: CallbackContext):
lang = context.user_data.get('lang')
if not lang:
await language_handler(update, context)
return
if update.message.text:
text = update.message.text
print(f"text is {text}")
if check_change_language_query(text):
await language_handler(update, context)
return
await chat_handler(update, context, text)
elif update.message.voice:
voice = await context.bot.get_file(update.message.voice.file_id)
await talk_handler(update, context, voice)
async def chat_handler(update: Update, context: ContextTypes.DEFAULT_TYPE, text: str):
response = ""
chat_id = update.effective_chat.id
lang = context.user_data.get('lang')
wait_message = get_random_wait_messages(
not_always=True,
lang=lang
)
if wait_message:
await context.bot.send_message(chat_id=chat_id, text=wait_message)
if lang == 'en':
response_en, history = chat(chat_id, text)
else:
response, response_en, history = bhashini_text_chat(chat_id,text, lang)
# if moderation_check(response):
# await context.bot.send_message(chat_id=chat_id, text=response)
# elif moderation_check(response_en):
# await context.bot.send_message(chat_id=chat_id, text=response_en)
# else:
# await context.bot.send_message(chat_id=chat_id, text="Please re-input.")
if guardrail(response) == True:
await context.bot.send_message(chat_id=chat_id, text=response)
elif guardrail(response_en) == True:
await context.bot.send_message(chat_id=chat_id, text=response_en)
else:
await context.bot.send_message(chat_id=chat_id, text="Sorry, I did not understand. Please input your query related to complaint filing only.")
async def talk_handler(update: Update, context: ContextTypes.DEFAULT_TYPE, voice):
lang = context.user_data.get('lang')
# getting audio file
audio_file = voice
if lang == 'en':
with tempfile.NamedTemporaryFile(suffix='.wav', delete=True) as temp_audio_file:
await audio_file.download_to_drive(custom_path=temp_audio_file.name)
chat_id = update.effective_chat.id
wait_message = get_random_wait_messages(
not_always=True,
lang=lang
)
if wait_message:
await context.bot.send_message(chat_id=chat_id, text=wait_message)
with open(temp_audio_file.name, "rb") as file:
audio_data = file.read()
audio_base64 = base64.b64encode(audio_data).decode('utf-8')
response_audio, assistant_message, history = audio_chat(
chat_id, audio_file=open(temp_audio_file.name, "rb")
)
response_audio.stream_to_file(temp_audio_file.name)
duration = get_duration_pydub(temp_audio_file.name)
await context.bot.send_audio(
chat_id=chat_id,
audio=open(temp_audio_file.name, "rb"),
duration=duration,
filename="response.wav",
performer="Mr. Nags",
)
await context.bot.send_message(
chat_id=chat_id, text=assistant_message
)
file.close()
else:
with tempfile.NamedTemporaryFile(suffix='.mp3', delete=True) as temp_audio_file:
await audio_file.download_to_drive(custom_path=temp_audio_file.name)
chat_id = update.effective_chat.id
wait_message = get_random_wait_messages(
not_always=True,
lang=lang
)
if wait_message:
await context.bot.send_message(chat_id=chat_id, text=wait_message)
with open(temp_audio_file.name, "rb") as file:
audio_data = file.read()
audio_base64 = base64.b64encode(audio_data).decode('utf-8')
response_audio, response, history = bhashini_audio_chat(
chat_id,
audio_file=audio_base64,
lang=lang
)
file_ = open(temp_audio_file.name, "wb")
file_.write(response_audio.content)
file_.close()
with open(temp_audio_file.name, "rb") as file:
duration = get_duration_pydub(temp_audio_file.name)
await context.bot.send_audio(
chat_id=chat_id,
audio=open(temp_audio_file.name, "rb"),
duration=duration,
filename="response.mp3",
performer="Mr. Nags",
)
await context.bot.send_message(
chat_id=chat_id, text=response
)
file_.close()
if __name__ == '__main__':
application = ApplicationBuilder().token(
token
).read_timeout(30).write_timeout(30).build()
#start_handler = CommandHandler('start', start)
language_handler_ = CommandHandler('set_language', language_handler)
chosen_language = CallbackQueryHandler(preferred_language_callback, pattern='[1-3]')
#application.add_handler(start_handler)
application.add_handler(language_handler_)
application.add_handler(chosen_language)
application.add_handler(
MessageHandler(
(filters.TEXT & (~filters.COMMAND)) | (filters.VOICE & (~filters.COMMAND)),
response_handler
)
)
application.run_polling()