shuvo108 commited on
Commit
eab630e
·
verified ·
1 Parent(s): e7f2ce0

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +59 -39
app.py CHANGED
@@ -1,47 +1,67 @@
1
  import gradio as gr
2
- import torch
3
- import numpy as np
4
  from PIL import Image
5
- from transformers import AutoModelForImageSegmentation
6
- from torchvision import transforms
7
 
8
- # Load RMBG-1.4 (BiRefNet)
9
- model = AutoModelForImageSegmentation.from_pretrained(
10
- "briaai/RMBG-1.4",
11
- trust_remote_code=True
12
- )
13
- model.eval()
14
 
15
- device = "cuda" if torch.cuda.is_available() else "cpu"
16
- model.to(device)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
17
 
18
- transform = transforms.Compose([
19
- transforms.ToTensor()
20
- ])
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
21
 
22
- def remove_bg(image: Image.Image):
23
- if image.mode != "RGB":
24
- image = image.convert("RGB")
 
 
25
 
26
- inp = transform(image).unsqueeze(0).to(device)
 
27
 
28
- with torch.no_grad():
29
- pred = model(inp)[0][0]
30
-
31
- mask = pred.sigmoid().cpu().numpy()
32
- mask = (mask > 0.5).astype(np.uint8) * 255
33
- mask = Image.fromarray(mask).resize(image.size)
34
-
35
- output = image.copy()
36
- output.putalpha(mask)
37
- return output
38
-
39
- demo = gr.Interface(
40
- fn=remove_bg,
41
- inputs=gr.Image(type="pil"),
42
- outputs=gr.Image(type="pil"),
43
- title="RMBG-1.4 Background Remover",
44
- description="BiRefNet based background removal"
45
- )
46
-
47
- demo.launch()
 
1
  import gradio as gr
2
+ from rembg import remove, new_session
 
3
  from PIL import Image
 
 
4
 
5
+ # . মডেল সেশন গ্লোবাল করা (একবারই লোড হবে)
6
+ print("ইঞ্জিন চালু হচ্ছে... মডেল লোড করা হচ্ছে।")
7
+ SESSION = new_session("birefnet-general")
 
 
 
8
 
9
+ def process_image(img):
10
+ if img is None:
11
+ return None
12
+ try:
13
+ # Alpha Matting অপ্টিমাইজড প্রসেসিং
14
+ output = remove(
15
+ img,
16
+ session=SESSION,
17
+ alpha_matting=True,
18
+ alpha_matting_foreground_threshold=240,
19
+ alpha_matting_background_threshold=10,
20
+ alpha_matting_erode_size=10,
21
+ post_process_mask=True
22
+ )
23
+ return output
24
+ except Exception as e:
25
+ print(f"Error: {e}")
26
+ return img
27
 
28
+ # . ইন্টারফেস ডিজাইন (Gradio 6.0 এর নিয়ম অনুযায়ী)
29
+ with gr.Blocks() as demo:
30
+ gr.HTML("""
31
+ <div style="text-align: center;">
32
+ <h1 style="color: #FF4B4B; margin-bottom: 0;">🚀 প্রো-গ্রেড এআই ব্যাকগ্রাউন্ড রিমুভার</h1>
33
+ <p style="color: #555;">BiRefNet মডেল ব্যবহার করে নিখুঁত স্বচ্ছ ছবি তৈরি করুন</p>
34
+ </div>
35
+ """)
36
+
37
+ with gr.Row():
38
+ with gr.Column(scale=1):
39
+ input_file = gr.Image(
40
+ label="ছবি আপলোড করুন",
41
+ type="pil",
42
+ interactive=True
43
+ )
44
+ submit_btn = gr.Button("ব্যাকগ্রাউন্ড সরান ✨", variant="primary")
45
+
46
+ with gr.Column(scale=1):
47
+ # এখানে 'show_download_button' এরর দিচ্ছিল, তাই সেটি বাদ দেওয়া হয়েছে
48
+ output_file = gr.Image(
49
+ label="স্বচ্ছ ফলাফল",
50
+ type="pil"
51
+ )
52
 
53
+ submit_btn.click(
54
+ fn=process_image,
55
+ inputs=input_file,
56
+ outputs=output_file
57
+ )
58
 
59
+ gr.Markdown("---")
60
+ gr.Markdown("ℹ️ **তথ্য:** এটি ১৬জিবি র‍্যাম অপ্টিমাইজড। প্রথমবার ছবি প্রসেস করতে মডেল লোড হতে কয়েক সেকেন্ড সময় নিতে পারে।")
61
 
62
+ # . লঞ্চ করার সময় থিম এবং সিএসএস পাস করা (নতুন নিয়ম)
63
+ if __name__ == "__main__":
64
+ demo.queue(max_size=10).launch(
65
+ theme=gr.themes.Soft(),
66
+ ssr_mode=False
67
+ )