randydev commited on
Commit
c7eac1c
1 Parent(s): 9fba954

Update main.py

Browse files
Files changed (1) hide show
  1. main.py +77 -85
main.py CHANGED
@@ -2,8 +2,7 @@ import asyncio
2
  import logging
3
  import os
4
 
5
- from pyrogram import Client, filters
6
- from pyrogram import *
7
  from pyrogram.types import Message
8
 
9
  # Your other imports
@@ -56,14 +55,14 @@ async def chatbot_talk(client: Client, message: Message):
56
 
57
  # Handling Photo Messages
58
  if message.photo:
59
- file_path = await message.download()
60
- caption = message.caption or "What's this?"
61
- x = GeminiLatest(api_keys=GOOGLE_API_KEY)
62
-
63
- # Send initial processing message
64
- ai_reply = await message.reply_text("Processing...")
65
-
66
  try:
 
 
 
 
 
 
 
67
  backup_chat = await db._get_chatbot_chat_from_db(message.from_user.id)
68
  backup_chat.append({"role": "user", "parts": [{"text": caption}]})
69
 
@@ -85,43 +84,41 @@ async def chatbot_talk(client: Client, message: Message):
85
  await db._update_chatbot_chat_in_db(message.from_user.id, backup_chat)
86
 
87
  os.remove(file_path)
88
- return
89
  except InvalidArgument as e:
90
  await ai_reply.edit_text(f"Error: {e}")
91
- return
92
  except Exception as e:
93
  await ai_reply.edit_text(f"Error: {e}")
94
- return
95
 
96
  # Handling Audio or Voice Messages
97
  if message.audio or message.voice:
98
- ai_reply = await message.reply_text("Processing...")
99
- audio_file_name = await message.download()
100
- caption = message.caption or "What's this?"
101
- model = genai.GenerativeModel(
102
- model_name="gemini-1.5-flash",
103
- safety_settings={
104
- genai.types.HarmCategory.HARM_CATEGORY_HATE_SPEECH: genai.types.HarmBlockThreshold.BLOCK_NONE,
105
- genai.types.HarmCategory.HARM_CATEGORY_HARASSMENT: genai.types.HarmBlockThreshold.BLOCK_NONE,
106
- genai.types.HarmCategory.HARM_CATEGORY_SEXUALLY_EXPLICIT: genai.types.HarmBlockThreshold.BLOCK_NONE,
107
- genai.types.HarmCategory.HARM_CATEGORY_DANGEROUS_CONTENT: genai.types.HarmBlockThreshold.BLOCK_NONE,
108
- }
109
- )
110
- backup_chat = await db._get_chatbot_chat_from_db(message.from_user.id)
111
- backup_chat.append({"role": "user", "parts": [{"text": caption}]})
112
-
113
- ai_reply.edit_text("Uploading file...")
114
- audio_file = genai.upload_file(path=audio_file_name)
115
-
116
- while audio_file.state.name == "PROCESSING":
117
- await asyncio.sleep(10)
118
- audio_file = genai.get_file(audio_file.name)
119
-
120
- if audio_file.state.name == "FAILED":
121
- await ai_reply.edit_text(f"Error: {audio_file.state.name}")
122
- return
123
-
124
  try:
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
125
  response = model.generate_content(
126
  [audio_file, caption],
127
  request_options={"timeout": 600}
@@ -143,43 +140,41 @@ async def chatbot_talk(client: Client, message: Message):
143
 
144
  audio_file.delete()
145
  os.remove(audio_file_name)
146
- return
147
  except InvalidArgument as e:
148
  await ai_reply.edit_text(f"Error: {e}")
149
- return
150
  except Exception as e:
151
  await ai_reply.edit_text(f"Error: {e}")
152
- return
153
 
154
  # Handling Video Messages
155
  if message.video:
156
- ai_reply = await message.reply_text("Processing...")
157
- video_file_name = await message.download(file_name="newvideo.mp4")
158
- caption = message.caption or "What's this?"
159
- model = genai.GenerativeModel(
160
- model_name="gemini-1.5-pro",
161
- safety_settings={
162
- genai.types.HarmCategory.HARM_CATEGORY_HATE_SPEECH: genai.types.HarmBlockThreshold.BLOCK_NONE,
163
- genai.types.HarmCategory.HARM_CATEGORY_HARASSMENT: genai.types.HarmBlockThreshold.BLOCK_NONE,
164
- genai.types.HarmCategory.HARM_CATEGORY_SEXUALLY_EXPLICIT: genai.types.HarmBlockThreshold.BLOCK_NONE,
165
- genai.types.HarmCategory.HARM_CATEGORY_DANGEROUS_CONTENT: genai.types.HarmBlockThreshold.BLOCK_NONE,
166
- }
167
- )
168
- backup_chat = await db._get_chatbot_chat_from_db(message.from_user.id)
169
- backup_chat.append({"role": "user", "parts": [{"text": caption}]})
170
-
171
- ai_reply.edit_text("Uploading file...")
172
- video_file = genai.upload_file(path=video_file_name)
173
-
174
- while video_file.state.name == "PROCESSING":
175
- await asyncio.sleep(10)
176
- video_file = genai.get_file(video_file.name)
177
-
178
- if video_file.state.name == "FAILED":
179
- await ai_reply.edit_text(f"Error: {video_file.state.name}")
180
- return
181
-
182
  try:
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
183
  response = model.generate_content(
184
  [video_file, caption],
185
  request_options={"timeout": 600}
@@ -201,29 +196,27 @@ async def chatbot_talk(client: Client, message: Message):
201
 
202
  video_file.delete()
203
  os.remove(video_file_name)
204
- return
205
  except InvalidArgument as e:
206
  await ai_reply.edit_text(f"Error: {e}")
207
- return
208
  except Exception as e:
209
  await ai_reply.edit_text(f"Error: {e}")
210
- return
211
 
212
  # Handling Text Messages
213
  if message.text:
214
- query = message.text.strip()
215
- match = re.search(r"\b(Randy|Rendi)\b(.*)", query, flags=re.IGNORECASE)
216
- if match:
217
- rest_of_sentence = match.group(2).strip()
218
- query_base = rest_of_sentence if rest_of_sentence else query
219
- else:
220
- query_base = query
221
-
222
- parts = query.split(maxsplit=1)
223
- command = parts[0].lower()
224
- pic_query = parts[1].strip() if len(parts) > 1 else ""
225
-
226
  try:
 
 
 
 
 
 
 
 
 
 
 
 
227
  model_flash = genai.GenerativeModel(
228
  model_name="gemini-1.5-flash"
229
  )
@@ -258,9 +251,9 @@ async def main():
258
  LOGS.info("Connected to the database.")
259
  await client.start() # Start the Pyrogram client
260
  LOGS.info("Bot started successfully.")
261
- await idle() # Keep the bot running until interrupted
262
  LOGS.info("Bot stopping...")
263
- await client.stop() # Ensure the client stops gracefully
264
  except Exception as e:
265
  LOGS.error(f"Unexpected error: {e}")
266
 
@@ -272,4 +265,3 @@ if __name__ == "__main__":
272
  LOGS.info("Bot has been terminated by the user.")
273
  except Exception as e:
274
  LOGS.error(f"Unexpected error: {e}")
275
-
 
2
  import logging
3
  import os
4
 
5
+ from pyrogram import Client, filters, idle
 
6
  from pyrogram.types import Message
7
 
8
  # Your other imports
 
55
 
56
  # Handling Photo Messages
57
  if message.photo:
 
 
 
 
 
 
 
58
  try:
59
+ file_path = await message.download()
60
+ caption = message.caption or "What's this?"
61
+ x = GeminiLatest(api_keys=GOOGLE_API_KEY)
62
+
63
+ # Send initial processing message
64
+ ai_reply = await message.reply_text("Processing...")
65
+
66
  backup_chat = await db._get_chatbot_chat_from_db(message.from_user.id)
67
  backup_chat.append({"role": "user", "parts": [{"text": caption}]})
68
 
 
84
  await db._update_chatbot_chat_in_db(message.from_user.id, backup_chat)
85
 
86
  os.remove(file_path)
 
87
  except InvalidArgument as e:
88
  await ai_reply.edit_text(f"Error: {e}")
 
89
  except Exception as e:
90
  await ai_reply.edit_text(f"Error: {e}")
91
+ return
92
 
93
  # Handling Audio or Voice Messages
94
  if message.audio or message.voice:
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
95
  try:
96
+ ai_reply = await message.reply_text("Processing...")
97
+ audio_file_name = await message.download()
98
+ caption = message.caption or "What's this?"
99
+ model = genai.GenerativeModel(
100
+ model_name="gemini-1.5-flash",
101
+ safety_settings={
102
+ genai.types.HarmCategory.HARM_CATEGORY_HATE_SPEECH: genai.types.HarmBlockThreshold.BLOCK_NONE,
103
+ genai.types.HarmCategory.HARM_CATEGORY_HARASSMENT: genai.types.HarmBlockThreshold.BLOCK_NONE,
104
+ genai.types.HarmCategory.HARM_CATEGORY_SEXUALLY_EXPLICIT: genai.types.HarmBlockThreshold.BLOCK_NONE,
105
+ genai.types.HarmCategory.HARM_CATEGORY_DANGEROUS_CONTENT: genai.types.HarmBlockThreshold.BLOCK_NONE,
106
+ }
107
+ )
108
+ backup_chat = await db._get_chatbot_chat_from_db(message.from_user.id)
109
+ backup_chat.append({"role": "user", "parts": [{"text": caption}]})
110
+
111
+ await ai_reply.edit_text("Uploading file...")
112
+ audio_file = genai.upload_file(path=audio_file_name)
113
+
114
+ while audio_file.state.name == "PROCESSING":
115
+ await asyncio.sleep(10)
116
+ audio_file = genai.get_file(audio_file.name)
117
+
118
+ if audio_file.state.name == "FAILED":
119
+ await ai_reply.edit_text(f"Error: {audio_file.state.name}")
120
+ return
121
+
122
  response = model.generate_content(
123
  [audio_file, caption],
124
  request_options={"timeout": 600}
 
140
 
141
  audio_file.delete()
142
  os.remove(audio_file_name)
 
143
  except InvalidArgument as e:
144
  await ai_reply.edit_text(f"Error: {e}")
 
145
  except Exception as e:
146
  await ai_reply.edit_text(f"Error: {e}")
147
+ return
148
 
149
  # Handling Video Messages
150
  if message.video:
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
151
  try:
152
+ ai_reply = await message.reply_text("Processing...")
153
+ video_file_name = await message.download(file_name="newvideo.mp4")
154
+ caption = message.caption or "What's this?"
155
+ model = genai.GenerativeModel(
156
+ model_name="gemini-1.5-pro",
157
+ safety_settings={
158
+ genai.types.HarmCategory.HARM_CATEGORY_HATE_SPEECH: genai.types.HarmBlockThreshold.BLOCK_NONE,
159
+ genai.types.HarmCategory.HARM_CATEGORY_HARASSMENT: genai.types.HarmBlockThreshold.BLOCK_NONE,
160
+ genai.types.HarmCategory.HARM_CATEGORY_SEXUALLY_EXPLICIT: genai.types.HarmBlockThreshold.BLOCK_NONE,
161
+ genai.types.HarmCategory.HARM_CATEGORY_DANGEROUS_CONTENT: genai.types.HarmBlockThreshold.BLOCK_NONE,
162
+ }
163
+ )
164
+ backup_chat = await db._get_chatbot_chat_from_db(message.from_user.id)
165
+ backup_chat.append({"role": "user", "parts": [{"text": caption}]})
166
+
167
+ await ai_reply.edit_text("Uploading file...")
168
+ video_file = genai.upload_file(path=video_file_name)
169
+
170
+ while video_file.state.name == "PROCESSING":
171
+ await asyncio.sleep(10)
172
+ video_file = genai.get_file(video_file.name)
173
+
174
+ if video_file.state.name == "FAILED":
175
+ await ai_reply.edit_text(f"Error: {video_file.state.name}")
176
+ return
177
+
178
  response = model.generate_content(
179
  [video_file, caption],
180
  request_options={"timeout": 600}
 
196
 
197
  video_file.delete()
198
  os.remove(video_file_name)
 
199
  except InvalidArgument as e:
200
  await ai_reply.edit_text(f"Error: {e}")
 
201
  except Exception as e:
202
  await ai_reply.edit_text(f"Error: {e}")
203
+ return
204
 
205
  # Handling Text Messages
206
  if message.text:
 
 
 
 
 
 
 
 
 
 
 
 
207
  try:
208
+ query = message.text.strip()
209
+ match = re.search(r"\b(Randy|Rendi)\b(.*)", query, flags=re.IGNORECASE)
210
+ if match:
211
+ rest_of_sentence = match.group(2).strip()
212
+ query_base = rest_of_sentence if rest_of_sentence else query
213
+ else:
214
+ query_base = query
215
+
216
+ parts = query.split(maxsplit=1)
217
+ command = parts[0].lower()
218
+ pic_query = parts[1].strip() if len(parts) > 1 else ""
219
+
220
  model_flash = genai.GenerativeModel(
221
  model_name="gemini-1.5-flash"
222
  )
 
251
  LOGS.info("Connected to the database.")
252
  await client.start() # Start the Pyrogram client
253
  LOGS.info("Bot started successfully.")
254
+ await idle() # Keep the bot running until interrupted
255
  LOGS.info("Bot stopping...")
256
+ await client.stop() # Ensure the client stops gracefully
257
  except Exception as e:
258
  LOGS.error(f"Unexpected error: {e}")
259
 
 
265
  LOGS.info("Bot has been terminated by the user.")
266
  except Exception as e:
267
  LOGS.error(f"Unexpected error: {e}")