randydev commited on
Commit
06d6831
·
verified ·
1 Parent(s): df4a135

Update chatbot/plugins/chat.py

Browse files
Files changed (1) hide show
  1. chatbot/plugins/chat.py +38 -1
chatbot/plugins/chat.py CHANGED
@@ -659,6 +659,7 @@ async def chatbot_talk(client: Client, message: Message):
659
  await asyncio.sleep(1.5)
660
  query = message.text.strip()
661
  query_base = query
 
662
  try:
663
  if query_base in ["/", "/help"]:
664
  await client.send_chat_action(message.chat.id, enums.ChatAction.CANCEL)
@@ -668,6 +669,42 @@ async def chatbot_talk(client: Client, message: Message):
668
  await message.reply_text(CREDITS_DEVELOPER)
669
  return
670
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
671
  if re.findall(r"\b(enabled:chatsystem)\b", query_base, re.IGNORECASE):
672
  await db.backup_chatbot.update_one(
673
  {"user_id": message.from_user.id},
@@ -726,7 +763,7 @@ async def chatbot_talk(client: Client, message: Message):
726
  for part in text_parts:
727
  await message.reply_text(part)
728
  else:
729
- keyboard_like = create_keyboard()
730
  await message.reply_text(
731
  response.text,
732
  disable_web_page_preview=True,
 
659
  await asyncio.sleep(1.5)
660
  query = message.text.strip()
661
  query_base = query
662
+ file_path = "gemini-native-image.png"
663
  try:
664
  if query_base in ["/", "/help"]:
665
  await client.send_chat_action(message.chat.id, enums.ChatAction.CANCEL)
 
669
  await message.reply_text(CREDITS_DEVELOPER)
670
  return
671
 
672
+ if re.findall(r"\b(image)\b", query_base, re.IGNORECASE):
673
+ try:
674
+ backup_chat = await db._get_chatbot_chat_from_db(message.from_user.id)
675
+ backup_chat.append({"role": "user", "parts": [{"text": query_base}]})
676
+ response = await gen.aio.models.generate_content(
677
+ model="gemini-2.0-flash-exp-image-generation",
678
+ contents=query_base,
679
+ config=ty.GenerateContentConfig(
680
+ response_modalities=['TEXT', 'IMAGE']
681
+ )
682
+ )
683
+ for part in response.candidates[0].content.parts:
684
+ if part.text is not None:
685
+ LOGS.info(f"Gemini Image: {part.text}")
686
+ elif part.inline_data is not None:
687
+ image = Image.open(BytesIO((part.inline_data.data)))
688
+ image.save(file_path)
689
+ keyboard_like = create_keyboard(user_id=message.from_user.id)
690
+ await message.reply_photo(
691
+ file_path,
692
+ reply_markup=keyboard_like
693
+ )
694
+ backup_chat.append({"role": "model", "parts": [{"text": "Nothing test image"}]})
695
+ await db._update_chatbot_chat_in_db(message.from_user.id, backup_chat)
696
+ await client.send_chat_action(message.chat.id, enums.ChatAction.CANCEL)
697
+ return
698
+ except Exception as e:
699
+ LOGS.error(f"Error: Gemini Image: {str(e)}")
700
+ return await message.reply_text("Server busy try again later")
701
+ finally:
702
+ if file_path:
703
+ try:
704
+ os.remove(file_path)
705
+ except:
706
+ pass
707
+
708
  if re.findall(r"\b(enabled:chatsystem)\b", query_base, re.IGNORECASE):
709
  await db.backup_chatbot.update_one(
710
  {"user_id": message.from_user.id},
 
763
  for part in text_parts:
764
  await message.reply_text(part)
765
  else:
766
+ keyboard_like = create_keyboard(user_id=message.from_user.id)
767
  await message.reply_text(
768
  response.text,
769
  disable_web_page_preview=True,