randydev commited on
Commit
574e2a6
·
verified ·
1 Parent(s): b4bd92c

Upload chat.py

Browse files
Files changed (1) hide show
  1. chatbot/plugins/chat.py +87 -78
chatbot/plugins/chat.py CHANGED
@@ -51,6 +51,7 @@ async def geni_files_delete(name: str):
51
  return response.text
52
 
53
  spam_chats = []
 
54
 
55
  DISABLE_COMMAND = [
56
  "start",
@@ -82,10 +83,18 @@ I am ready to be a gemini bot developer
82
  @Client.on_callback_query(filters.regex("^cancels"))
83
  async def cancel_(client, callback_query):
84
  try:
85
- spam_chats.clear()
86
- return await callback_query.edit_message_text("ok cancel")
 
 
 
 
 
 
 
87
  except Exception as e:
88
- await callback_query.answer(f"Nothing cancel: {e}")
 
89
 
90
  @Client.on_message(
91
  ~filters.scheduled
@@ -252,12 +261,6 @@ async def chatbot_talk(client: Client, message: Message):
252
  return await ai_reply.edit_text(f"Error: {e}")
253
  except Exception as e:
254
  return await ai_reply.edit_text(f"Error: {e}")
255
-
256
- if message.document:
257
- is_check_plan = await db.is_gemini_plan(user_id=message.from_user.id)
258
- if not is_check_plan:
259
- return await message.reply_text("Only plan gemini")
260
- return await message.reply_text("maintenance soon")
261
  if message.audio or message.voice:
262
  await client.send_chat_action(message.chat.id, enums.ChatAction.UPLOAD_AUDIO)
263
  await asyncio.sleep(1.5)
@@ -321,76 +324,81 @@ async def chatbot_talk(client: Client, message: Message):
321
  return await ai_reply.edit_text(f"Error: {e}")
322
 
323
  if message.video:
324
- spam_chats.append(message.chat.id)
325
- await client.send_chat_action(message.chat.id, enums.ChatAction.UPLOAD_VIDEO)
326
- await asyncio.sleep(1.5)
327
- buttons = [
328
- [
329
- InlineKeyboardButton(
330
- text="Cancel",
331
- callback_data="cancels"
332
- ),
333
- ],
334
- ]
335
- if client.me.is_premium:
336
- ai_reply = await message.reply_text(f"{custom_loading}Processing...")
337
- else:
338
- ai_reply = await message.reply_text(
339
- f"Processing...\n\nYou can use stop everything",
340
- reply_markup=InlineKeyboardMarkup(buttons)
341
- )
342
- video_file_name = await message.download(file_name="newvideo.mp4")
343
- caption = message.caption or "What's this?"
344
- model = genai.GenerativeModel(
345
- model_name=model_,
346
- safety_settings={
347
- genai.types.HarmCategory.HARM_CATEGORY_HATE_SPEECH: genai.types.HarmBlockThreshold.BLOCK_NONE,
348
- genai.types.HarmCategory.HARM_CATEGORY_HARASSMENT: genai.types.HarmBlockThreshold.BLOCK_NONE,
349
- genai.types.HarmCategory.HARM_CATEGORY_SEXUALLY_EXPLICIT: genai.types.HarmBlockThreshold.BLOCK_NONE,
350
- genai.types.HarmCategory.HARM_CATEGORY_DANGEROUS_CONTENT: genai.types.HarmBlockThreshold.BLOCK_NONE,
351
- }
352
- )
353
- backup_chat = await db._get_chatbot_chat_from_db(message.from_user.id)
354
- backup_chat.append({"role": "user", "parts": [{"text": caption}]})
355
- if client.me.is_premium:
356
- await ai_reply.edit_text(f"{custom_loading}Uploading file..")
357
- else:
358
- await ai_reply.edit_text("Uploading file..")
359
- video_file = genai.upload_file(path=video_file_name)
360
- while video_file.state.name == "PROCESSING":
361
- await asyncio.sleep(10)
362
- video_file = genai.get_file(video_file.name)
363
- if video_file.state.name == "FAILED":
364
- return await ai_reply.edit_text(f"Error: {video_file.state.name}")
365
- try:
366
- await client.send_chat_action(message.chat.id, enums.ChatAction.TYPING)
367
- await asyncio.sleep(1.5)
368
- response = model.generate_content(
369
- [video_file, caption],
370
- request_options={"timeout": 600}
371
  )
372
- if len(response.text) > 4096:
373
- with open("chat.txt", "w+", encoding="utf8") as out_file:
374
- out_file.write(response.text)
375
- await message.reply_document(
376
- document="chat.txt",
377
- disable_notification=True
 
 
 
 
 
378
  )
379
- await ai_reply.delete()
380
- os.remove("chat.txt")
381
- else:
382
- await ai_reply.edit_text(response.text)
383
- backup_chat.append({"role": "model", "parts": [{"text": response.text}]})
384
- await db._update_chatbot_chat_in_db(message.from_user.id, backup_chat)
385
- await client.send_chat_action(message.chat.id, enums.ChatAction.CANCEL)
386
- video_file.delete()
387
- os.remove(video_file_name)
388
- return
389
- except InvalidArgument as e:
390
- return await ai_reply.edit_text(f"Error: {e}")
391
- except Exception as e:
392
- return await ai_reply.edit_text(f"Error: {e}")
393
-
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
394
  if message.text:
395
  await client.send_chat_action(message.chat.id, enums.ChatAction.TYPING)
396
  await asyncio.sleep(1.5)
@@ -428,4 +436,5 @@ async def chatbot_talk(client: Client, message: Message):
428
  await client.send_chat_action(message.chat.id, enums.ChatAction.CANCEL)
429
  return
430
  except Exception as e:
431
- return await message.reply_text(f"Error: {e}")
 
 
51
  return response.text
52
 
53
  spam_chats = []
54
+ running_tasks = {}
55
 
56
  DISABLE_COMMAND = [
57
  "start",
 
83
  @Client.on_callback_query(filters.regex("^cancels"))
84
  async def cancel_(client, callback_query):
85
  try:
86
+ chat_id = callback_query.message.chat.id
87
+ if chat_id in running_tasks:
88
+ running_tasks[chat_id].cancel()
89
+ del running_tasks[chat_id]
90
+ await callback_query.edit_message_text("✅ Processing canceled.")
91
+ else:
92
+ await callback_query.edit_message_text("⚠️ No active task to cancel.")
93
+ except asyncio.CancelledError:
94
+ await callback_query.edit_message_text("⚠️ Task already canceled.")
95
  except Exception as e:
96
+ await callback_query.answer(f"Nothing to cancel: {e}")
97
+
98
 
99
  @Client.on_message(
100
  ~filters.scheduled
 
261
  return await ai_reply.edit_text(f"Error: {e}")
262
  except Exception as e:
263
  return await ai_reply.edit_text(f"Error: {e}")
 
 
 
 
 
 
264
  if message.audio or message.voice:
265
  await client.send_chat_action(message.chat.id, enums.ChatAction.UPLOAD_AUDIO)
266
  await asyncio.sleep(1.5)
 
324
  return await ai_reply.edit_text(f"Error: {e}")
325
 
326
  if message.video:
327
+ chat_id = message.chat.id
328
+ async def task_video():
329
+ try:
330
+ spam_chats.append()
331
+ await client.send_chat_action(message.chat.id, enums.ChatAction.UPLOAD_VIDEO)
332
+ await asyncio.sleep(1.5)
333
+ buttons = [
334
+ [
335
+ InlineKeyboardButton(
336
+ text="Cancel",
337
+ callback_data="cancels"
338
+ ),
339
+ ],
340
+ ]
341
+ ai_reply = await message.reply_text(
342
+ f"Processing...\n\nYou can use stop everything",
343
+ reply_markup=InlineKeyboardMarkup(buttons)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
344
  )
345
+ await asyncio.sleep(5)
346
+ video_file_name = await message.download(file_name="newvideo.mp4")
347
+ caption = message.caption or "What's this?"
348
+ model = genai.GenerativeModel(
349
+ model_name=model_,
350
+ safety_settings={
351
+ genai.types.HarmCategory.HARM_CATEGORY_HATE_SPEECH: genai.types.HarmBlockThreshold.BLOCK_NONE,
352
+ genai.types.HarmCategory.HARM_CATEGORY_HARASSMENT: genai.types.HarmBlockThreshold.BLOCK_NONE,
353
+ genai.types.HarmCategory.HARM_CATEGORY_SEXUALLY_EXPLICIT: genai.types.HarmBlockThreshold.BLOCK_NONE,
354
+ genai.types.HarmCategory.HARM_CATEGORY_DANGEROUS_CONTENT: genai.types.HarmBlockThreshold.BLOCK_NONE,
355
+ }
356
  )
357
+ backup_chat = await db._get_chatbot_chat_from_db(message.from_user.id)
358
+ backup_chat.append({"role": "user", "parts": [{"text": caption}]})
359
+ await ai_reply.edit_text("Uploading file..")
360
+ video_file = genai.upload_file(path=video_file_name)
361
+ while video_file.state.name == "PROCESSING":
362
+ await asyncio.sleep(10)
363
+ video_file = genai.get_file(video_file.name)
364
+ if video_file.state.name == "FAILED":
365
+ return await ai_reply.edit_text(f"Error: {video_file.state.name}")
366
+ await client.send_chat_action(message.chat.id, enums.ChatAction.TYPING)
367
+ await asyncio.sleep(1.5)
368
+ response = model.generate_content(
369
+ [video_file, caption],
370
+ request_options={"timeout": 600}
371
+ )
372
+ if len(response.text) > 4096:
373
+ with open("chat.txt", "w+", encoding="utf8") as out_file:
374
+ out_file.write(response.text)
375
+ await message.reply_document(
376
+ document="chat.txt",
377
+ disable_notification=True
378
+ )
379
+ await ai_reply.delete()
380
+ os.remove("chat.txt")
381
+ else:
382
+ await ai_reply.edit_text(response.text)
383
+ backup_chat.append({"role": "model", "parts": [{"text": response.text}]})
384
+ await db._update_chatbot_chat_in_db(message.from_user.id, backup_chat)
385
+ await client.send_chat_action(message.chat.id, enums.ChatAction.CANCEL)
386
+ video_file.delete()
387
+ os.remove(video_file_name)
388
+ return
389
+ except asyncio.CancelledError:
390
+ await ai_reply.edit_text("✅ Video processed successfully!")
391
+ except InvalidArgument as e:
392
+ return await ai_reply.edit_text(f"Error: {e}")
393
+ except Exception as e:
394
+ return await ai_reply.edit_text(f"Error: {e}")
395
+ finally:
396
+ if chat_id in spam_chats:
397
+ spam_chats.remove(chat_id)
398
+ if chat_id in running_tasks:
399
+ del running_tasks[chat_id]
400
+ task = asyncio.create_task(task_video())
401
+ running_tasks[chat_id] = task
402
  if message.text:
403
  await client.send_chat_action(message.chat.id, enums.ChatAction.TYPING)
404
  await asyncio.sleep(1.5)
 
436
  await client.send_chat_action(message.chat.id, enums.ChatAction.CANCEL)
437
  return
438
  except Exception as e:
439
+ return await message.reply_text(f"Error: {e}")
440
+