randydev commited on
Commit
841dd86
·
verified ·
1 Parent(s): f1b0681

Update chatbot/plugins/chat.py

Browse files
Files changed (1) hide show
  1. chatbot/plugins/chat.py +1 -24
chatbot/plugins/chat.py CHANGED
@@ -31,22 +31,15 @@ from google.api_core.exceptions import InvalidArgument
31
  async def chatbot_talk(client: Client, message: Message):
32
  try:
33
  genai.configure(api_key=GOOGLE_API_KEY)
34
-
35
- # Handling Photo Messages
36
  if message.photo:
37
  try:
38
  file_path = await message.download()
39
  caption = message.caption or "What's this?"
40
  x = GeminiLatest(api_keys=GOOGLE_API_KEY)
41
-
42
- # Send initial processing message
43
  ai_reply = await message.reply_text("Processing...")
44
-
45
  backup_chat = await db._get_chatbot_chat_from_db(message.from_user.id)
46
  backup_chat.append({"role": "user", "parts": [{"text": caption}]})
47
-
48
  response_reads = x.get_response_image(caption, file_path)
49
-
50
  if len(response_reads) > 4096:
51
  with open("chat.txt", "w+", encoding="utf8") as out_file:
52
  out_file.write(response_reads)
@@ -58,10 +51,8 @@ async def chatbot_talk(client: Client, message: Message):
58
  os.remove("chat.txt")
59
  else:
60
  await ai_reply.edit_text(response_reads)
61
-
62
  backup_chat.append({"role": "model", "parts": [{"text": response_reads}]})
63
  await db._update_chatbot_chat_in_db(message.from_user.id, backup_chat)
64
-
65
  os.remove(file_path)
66
  except InvalidArgument as e:
67
  await ai_reply.edit_text(f"Error: {e}")
@@ -69,7 +60,6 @@ async def chatbot_talk(client: Client, message: Message):
69
  await ai_reply.edit_text(f"Error: {e}")
70
  return
71
 
72
- # Handling Audio or Voice Messages
73
  if message.audio or message.voice:
74
  try:
75
  ai_reply = await message.reply_text("Processing...")
@@ -86,18 +76,14 @@ async def chatbot_talk(client: Client, message: Message):
86
  )
87
  backup_chat = await db._get_chatbot_chat_from_db(message.from_user.id)
88
  backup_chat.append({"role": "user", "parts": [{"text": caption}]})
89
-
90
  await ai_reply.edit_text("Uploading file...")
91
  audio_file = genai.upload_file(path=audio_file_name)
92
-
93
  while audio_file.state.name == "PROCESSING":
94
  await asyncio.sleep(10)
95
  audio_file = genai.get_file(audio_file.name)
96
-
97
  if audio_file.state.name == "FAILED":
98
  await ai_reply.edit_text(f"Error: {audio_file.state.name}")
99
  return
100
-
101
  response = model.generate_content(
102
  [audio_file, caption],
103
  request_options={"timeout": 600}
@@ -125,7 +111,6 @@ async def chatbot_talk(client: Client, message: Message):
125
  await ai_reply.edit_text(f"Error: {e}")
126
  return
127
 
128
- # Handling Video Messages
129
  if message.video:
130
  try:
131
  ai_reply = await message.reply_text("Processing...")
@@ -142,14 +127,11 @@ async def chatbot_talk(client: Client, message: Message):
142
  )
143
  backup_chat = await db._get_chatbot_chat_from_db(message.from_user.id)
144
  backup_chat.append({"role": "user", "parts": [{"text": caption}]})
145
-
146
  await ai_reply.edit_text("Uploading file...")
147
  video_file = genai.upload_file(path=video_file_name)
148
-
149
  while video_file.state.name == "PROCESSING":
150
  await asyncio.sleep(10)
151
  video_file = genai.get_file(video_file.name)
152
-
153
  if video_file.state.name == "FAILED":
154
  await ai_reply.edit_text(f"Error: {video_file.state.name}")
155
  return
@@ -172,7 +154,6 @@ async def chatbot_talk(client: Client, message: Message):
172
 
173
  backup_chat.append({"role": "model", "parts": [{"text": response.text}]})
174
  await db._update_chatbot_chat_in_db(message.from_user.id, backup_chat)
175
-
176
  video_file.delete()
177
  os.remove(video_file_name)
178
  except InvalidArgument as e:
@@ -181,7 +162,6 @@ async def chatbot_talk(client: Client, message: Message):
181
  await ai_reply.edit_text(f"Error: {e}")
182
  return
183
 
184
- # Handling Text Messages
185
  if message.text:
186
  try:
187
  query = message.text.strip()
@@ -201,11 +181,9 @@ async def chatbot_talk(client: Client, message: Message):
201
  )
202
  backup_chat = await db._get_chatbot_chat_from_db(message.from_user.id)
203
  backup_chat.append({"role": "user", "parts": [{"text": query_base}]})
204
-
205
  chat_session = model_flash.start_chat(history=backup_chat)
206
  response_data = chat_session.send_message(query_base)
207
  output = response_data.text
208
-
209
  if len(output) > 4096:
210
  with open("chat.txt", "w+", encoding="utf8") as out_file:
211
  out_file.write(output)
@@ -216,8 +194,7 @@ async def chatbot_talk(client: Client, message: Message):
216
  os.remove("chat.txt")
217
  else:
218
  await message.reply_text(output)
219
-
220
  backup_chat.append({"role": "model", "parts": [{"text": output}]})
221
  await db._update_chatbot_chat_in_db(message.from_user.id, backup_chat)
222
  except Exception as e:
223
- await message.reply_text(str(e))
 
31
  async def chatbot_talk(client: Client, message: Message):
32
  try:
33
  genai.configure(api_key=GOOGLE_API_KEY)
 
 
34
  if message.photo:
35
  try:
36
  file_path = await message.download()
37
  caption = message.caption or "What's this?"
38
  x = GeminiLatest(api_keys=GOOGLE_API_KEY)
 
 
39
  ai_reply = await message.reply_text("Processing...")
 
40
  backup_chat = await db._get_chatbot_chat_from_db(message.from_user.id)
41
  backup_chat.append({"role": "user", "parts": [{"text": caption}]})
 
42
  response_reads = x.get_response_image(caption, file_path)
 
43
  if len(response_reads) > 4096:
44
  with open("chat.txt", "w+", encoding="utf8") as out_file:
45
  out_file.write(response_reads)
 
51
  os.remove("chat.txt")
52
  else:
53
  await ai_reply.edit_text(response_reads)
 
54
  backup_chat.append({"role": "model", "parts": [{"text": response_reads}]})
55
  await db._update_chatbot_chat_in_db(message.from_user.id, backup_chat)
 
56
  os.remove(file_path)
57
  except InvalidArgument as e:
58
  await ai_reply.edit_text(f"Error: {e}")
 
60
  await ai_reply.edit_text(f"Error: {e}")
61
  return
62
 
 
63
  if message.audio or message.voice:
64
  try:
65
  ai_reply = await message.reply_text("Processing...")
 
76
  )
77
  backup_chat = await db._get_chatbot_chat_from_db(message.from_user.id)
78
  backup_chat.append({"role": "user", "parts": [{"text": caption}]})
 
79
  await ai_reply.edit_text("Uploading file...")
80
  audio_file = genai.upload_file(path=audio_file_name)
 
81
  while audio_file.state.name == "PROCESSING":
82
  await asyncio.sleep(10)
83
  audio_file = genai.get_file(audio_file.name)
 
84
  if audio_file.state.name == "FAILED":
85
  await ai_reply.edit_text(f"Error: {audio_file.state.name}")
86
  return
 
87
  response = model.generate_content(
88
  [audio_file, caption],
89
  request_options={"timeout": 600}
 
111
  await ai_reply.edit_text(f"Error: {e}")
112
  return
113
 
 
114
  if message.video:
115
  try:
116
  ai_reply = await message.reply_text("Processing...")
 
127
  )
128
  backup_chat = await db._get_chatbot_chat_from_db(message.from_user.id)
129
  backup_chat.append({"role": "user", "parts": [{"text": caption}]})
 
130
  await ai_reply.edit_text("Uploading file...")
131
  video_file = genai.upload_file(path=video_file_name)
 
132
  while video_file.state.name == "PROCESSING":
133
  await asyncio.sleep(10)
134
  video_file = genai.get_file(video_file.name)
 
135
  if video_file.state.name == "FAILED":
136
  await ai_reply.edit_text(f"Error: {video_file.state.name}")
137
  return
 
154
 
155
  backup_chat.append({"role": "model", "parts": [{"text": response.text}]})
156
  await db._update_chatbot_chat_in_db(message.from_user.id, backup_chat)
 
157
  video_file.delete()
158
  os.remove(video_file_name)
159
  except InvalidArgument as e:
 
162
  await ai_reply.edit_text(f"Error: {e}")
163
  return
164
 
 
165
  if message.text:
166
  try:
167
  query = message.text.strip()
 
181
  )
182
  backup_chat = await db._get_chatbot_chat_from_db(message.from_user.id)
183
  backup_chat.append({"role": "user", "parts": [{"text": query_base}]})
 
184
  chat_session = model_flash.start_chat(history=backup_chat)
185
  response_data = chat_session.send_message(query_base)
186
  output = response_data.text
 
187
  if len(output) > 4096:
188
  with open("chat.txt", "w+", encoding="utf8") as out_file:
189
  out_file.write(output)
 
194
  os.remove("chat.txt")
195
  else:
196
  await message.reply_text(output)
 
197
  backup_chat.append({"role": "model", "parts": [{"text": output}]})
198
  await db._update_chatbot_chat_in_db(message.from_user.id, backup_chat)
199
  except Exception as e:
200
+ return await message.reply_text(str(e))