File size: 1,418 Bytes
c8e8a00
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e9f5b40
c8e8a00
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
import gradio as gr
import requests
from io import BytesIO
from PIL import Image

# ضع مفتاح API الخاص بك هنا
REMOVE_BG_API_KEY = "e35sbLVzVUkbWALM3PZicdZ7"

# دالة لإزالة الخلفية من الصورة
def remove_background(image):
    try:
        # تحويل الصورة إلى bytes
        buffered = BytesIO()
        image.save(buffered, format="PNG")
        image_bytes = buffered.getvalue()

        # إرسال طلب إلى API الخاص بـ remove.bg
        response = requests.post(
            "https://api.remove.bg/v1.0/removebg",
            files={"image_file": image_bytes},
            data={"size": "auto"},
            headers={"X-Api-Key": REMOVE_BG_API_KEY},
        )

        if response.status_code == 200:
            # تحويل الاستجابة إلى صورة
            output_image = Image.open(BytesIO(response.content))
            return output_image
        else:
            # في حالة وجود خطأ
            return f"Error: {response.status_code} - {response.text}"
    except Exception as e:
        return str(e)

# إنشاء واجهة Gradio
interface = gr.Interface(
    fn=remove_background,
    inputs=gr.Image(type="pil"),
    outputs=gr.Image(type="pil"),
    title="إزالة الخلفية من الصور",
    description="remove bg"
)

# تشغيل التطبيق
if __name__ == "__main__":
    interface.launch()