remove-bg / app.py
yyasso's picture
Update app.py
e9f5b40 verified
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()