Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,46 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import requests
|
| 3 |
+
from io import BytesIO
|
| 4 |
+
from PIL import Image
|
| 5 |
+
|
| 6 |
+
# ضع مفتاح API الخاص بك هنا
|
| 7 |
+
REMOVE_BG_API_KEY = "e35sbLVzVUkbWALM3PZicdZ7"
|
| 8 |
+
|
| 9 |
+
# دالة لإزالة الخلفية من الصورة
|
| 10 |
+
def remove_background(image):
|
| 11 |
+
try:
|
| 12 |
+
# تحويل الصورة إلى bytes
|
| 13 |
+
buffered = BytesIO()
|
| 14 |
+
image.save(buffered, format="PNG")
|
| 15 |
+
image_bytes = buffered.getvalue()
|
| 16 |
+
|
| 17 |
+
# إرسال طلب إلى API الخاص بـ remove.bg
|
| 18 |
+
response = requests.post(
|
| 19 |
+
"https://api.remove.bg/v1.0/removebg",
|
| 20 |
+
files={"image_file": image_bytes},
|
| 21 |
+
data={"size": "auto"},
|
| 22 |
+
headers={"X-Api-Key": REMOVE_BG_API_KEY},
|
| 23 |
+
)
|
| 24 |
+
|
| 25 |
+
if response.status_code == 200:
|
| 26 |
+
# تحويل الاستجابة إلى صورة
|
| 27 |
+
output_image = Image.open(BytesIO(response.content))
|
| 28 |
+
return output_image
|
| 29 |
+
else:
|
| 30 |
+
# في حالة وجود خطأ
|
| 31 |
+
return f"Error: {response.status_code} - {response.text}"
|
| 32 |
+
except Exception as e:
|
| 33 |
+
return str(e)
|
| 34 |
+
|
| 35 |
+
# إنشاء واجهة Gradio
|
| 36 |
+
interface = gr.Interface(
|
| 37 |
+
fn=remove_background,
|
| 38 |
+
inputs=gr.Image(type="pil"),
|
| 39 |
+
outputs=gr.Image(type="pil"),
|
| 40 |
+
title="إزالة الخلفية من الصور",
|
| 41 |
+
description="ارفع صورة لإزالة الخلفية باستخدام API الخاص بـ remove.bg."
|
| 42 |
+
)
|
| 43 |
+
|
| 44 |
+
# تشغيل التطبيق
|
| 45 |
+
if __name__ == "__main__":
|
| 46 |
+
interface.launch()
|