Spaces:
Sleeping
Sleeping
Upload 4 files
Browse files- config.toml +2 -0
- main.py +137 -0
- requirements.txt +59 -0
- runtime.txt +1 -0
config.toml
ADDED
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
1 |
+
[server]
|
2 |
+
maxUploadSize = 5
|
main.py
ADDED
@@ -0,0 +1,137 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 sys
|
9 |
+
|
10 |
+
# Import allowed image extensions
|
11 |
+
IMAGE_EXTENSIONS = [
|
12 |
+
".bmp",
|
13 |
+
".dng",
|
14 |
+
".jpeg",
|
15 |
+
".jpg",
|
16 |
+
".mpo",
|
17 |
+
".png",
|
18 |
+
".tif",
|
19 |
+
".tiff",
|
20 |
+
".webp",
|
21 |
+
".pfm",
|
22 |
+
]
|
23 |
+
|
24 |
+
# Setup constants
|
25 |
+
OUTPUT_FOLDER = 'output_images'
|
26 |
+
DEVICE = "cuda" if torch.cuda.is_available() else "cpu"
|
27 |
+
|
28 |
+
# Create output folder if it doesn't exist
|
29 |
+
if not os.path.exists(OUTPUT_FOLDER):
|
30 |
+
os.makedirs(OUTPUT_FOLDER)
|
31 |
+
|
32 |
+
@st.cache_resource
|
33 |
+
def load_model():
|
34 |
+
"""Load the BiRefNet model with caching"""
|
35 |
+
try:
|
36 |
+
torch.set_float32_matmul_precision("high")
|
37 |
+
model = AutoModelForImageSegmentation.from_pretrained(
|
38 |
+
"ZhengPeng7/BiRefNet_lite",
|
39 |
+
trust_remote_code=True
|
40 |
+
)
|
41 |
+
return model.to(DEVICE)
|
42 |
+
except Exception as e:
|
43 |
+
st.error(f"Error loading model: {str(e)}")
|
44 |
+
raise
|
45 |
+
|
46 |
+
def process_image(image, model):
|
47 |
+
"""Process a single image and remove its background"""
|
48 |
+
# Define image transformation pipeline
|
49 |
+
transform_image = transforms.Compose([
|
50 |
+
transforms.Resize((1024, 1024)),
|
51 |
+
transforms.ToTensor(),
|
52 |
+
transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])
|
53 |
+
])
|
54 |
+
|
55 |
+
# Prepare image
|
56 |
+
image = image.convert("RGB")
|
57 |
+
original_size = image.size
|
58 |
+
input_tensor = transform_image(image).unsqueeze(0).to(DEVICE)
|
59 |
+
|
60 |
+
# Process image
|
61 |
+
with torch.no_grad():
|
62 |
+
preds = model(input_tensor)[-1].sigmoid().cpu()
|
63 |
+
pred = preds[0].squeeze()
|
64 |
+
mask = transforms.ToPILImage()(pred).resize(original_size)
|
65 |
+
|
66 |
+
# Apply mask to original image
|
67 |
+
result = image.copy()
|
68 |
+
result.putalpha(mask)
|
69 |
+
|
70 |
+
return result
|
71 |
+
|
72 |
+
def sidebar_data():
|
73 |
+
# Title and introduction
|
74 |
+
st.sidebar.title("Kenapa?")
|
75 |
+
st.sidebar.write("""
|
76 |
+
Saya nggak akan upload ini kalau web hapus background yang di post di IMPHNEN dijadiin berbayar.
|
77 |
+
""")
|
78 |
+
st.sidebar.markdown("Menggunakan model dari [Birefnet](https://github.com/ZhengPeng7/BiRefNet) versi lite yang ukurannya 170mb hihihi.")
|
79 |
+
|
80 |
+
# System information
|
81 |
+
st.sidebar.markdown("### SysInfo()")
|
82 |
+
st.sidebar.code(f"""
|
83 |
+
Python: {sys.version.split()[0]}
|
84 |
+
Torch: {torch.__version__}
|
85 |
+
Torchvision: {torch.__version__}
|
86 |
+
""")
|
87 |
+
|
88 |
+
|
89 |
+
def main():
|
90 |
+
st.title("Web untuk hapus background")
|
91 |
+
st.write("Upload gambar untuk diproses")
|
92 |
+
|
93 |
+
sidebar_data()
|
94 |
+
|
95 |
+
|
96 |
+
# File uploader with supported extensions
|
97 |
+
uploaded_file = st.file_uploader(
|
98 |
+
"Pilih gambar...",
|
99 |
+
type=[ext.replace(".", "") for ext in IMAGE_EXTENSIONS]
|
100 |
+
)
|
101 |
+
|
102 |
+
if uploaded_file is not None:
|
103 |
+
try:
|
104 |
+
# Load the model (will use cached version if already loaded)
|
105 |
+
model = load_model()
|
106 |
+
|
107 |
+
# Load and display original image
|
108 |
+
image = Image.open(io.BytesIO(uploaded_file.read()))
|
109 |
+
col1, col2 = st.columns(2)
|
110 |
+
|
111 |
+
with col1:
|
112 |
+
st.subheader("Gambar asli")
|
113 |
+
st.image(image, use_container_width=True) # Updated parameter
|
114 |
+
|
115 |
+
# Process image and display result
|
116 |
+
with st.spinner("Removing background..."):
|
117 |
+
result_image = process_image(image, model)
|
118 |
+
|
119 |
+
with col2:
|
120 |
+
st.subheader("Hasilnya")
|
121 |
+
st.image(result_image, use_container_width=True) # Updated parameter
|
122 |
+
|
123 |
+
# Add download button for processed image
|
124 |
+
buf = io.BytesIO()
|
125 |
+
result_image.save(buf, format='PNG')
|
126 |
+
st.download_button(
|
127 |
+
label="Download hasil",
|
128 |
+
data=buf.getvalue(),
|
129 |
+
file_name="processed_image.png",
|
130 |
+
mime="image/png"
|
131 |
+
)
|
132 |
+
|
133 |
+
except Exception as e:
|
134 |
+
st.error(f"An error occurred: {str(e)}")
|
135 |
+
|
136 |
+
if __name__ == "__main__":
|
137 |
+
main()
|
requirements.txt
ADDED
@@ -0,0 +1,59 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
altair==5.5.0
|
2 |
+
attrs==24.3.0
|
3 |
+
blinker==1.9.0
|
4 |
+
cachetools==5.5.0
|
5 |
+
certifi==2024.12.14
|
6 |
+
charset-normalizer==3.4.1
|
7 |
+
click==8.1.8
|
8 |
+
einops==0.8.0
|
9 |
+
filelock==3.13.1
|
10 |
+
fsspec==2024.2.0
|
11 |
+
gitdb==4.0.11
|
12 |
+
GitPython==3.1.43
|
13 |
+
huggingface-hub==0.27.0
|
14 |
+
idna==3.10
|
15 |
+
Jinja2==3.1.3
|
16 |
+
jsonschema==4.23.0
|
17 |
+
jsonschema-specifications==2024.10.1
|
18 |
+
kornia==0.7.4
|
19 |
+
kornia_rs==0.1.7
|
20 |
+
markdown-it-py==3.0.0
|
21 |
+
MarkupSafe==2.1.5
|
22 |
+
mdurl==0.1.2
|
23 |
+
mpmath==1.3.0
|
24 |
+
narwhals==1.19.1
|
25 |
+
networkx==3.2.1
|
26 |
+
numpy==1.26.3
|
27 |
+
packaging==24.2
|
28 |
+
pandas==2.2.3
|
29 |
+
pillow==10.2.0
|
30 |
+
protobuf==5.29.2
|
31 |
+
pyarrow==18.1.0
|
32 |
+
pydeck==0.9.1
|
33 |
+
Pygments==2.18.0
|
34 |
+
python-dateutil==2.9.0.post0
|
35 |
+
pytz==2024.2
|
36 |
+
PyYAML==6.0.2
|
37 |
+
referencing==0.35.1
|
38 |
+
regex==2024.11.6
|
39 |
+
requests==2.32.3
|
40 |
+
rich==13.9.4
|
41 |
+
rpds-py==0.22.3
|
42 |
+
safetensors==0.4.5
|
43 |
+
six==1.17.0
|
44 |
+
smmap==5.0.1
|
45 |
+
streamlit==1.41.1
|
46 |
+
sympy==1.13.1
|
47 |
+
tenacity==9.0.0
|
48 |
+
timm==1.0.12
|
49 |
+
tokenizers==0.21.0
|
50 |
+
toml==0.10.2
|
51 |
+
torch==2.4.1+cpu
|
52 |
+
torchvision==0.19.1+cpu
|
53 |
+
tornado==6.4.2
|
54 |
+
tqdm==4.67.1
|
55 |
+
transformers==4.47.1
|
56 |
+
typing_extensions==4.12.2
|
57 |
+
tzdata==2024.2
|
58 |
+
urllib3==2.3.0
|
59 |
+
watchdog==6.0.0
|
runtime.txt
ADDED
@@ -0,0 +1 @@
|
|
|
|
|
1 |
+
python=3.9
|