randydev commited on
Commit
8441d3e
·
verified ·
1 Parent(s): afe7d81

Update chatbot/plugins/chat.py

Browse files
Files changed (1) hide show
  1. chatbot/plugins/chat.py +67 -0
chatbot/plugins/chat.py CHANGED
@@ -64,6 +64,22 @@ async def geni_files_delete(name: str):
64
  return None
65
  return response.text
66
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
67
  CREDITS_DEVELOPER = """
68
  🗓️ 2 May 2025
69
 
@@ -239,6 +255,30 @@ async def deletemydata(client, callback):
239
  else:
240
  await callback.answer(delm, True)
241
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
242
  @Client.on_callback_query(filters.regex("^refreshch$"))
243
  async def reshch(client, callback):
244
  await callback.answer("Coming Soon", True)
@@ -443,6 +483,33 @@ async def chatbot_talk(client: Client, message: Message):
443
  try:
444
  await client.send_chat_action(message.chat.id, enums.ChatAction.UPLOAD_PHOTO)
445
  await asyncio.sleep(1.5)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
446
  caption = message.caption or "What this?"
447
  backup_chat = await db._get_chatbot_chat_from_db(message.from_user.id)
448
  backup_chat.append({"role": "user", "parts": [{"text": caption}]})
 
64
  return None
65
  return response.text
66
 
67
+ async def remove_bg_myapi(return_image):
68
+ with open(return_image, "rb") as image_file:
69
+ encoded_string = base64.b64encode(image_file.read()).decode("utf-8")
70
+ payload = {
71
+ "image_base64": encoded_string
72
+ }
73
+ output_bg = "output.png"
74
+ response = requests.post("https://randydev-meta-ai.hf.space/remove-bg-base64", json=payload)
75
+ if response.status_code == 200:
76
+ result_base64 = response.json().get("output_image_base64")
77
+ with open(output_bg, "wb") as out_file:
78
+ out_file.write(base64.b64decode(result_base64))
79
+ return output_bg
80
+ else:
81
+ return None
82
+
83
  CREDITS_DEVELOPER = """
84
  🗓️ 2 May 2025
85
 
 
255
  else:
256
  await callback.answer(delm, True)
257
 
258
+ @Client.on_callback_query(filters.regex("^removerbg_(\d+)$"))
259
+ async def remover_bg(client, callback):
260
+ user_id = int(callback.matches[0].group(1))
261
+ try:
262
+ data = await db.backup_chatbot.find_one({"user_id": int(user_id)})
263
+ if not data:
264
+ return await callback.answer("Can't found user", True)
265
+ get_response = data.get("background_file_id", None)
266
+ if not get_response:
267
+ return await callback.answer("Server busy try again later", True)
268
+ media = await client.download_media(get_response)
269
+ output_bg = await remove_bg_myapi(media)
270
+ if not output_bg:
271
+ return await callback.answer("Server busy try again later", True)
272
+ await callback.edit_message_text("Please wait Downloading Background Remover.....")
273
+ await callback.message.edit_media(
274
+ media=InputMediaDocument(media=output_bg)
275
+ )
276
+ os.remove(output_bg)
277
+ await callback.answer()
278
+ except Exception as e:
279
+ LOGS.error(f"remover_bg failed: {str(e)}")
280
+ await callback.answer("Server error", show_alert=True)
281
+
282
  @Client.on_callback_query(filters.regex("^refreshch$"))
283
  async def reshch(client, callback):
284
  await callback.answer("Coming Soon", True)
 
483
  try:
484
  await client.send_chat_action(message.chat.id, enums.ChatAction.UPLOAD_PHOTO)
485
  await asyncio.sleep(1.5)
486
+
487
+ if re.findall(r"\b(.editimage)\b", query_base, re.IGNORECASE):
488
+ await db.backup_chatbot.update_one(
489
+ {"user_id": message.from_user.id},
490
+ {"$set": {"background_file_id": message.photo.file_id}},
491
+ upsert=True
492
+ )
493
+ buttons = [
494
+ [
495
+ InlineKeyboardButton(
496
+ text="✂️ Background Remover",
497
+ callback_data=f"removerbg_{message.from_user.id}"
498
+ )
499
+ ],
500
+ [
501
+ InlineKeyboardButton(
502
+ text="❌ Cancel",
503
+ callback_data="closedd"
504
+ )
505
+ ]
506
+ ]
507
+ await message.reply_text(
508
+ "Are you sure you want to image this edit image?",
509
+ reply_markup=InlineKeyboardMarkup(buttons)
510
+ )
511
+ return
512
+
513
  caption = message.caption or "What this?"
514
  backup_chat = await db._get_chatbot_chat_from_db(message.from_user.id)
515
  backup_chat.append({"role": "user", "parts": [{"text": caption}]})