Spaces:
Sleeping
Sleeping
Upload 4 files
Browse files- README.md +5 -6
- app.py +214 -0
- gitattributes.txt +35 -0
- requirements.txt +9 -0
README.md
CHANGED
|
@@ -1,13 +1,12 @@
|
|
| 1 |
---
|
| 2 |
-
title:
|
| 3 |
-
emoji:
|
| 4 |
colorFrom: gray
|
| 5 |
colorTo: purple
|
| 6 |
-
sdk:
|
| 7 |
-
sdk_version:
|
| 8 |
app_file: app.py
|
| 9 |
pinned: false
|
| 10 |
-
short_description: 'Crystal AI Tools '
|
| 11 |
---
|
| 12 |
|
| 13 |
-
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|
|
|
|
| 1 |
---
|
| 2 |
+
title: Back Ground Removal
|
| 3 |
+
emoji: 📊
|
| 4 |
colorFrom: gray
|
| 5 |
colorTo: purple
|
| 6 |
+
sdk: streamlit
|
| 7 |
+
sdk_version: 1.51.0
|
| 8 |
app_file: app.py
|
| 9 |
pinned: false
|
|
|
|
| 10 |
---
|
| 11 |
|
| 12 |
+
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|
app.py
ADDED
|
@@ -0,0 +1,214 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
from PIL import Image
|
| 3 |
+
import torch
|
| 4 |
+
from torchvision import transforms
|
| 5 |
+
from transformers import AutoModelForImageSegmentation
|
| 6 |
+
import io
|
| 7 |
+
import os
|
| 8 |
+
import requests
|
| 9 |
+
from io import BytesIO
|
| 10 |
+
|
| 11 |
+
# --- 1. إعدادات الصفحة والعلامة التجارية ---
|
| 12 |
+
st.set_page_config(
|
| 13 |
+
page_title="SkyData - Background Removal",
|
| 14 |
+
layout="wide"
|
| 15 |
+
)
|
| 16 |
+
|
| 17 |
+
# --- 2. قاموس الترجمة (عربي / إنجليزي) ---
|
| 18 |
+
localization = {
|
| 19 |
+
'en': {
|
| 20 |
+
'brand_title': "",
|
| 21 |
+
'lang_select': "Select Language",
|
| 22 |
+
'input_method': "Select Input Method",
|
| 23 |
+
'tab_upload': "Image Upload",
|
| 24 |
+
'tab_url': "URL Input",
|
| 25 |
+
'tab_file': "File Output",
|
| 26 |
+
'upload_prompt': "Upload an image",
|
| 27 |
+
'upload_prompt_file': "Upload an image for file output",
|
| 28 |
+
'url_prompt': "Paste an image URL",
|
| 29 |
+
'processed_caption': "Processed Image",
|
| 30 |
+
'download_button': "Download PNG",
|
| 31 |
+
'error_fetching': "Error fetching image from URL:",
|
| 32 |
+
'error_processing': "Error processing image:",
|
| 33 |
+
'generic_error': "An error occurred:",
|
| 34 |
+
'page_title': "SkyData - Background Removal",
|
| 35 |
+
'spinner_text': "Processing image, please wait..."
|
| 36 |
+
},
|
| 37 |
+
'ar': {
|
| 38 |
+
'brand_title': "",
|
| 39 |
+
'lang_select': "اختر اللغة",
|
| 40 |
+
'input_method': "اختر طريقة الإدخال",
|
| 41 |
+
'tab_upload': "رفع صورة",
|
| 42 |
+
'tab_url': "رابط صورة",
|
| 43 |
+
'tab_file': "تصدير ملف",
|
| 44 |
+
'upload_prompt': "ارفع صورة",
|
| 45 |
+
'upload_prompt_file': "ارفع صورة لتصدير الملف",
|
| 46 |
+
'url_prompt': "الصق رابط الصورة",
|
| 47 |
+
'processed_caption': "الصورة المعالجة",
|
| 48 |
+
'download_button': "تحميل بصيغة PNG",
|
| 49 |
+
'error_fetching': "خطأ في جلب الصورة من الرابط:",
|
| 50 |
+
'error_processing': "خطأ في معالجة الصورة:",
|
| 51 |
+
'generic_error': "حدث خطأ:",
|
| 52 |
+
'page_title': "SkyData - أداة إزالة الخلفية",
|
| 53 |
+
'spinner_text': "جاري معالجة الصورة، يرجى الانتظار..."
|
| 54 |
+
}
|
| 55 |
+
}
|
| 56 |
+
|
| 57 |
+
# --- 3. تحميل النموذج ---
|
| 58 |
+
|
| 59 |
+
torch.set_float32_matmul_precision(["high", "highest"][0])
|
| 60 |
+
|
| 61 |
+
use_cuda = torch.cuda.is_available()
|
| 62 |
+
device = "cuda" if use_cuda else "cpu"
|
| 63 |
+
|
| 64 |
+
@st.cache_resource
|
| 65 |
+
def load_model():
|
| 66 |
+
model = AutoModelForImageSegmentation.from_pretrained("ZhengPeng7/BiRefNet", trust_remote_code=True)
|
| 67 |
+
model.to(device)
|
| 68 |
+
if use_cuda:
|
| 69 |
+
model = model.half()
|
| 70 |
+
return model
|
| 71 |
+
|
| 72 |
+
birefnet = load_model()
|
| 73 |
+
|
| 74 |
+
transform_image = transforms.Compose([
|
| 75 |
+
transforms.Resize((1024, 1024)), # الدقة العالية
|
| 76 |
+
transforms.ToTensor(),
|
| 77 |
+
transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225]),
|
| 78 |
+
])
|
| 79 |
+
|
| 80 |
+
# --- 4. دالة المعالجة الرئيسية (معدلة) ---
|
| 81 |
+
@st.cache_data
|
| 82 |
+
def process(image):
|
| 83 |
+
# المعالجة بالذكاء الاصطناعي
|
| 84 |
+
image_size = image.size
|
| 85 |
+
input_images = transform_image(image).unsqueeze(0).to(device)
|
| 86 |
+
if use_cuda:
|
| 87 |
+
input_images = input_images.half()
|
| 88 |
+
|
| 89 |
+
with torch.no_grad():
|
| 90 |
+
preds = birefnet(input_images)[-1].sigmoid().cpu()
|
| 91 |
+
|
| 92 |
+
pred = preds[0].squeeze()
|
| 93 |
+
pred_pil = transforms.ToPILImage()(pred)
|
| 94 |
+
mask = pred_pil.resize(image_size)
|
| 95 |
+
|
| 96 |
+
# تطبيق القناع
|
| 97 |
+
image.putalpha(mask) # تعديل الصورة لتصبح شفافة
|
| 98 |
+
|
| 99 |
+
# تحويل الصورة إلى bytes للتحميل
|
| 100 |
+
img_bytes = io.BytesIO()
|
| 101 |
+
image.save(img_bytes, format="PNG") # 'image' هي الآن الصورة الشفافة
|
| 102 |
+
img_bytes = img_bytes.getvalue()
|
| 103 |
+
|
| 104 |
+
# إرجاع الصورة (للعرض) و الـ bytes (للتحميل)
|
| 105 |
+
return image, img_bytes
|
| 106 |
+
|
| 107 |
+
# --- 5. واجهة التطبيق (Sidebar) ---
|
| 108 |
+
|
| 109 |
+
LOGO_URL = "https://i.ibb.co/v4vwvcGq/skydatafull.webp"
|
| 110 |
+
st.sidebar.image(LOGO_URL)
|
| 111 |
+
|
| 112 |
+
lang_choice = st.sidebar.radio(
|
| 113 |
+
label="Select Language / اختر اللغة",
|
| 114 |
+
options=["English", "العربية"],
|
| 115 |
+
horizontal=True
|
| 116 |
+
)
|
| 117 |
+
lang_code = 'ar' if lang_choice == 'العربية' else 'en'
|
| 118 |
+
loc = localization[lang_code]
|
| 119 |
+
|
| 120 |
+
if lang_code == 'ar':
|
| 121 |
+
st.markdown(
|
| 122 |
+
"""
|
| 123 |
+
<style>
|
| 124 |
+
html, body, [data-testid="stSidebar"], [data-testid="stHeader"] {
|
| 125 |
+
direction: rtl !important;
|
| 126 |
+
}
|
| 127 |
+
[data-testid="stSidebar"] [data-testid="stRadio"] div[role="radiogroup"] {
|
| 128 |
+
flex-direction: row-reverse;
|
| 129 |
+
}
|
| 130 |
+
</style>
|
| 131 |
+
""",
|
| 132 |
+
unsafe_allow_html=True
|
| 133 |
+
)
|
| 134 |
+
|
| 135 |
+
# --- 6. واجهة التطبيق الرئيسية (Main App) ---
|
| 136 |
+
|
| 137 |
+
st.sidebar.title(loc['brand_title'])
|
| 138 |
+
selected_tab = st.sidebar.radio(loc['input_method'], [loc['tab_upload'], loc['tab_url'], loc['tab_file']])
|
| 139 |
+
st.title(loc['page_title'])
|
| 140 |
+
|
| 141 |
+
# --- تم التعديل: إضافة زر التحميل لكل التبويبات ---
|
| 142 |
+
|
| 143 |
+
if selected_tab == loc['tab_upload']:
|
| 144 |
+
uploaded_file = st.file_uploader(loc['upload_prompt'], type=["jpg", "jpeg", "png"])
|
| 145 |
+
if uploaded_file is not None:
|
| 146 |
+
try:
|
| 147 |
+
image = Image.open(uploaded_file).convert("RGB")
|
| 148 |
+
with st.spinner(loc['spinner_text']):
|
| 149 |
+
processed_image, file_bytes = process(image) # <-- تم التعديل
|
| 150 |
+
|
| 151 |
+
st.image(processed_image, caption=loc['processed_caption'])
|
| 152 |
+
|
| 153 |
+
# --- تمت الإضافة: زر التحميل ---
|
| 154 |
+
st.download_button(
|
| 155 |
+
label=loc['download_button'],
|
| 156 |
+
data=file_bytes,
|
| 157 |
+
file_name=f"{uploaded_file.name.rsplit('.', 1)[0]}.png",
|
| 158 |
+
mime="image/png",
|
| 159 |
+
)
|
| 160 |
+
except Exception as e:
|
| 161 |
+
st.error(f"{loc['generic_error']} {e}")
|
| 162 |
+
|
| 163 |
+
|
| 164 |
+
elif selected_tab == loc['tab_url']:
|
| 165 |
+
image_url = st.text_input(loc['url_prompt'])
|
| 166 |
+
if image_url:
|
| 167 |
+
try:
|
| 168 |
+
response = requests.get(image_url, stream=True)
|
| 169 |
+
response.raise_for_status()
|
| 170 |
+
image = Image.open(BytesIO(response.content)).convert("RGB")
|
| 171 |
+
|
| 172 |
+
with st.spinner(loc['spinner_text']):
|
| 173 |
+
processed_image, file_bytes = process(image) # <-- تم التعديل
|
| 174 |
+
|
| 175 |
+
st.image(processed_image, caption=loc['processed_caption'])
|
| 176 |
+
|
| 177 |
+
# --- تمت الإضافة: زر التحميل ---
|
| 178 |
+
try: # محاولة استخراج اسم الملف من الرابط
|
| 179 |
+
file_name = image_url.split('/')[-1].rsplit('.', 1)[0] + ".png"
|
| 180 |
+
except Exception:
|
| 181 |
+
file_name = "processed_image.png"
|
| 182 |
+
|
| 183 |
+
st.download_button(
|
| 184 |
+
label=loc['download_button'],
|
| 185 |
+
data=file_bytes,
|
| 186 |
+
file_name=file_name,
|
| 187 |
+
mime="image/png",
|
| 188 |
+
)
|
| 189 |
+
except requests.exceptions.RequestException as e:
|
| 190 |
+
st.error(f"{loc['error_fetching']} {e}")
|
| 191 |
+
except Exception as e:
|
| 192 |
+
st.error(f"{loc['error_processing']} {e}")
|
| 193 |
+
|
| 194 |
+
|
| 195 |
+
elif selected_tab == loc['tab_file']:
|
| 196 |
+
uploaded_file = st.file_uploader(loc['upload_prompt_file'], type=["jpg", "jpeg", "png"])
|
| 197 |
+
if uploaded_file is not None:
|
| 198 |
+
try: # <-- تم نقل معالجة الأخطاء هنا
|
| 199 |
+
image = Image.open(uploaded_file).convert("RGB")
|
| 200 |
+
|
| 201 |
+
with st.spinner(loc['spinner_text']):
|
| 202 |
+
processed_image, file_bytes = process(image) # <-- تم التعديل
|
| 203 |
+
|
| 204 |
+
st.image(processed_image, caption=loc['processed_caption'])
|
| 205 |
+
|
| 206 |
+
# زر التحميل موجود هنا أصلاً
|
| 207 |
+
st.download_button(
|
| 208 |
+
label=loc['download_button'],
|
| 209 |
+
data=file_bytes,
|
| 210 |
+
file_name=f"{uploaded_file.name.rsplit('.', 1)[0]}.png",
|
| 211 |
+
mime="image/png",
|
| 212 |
+
)
|
| 213 |
+
except Exception as e:
|
| 214 |
+
st.error(f"{loc['generic_error']} {e}")
|
gitattributes.txt
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
*.7z filter=lfs diff=lfs merge=lfs -text
|
| 2 |
+
*.arrow filter=lfs diff=lfs merge=lfs -text
|
| 3 |
+
*.bin filter=lfs diff=lfs merge=lfs -text
|
| 4 |
+
*.bz2 filter=lfs diff=lfs merge=lfs -text
|
| 5 |
+
*.ckpt filter=lfs diff=lfs merge=lfs -text
|
| 6 |
+
*.ftz filter=lfs diff=lfs merge=lfs -text
|
| 7 |
+
*.gz filter=lfs diff=lfs merge=lfs -text
|
| 8 |
+
*.h5 filter=lfs diff=lfs merge=lfs -text
|
| 9 |
+
*.joblib filter=lfs diff=lfs merge=lfs -text
|
| 10 |
+
*.lfs.* filter=lfs diff=lfs merge=lfs -text
|
| 11 |
+
*.mlmodel filter=lfs diff=lfs merge=lfs -text
|
| 12 |
+
*.model filter=lfs diff=lfs merge=lfs -text
|
| 13 |
+
*.msgpack filter=lfs diff=lfs merge=lfs -text
|
| 14 |
+
*.npy filter=lfs diff=lfs merge=lfs -text
|
| 15 |
+
*.npz filter=lfs diff=lfs merge=lfs -text
|
| 16 |
+
*.onnx filter=lfs diff=lfs merge=lfs -text
|
| 17 |
+
*.ot filter=lfs diff=lfs merge=lfs -text
|
| 18 |
+
*.parquet filter=lfs diff=lfs merge=lfs -text
|
| 19 |
+
*.pb filter=lfs diff=lfs merge=lfs -text
|
| 20 |
+
*.pickle filter=lfs diff=lfs merge=lfs -text
|
| 21 |
+
*.pkl filter=lfs diff=lfs merge=lfs -text
|
| 22 |
+
*.pt filter=lfs diff=lfs merge=lfs -text
|
| 23 |
+
*.pth filter=lfs diff=lfs merge=lfs -text
|
| 24 |
+
*.rar filter=lfs diff=lfs merge=lfs -text
|
| 25 |
+
*.safetensors filter=lfs diff=lfs merge=lfs -text
|
| 26 |
+
saved_model/**/* filter=lfs diff=lfs merge=lfs -text
|
| 27 |
+
*.tar.* filter=lfs diff=lfs merge=lfs -text
|
| 28 |
+
*.tar filter=lfs diff=lfs merge=lfs -text
|
| 29 |
+
*.tflite filter=lfs diff=lfs merge=lfs -text
|
| 30 |
+
*.tgz filter=lfs diff=lfs merge=lfs -text
|
| 31 |
+
*.wasm filter=lfs diff=lfs merge=lfs -text
|
| 32 |
+
*.xz filter=lfs diff=lfs merge=lfs -text
|
| 33 |
+
*.zip filter=lfs diff=lfs merge=lfs -text
|
| 34 |
+
*.zst filter=lfs diff=lfs merge=lfs -text
|
| 35 |
+
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
requirements.txt
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
Pillow==11.1.0
|
| 2 |
+
Requests==2.32.3
|
| 3 |
+
streamlit==1.42.0
|
| 4 |
+
torch==2.6.0
|
| 5 |
+
torchvision==0.21.0
|
| 6 |
+
transformers==4.48.3
|
| 7 |
+
einops>=0.4.0
|
| 8 |
+
kornia>=0.6.0
|
| 9 |
+
timm>=0.6.0
|