randydev commited on
Commit
f27110e
·
verified ·
1 Parent(s): 72a8c77

Update chatbot/plugins/chat.py

Browse files
Files changed (1) hide show
  1. chatbot/plugins/chat.py +46 -0
chatbot/plugins/chat.py CHANGED
@@ -482,10 +482,56 @@ async def chatbot_talk(client: Client, message: Message):
482
 
483
  if message.photo:
484
  file_photo = None
 
 
485
  try:
486
  caption = message.caption or "What this?"
487
  await client.send_chat_action(message.chat.id, enums.ChatAction.UPLOAD_PHOTO)
488
  await asyncio.sleep(1.5)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
489
  if re.findall(r"\b(pro:editimage)\b", caption, re.IGNORECASE):
490
  await db.backup_chatbot.update_one(
491
  {"user_id": message.from_user.id},
 
482
 
483
  if message.photo:
484
  file_photo = None
485
+ captions = ""
486
+ file_path = "gemini-native-image.png"
487
  try:
488
  caption = message.caption or "What this?"
489
  await client.send_chat_action(message.chat.id, enums.ChatAction.UPLOAD_PHOTO)
490
  await asyncio.sleep(1.5)
491
+
492
+ if re.findall(r"\b(picture)\b", caption, re.IGNORECASE):
493
+ try:
494
+ backup_chat = await db._get_chatbot_chat_from_db(message.from_user.id)
495
+ backup_chat.append({"role": "user", "parts": [{"text": caption}]})
496
+ images = Image.open(await message.download())
497
+ response = await gen.aio.models.generate_content(
498
+ model="gemini-2.0-flash-exp-image-generation",
499
+ contents=[str(caption), images],
500
+ config=types.GenerateContentConfig(
501
+ response_modalities=['TEXT', 'IMAGE']
502
+ )
503
+ )
504
+ for part in response.candidates[0].content.parts:
505
+ if part.text is not None:
506
+ captions += part.text
507
+ elif part.inline_data is not None:
508
+ image = Image.open(BytesIO(part.inline_data.data))
509
+ image.save(file_path)
510
+ keyboard_like = create_keyboard(user_id=message.from_user.id)
511
+ await message.reply_photo(
512
+ file_path,
513
+ caption=captions,
514
+ reply_markup=keyboard_like
515
+ )
516
+ await db.backup_chatbot.update_one(
517
+ {"user_id": message.from_user.id},
518
+ {"$set": {"translate_text": captions}},
519
+ upsert=True
520
+ )
521
+ backup_chat.append({"role": "model", "parts": [{"text": captions}]})
522
+ await db._update_chatbot_chat_in_db(message.from_user.id, backup_chat)
523
+ await client.send_chat_action(message.chat.id, enums.ChatAction.CANCEL)
524
+ return
525
+ except Exception as e:
526
+ LOGS.error(f"Error: Gemini Edit Image: {str(e)}")
527
+ return await message.reply_text("Server busy try again later")
528
+ finally:
529
+ if file_path:
530
+ try:
531
+ os.remove(file_path)
532
+ except:
533
+ pass
534
+
535
  if re.findall(r"\b(pro:editimage)\b", caption, re.IGNORECASE):
536
  await db.backup_chatbot.update_one(
537
  {"user_id": message.from_user.id},