Spaces:
Runtime error
Runtime error
Update bg_removal_engine.py
Browse files- bg_removal_engine.py +50 -24
bg_removal_engine.py
CHANGED
|
@@ -1,52 +1,78 @@
|
|
| 1 |
import telebot
|
|
|
|
|
|
|
| 2 |
from rembg import remove
|
| 3 |
from PIL import Image
|
| 4 |
-
import io
|
| 5 |
-
import os
|
| 6 |
|
| 7 |
-
# ال
|
| 8 |
TOKEN = '8238787092:AAHPlHZeE3woH2t-q14LUYhg8AynYD2aX1E'
|
| 9 |
-
|
| 10 |
bot = telebot.TeleBot(TOKEN)
|
| 11 |
|
| 12 |
-
# رسالة الترحيب
|
| 13 |
-
@bot.message_handler(commands=['start'
|
| 14 |
-
def
|
| 15 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
|
| 17 |
-
# معالجة الصور ا
|
| 18 |
@bot.message_handler(content_types=['photo'])
|
| 19 |
def handle_photo(message):
|
| 20 |
try:
|
| 21 |
-
# إ
|
| 22 |
-
|
| 23 |
|
| 24 |
-
# تحميل ال
|
| 25 |
file_info = bot.get_file(message.photo[-1].file_id)
|
| 26 |
downloaded_file = bot.download_file(file_info.file_path)
|
| 27 |
|
| 28 |
-
#
|
| 29 |
-
|
|
|
|
| 30 |
|
| 31 |
-
#
|
|
|
|
| 32 |
output_image = remove(input_image)
|
| 33 |
|
| 34 |
-
# تحويل النتيجة لملف
|
| 35 |
img_byte_arr = io.BytesIO()
|
| 36 |
output_image.save(img_byte_arr, format='PNG')
|
| 37 |
img_byte_arr.seek(0)
|
| 38 |
|
| 39 |
-
# إرسال النتيجة
|
| 40 |
-
bot.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 41 |
|
| 42 |
-
# مسح رسالة الانت
|
| 43 |
-
bot.delete_message(message.chat.id,
|
| 44 |
|
| 45 |
except Exception as e:
|
| 46 |
-
bot.reply_to(message, "وقع مشكل تقني
|
| 47 |
-
print(f"Error
|
| 48 |
|
| 49 |
-
# تشغيل البوت بشكل مستمر
|
| 50 |
if __name__ == "__main__":
|
| 51 |
-
print("ال
|
| 52 |
bot.infinity_polling()
|
|
|
|
| 1 |
import telebot
|
| 2 |
+
from telebot import types
|
| 3 |
+
import io
|
| 4 |
from rembg import remove
|
| 5 |
from PIL import Image
|
|
|
|
|
|
|
| 6 |
|
| 7 |
+
# التوكين ديالك (تأكد باللي هو الصحيح)
|
| 8 |
TOKEN = '8238787092:AAHPlHZeE3woH2t-q14LUYhg8AynYD2aX1E'
|
|
|
|
| 9 |
bot = telebot.TeleBot(TOKEN)
|
| 10 |
|
| 11 |
+
# 1. تخصيص رسالة الترحيب بأزرار
|
| 12 |
+
@bot.message_handler(commands=['start'])
|
| 13 |
+
def start(message):
|
| 14 |
+
# إنشاء أزرار الكيبورد (Reply Keyboard)
|
| 15 |
+
markup = types.ReplyKeyboardMarkup(resize_keyboard=True, row_width=2)
|
| 16 |
+
item1 = types.KeyboardButton("إزالة خلفية صورة 🖼️")
|
| 17 |
+
item2 = types.KeyboardButton("حول البوت ℹ️")
|
| 18 |
+
markup.add(item1, item2)
|
| 19 |
+
|
| 20 |
+
welcome_text = (
|
| 21 |
+
f"🚀 **أهلاً بك يا {message.from_user.first_name}!**\n\n"
|
| 22 |
+
"أنا بوت **Removeybg** الاحترافي. استعمل أحدث تقنيات الذكاء الاصطناعي "
|
| 23 |
+
"لعزل الصور بدقة عالية.\n\n"
|
| 24 |
+
"👇 **اضغط على الزر أسفله أو أرسل صورتك مباشرة!**"
|
| 25 |
+
)
|
| 26 |
+
bot.send_message(message.chat.id, welcome_text, parse_mode='Markdown', reply_markup=markup)
|
| 27 |
+
|
| 28 |
+
# 2. التعامل مع الأزرار أو النصوص
|
| 29 |
+
@bot.message_handler(func=lambda message: True)
|
| 30 |
+
def echo_all(message):
|
| 31 |
+
if message.text == "إزالة خلفية صورة 🖼️":
|
| 32 |
+
bot.reply_to(message, "تفضل! صيفط ليا التصويرة اللي بغيتي تحيد ليها الخلفية.")
|
| 33 |
+
elif message.text == "حول البوت ℹ️":
|
| 34 |
+
bot.reply_to(message, "هذا البوت تم تطويره بواسطة Jamal (Digital Entrepreneur) "
|
| 35 |
+
"باستخدام تقنية rembg والذكاء الاصطناعي.")
|
| 36 |
|
| 37 |
+
# 3. معالجة الصور باحترافية
|
| 38 |
@bot.message_handler(content_types=['photo'])
|
| 39 |
def handle_photo(message):
|
| 40 |
try:
|
| 41 |
+
# إرسال رسالة انتظار وتخزينها فمتغير باش نقدرو نعدلوها
|
| 42 |
+
status_msg = bot.reply_to(message, "⏳ جاري تحميل الصورة... [25%]")
|
| 43 |
|
| 44 |
+
# تحميل الملف
|
| 45 |
file_info = bot.get_file(message.photo[-1].file_id)
|
| 46 |
downloaded_file = bot.download_file(file_info.file_path)
|
| 47 |
|
| 48 |
+
# تحديث الحالة
|
| 49 |
+
bot.edit_message_text("⚙️ جاري العزل بالذكاء الاصطناعي... [60%]",
|
| 50 |
+
message.chat.id, status_msg.message_id)
|
| 51 |
|
| 52 |
+
# المعالجة بـ rembg
|
| 53 |
+
input_image = Image.open(io.BytesIO(downloaded_file))
|
| 54 |
output_image = remove(input_image)
|
| 55 |
|
| 56 |
+
# تحويل النتيجة لملف
|
| 57 |
img_byte_arr = io.BytesIO()
|
| 58 |
output_image.save(img_byte_arr, format='PNG')
|
| 59 |
img_byte_arr.seek(0)
|
| 60 |
|
| 61 |
+
# اللمسة الأخيرة: إرسال النتيجة ومسح رسالة الحالة
|
| 62 |
+
bot.edit_message_text("✅ تمت المعالجة بنجاح! [100%]",
|
| 63 |
+
message.chat.id, status_msg.message_id)
|
| 64 |
+
|
| 65 |
+
bot.send_document(message.chat.id, img_byte_arr,
|
| 66 |
+
visible_file_name='Removeybg_Result.png',
|
| 67 |
+
caption="تفضل صورتك بدون خلفية بدقة HD! 🚀")
|
| 68 |
|
| 69 |
+
# مسح رسالة الحالة بعد الإرسال بـ 2 ثواني (اختياري)
|
| 70 |
+
bot.delete_message(message.chat.id, status_msg.message_id)
|
| 71 |
|
| 72 |
except Exception as e:
|
| 73 |
+
bot.reply_to(message, "❌ وقع مشكل تقني. تأكد أن الملف صورة وحاول مرة أخرى.")
|
| 74 |
+
print(f"Error: {e}")
|
| 75 |
|
|
|
|
| 76 |
if __name__ == "__main__":
|
| 77 |
+
print("البوت الاحترافي متصل الآن...")
|
| 78 |
bot.infinity_polling()
|