Update apptest.py
Browse files- apptest.py +29 -77
apptest.py
CHANGED
|
@@ -6,42 +6,51 @@ from torchvision import transforms
|
|
| 6 |
from PIL import Image
|
| 7 |
import timm
|
| 8 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
# --- 1. 初始化模型 ---
|
| 10 |
model_id = "briaai/RMBG-2.0"
|
| 11 |
print(f"正在載入模型: {model_id} ...")
|
| 12 |
|
| 13 |
hf_token = os.getenv("HF_TOKEN")
|
|
|
|
| 14 |
if not hf_token:
|
| 15 |
print("⚠️ 警告: 未偵測到 HF_TOKEN")
|
| 16 |
|
| 17 |
try:
|
| 18 |
model = AutoModelForImageSegmentation.from_pretrained(
|
| 19 |
-
model_id,
|
|
|
|
|
|
|
| 20 |
)
|
| 21 |
-
device = torch.device("cpu")
|
| 22 |
model.to(device)
|
| 23 |
model.eval()
|
| 24 |
print("✅ 模型載入成功!")
|
| 25 |
except Exception as e:
|
| 26 |
print(f"❌ 模型載入失敗: {e}")
|
| 27 |
|
| 28 |
-
# --- 2. 圖像處理 ---
|
| 29 |
def process_image(input_image):
|
| 30 |
if input_image is None:
|
| 31 |
return None
|
| 32 |
-
|
| 33 |
image_size = (1024, 1024)
|
| 34 |
transform_image = transforms.Compose([
|
| 35 |
transforms.Resize(image_size),
|
| 36 |
transforms.ToTensor(),
|
| 37 |
transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])
|
| 38 |
])
|
| 39 |
-
|
| 40 |
input_images = transform_image(input_image).unsqueeze(0).to(device)
|
| 41 |
-
|
| 42 |
with torch.no_grad():
|
| 43 |
preds = model(input_images)[-1].sigmoid().cpu()
|
| 44 |
-
|
| 45 |
pred = preds[0].squeeze()
|
| 46 |
pred_pil = transforms.ToPILImage()(pred)
|
| 47 |
mask = pred_pil.resize(input_image.size)
|
|
@@ -49,78 +58,21 @@ def process_image(input_image):
|
|
| 49 |
image.putalpha(mask)
|
| 50 |
return image
|
| 51 |
|
| 52 |
-
# --- 3.
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
/* 全域字體與背景 */
|
| 56 |
-
.gradio-container {
|
| 57 |
-
font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif !important;
|
| 58 |
-
background-color: #f0f2f5 !important;
|
| 59 |
-
}
|
| 60 |
-
|
| 61 |
-
/* 隱藏 Footer */
|
| 62 |
-
footer {display: none !important;}
|
| 63 |
-
|
| 64 |
-
/* 標題美化 */
|
| 65 |
-
h1 {
|
| 66 |
-
text-align: center;
|
| 67 |
-
color: #333;
|
| 68 |
-
margin-bottom: 20px;
|
| 69 |
-
}
|
| 70 |
-
|
| 71 |
-
/* 按鈕美化 */
|
| 72 |
-
button {
|
| 73 |
-
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%) !important;
|
| 74 |
-
color: white !important;
|
| 75 |
-
border: none !important;
|
| 76 |
-
border-radius: 12px !important;
|
| 77 |
-
font-weight: bold !important;
|
| 78 |
-
padding: 10px 20px !important;
|
| 79 |
-
box-shadow: 0 4px 6px rgba(0,0,0,0.1);
|
| 80 |
-
}
|
| 81 |
-
button:hover {
|
| 82 |
-
transform: translateY(-2px);
|
| 83 |
-
box-shadow: 0 6px 12px rgba(0,0,0,0.15);
|
| 84 |
-
}
|
| 85 |
|
| 86 |
-
|
| 87 |
-
.image-container {
|
| 88 |
-
border-radius: 15px;
|
| 89 |
-
overflow: hidden;
|
| 90 |
-
border: 2px solid #fff;
|
| 91 |
-
box-shadow: 0 4px 6px rgba(0,0,0,0.05);
|
| 92 |
-
background-color: white;
|
| 93 |
-
}
|
| 94 |
-
</style>
|
| 95 |
-
"""
|
| 96 |
-
|
| 97 |
-
# --- 4. 建立介面 ---
|
| 98 |
-
# 重點:這裡不放 css 參數,避開錯誤
|
| 99 |
-
with gr.Blocks() as app:
|
| 100 |
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
gr.
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
""")
|
| 111 |
-
|
| 112 |
-
with gr.Column():
|
| 113 |
-
gr.Markdown("# ✂️ AI 智能去背")
|
| 114 |
-
|
| 115 |
-
with gr.Row():
|
| 116 |
-
# 左邊:輸入
|
| 117 |
-
with gr.Column():
|
| 118 |
-
input_img = gr.Image(type="pil", label="上傳圖片", elem_classes="image-container")
|
| 119 |
-
btn = gr.Button("🚀 開始去背")
|
| 120 |
-
|
| 121 |
-
# 右邊:輸出
|
| 122 |
-
with gr.Column():
|
| 123 |
-
output_img = gr.Image(type="pil", label="去背結果", elem_classes="image-container")
|
| 124 |
|
| 125 |
btn.click(fn=process_image, inputs=input_img, outputs=output_img)
|
| 126 |
|
|
|
|
| 6 |
from PIL import Image
|
| 7 |
import timm
|
| 8 |
|
| 9 |
+
# --- 🔍 版本檢查區 (請看這裡) ---
|
| 10 |
+
import sys
|
| 11 |
+
print("="*30)
|
| 12 |
+
print(f"Python version: {sys.version}")
|
| 13 |
+
print(f"Gradio version: {gr.__version__}")
|
| 14 |
+
print(f"Torch version: {torch.__version__}")
|
| 15 |
+
print(f"Timm version: {timm.__version__}")
|
| 16 |
+
print("="*30)
|
| 17 |
+
# -----------------------------
|
| 18 |
+
|
| 19 |
# --- 1. 初始化模型 ---
|
| 20 |
model_id = "briaai/RMBG-2.0"
|
| 21 |
print(f"正在載入模型: {model_id} ...")
|
| 22 |
|
| 23 |
hf_token = os.getenv("HF_TOKEN")
|
| 24 |
+
|
| 25 |
if not hf_token:
|
| 26 |
print("⚠️ 警告: 未偵測到 HF_TOKEN")
|
| 27 |
|
| 28 |
try:
|
| 29 |
model = AutoModelForImageSegmentation.from_pretrained(
|
| 30 |
+
model_id,
|
| 31 |
+
trust_remote_code=True,
|
| 32 |
+
token=hf_token
|
| 33 |
)
|
| 34 |
+
device = torch.device("cpu")
|
| 35 |
model.to(device)
|
| 36 |
model.eval()
|
| 37 |
print("✅ 模型載入成功!")
|
| 38 |
except Exception as e:
|
| 39 |
print(f"❌ 模型載入失敗: {e}")
|
| 40 |
|
| 41 |
+
# --- 2. 圖像處理 (官方邏輯) ---
|
| 42 |
def process_image(input_image):
|
| 43 |
if input_image is None:
|
| 44 |
return None
|
|
|
|
| 45 |
image_size = (1024, 1024)
|
| 46 |
transform_image = transforms.Compose([
|
| 47 |
transforms.Resize(image_size),
|
| 48 |
transforms.ToTensor(),
|
| 49 |
transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])
|
| 50 |
])
|
|
|
|
| 51 |
input_images = transform_image(input_image).unsqueeze(0).to(device)
|
|
|
|
| 52 |
with torch.no_grad():
|
| 53 |
preds = model(input_images)[-1].sigmoid().cpu()
|
|
|
|
| 54 |
pred = preds[0].squeeze()
|
| 55 |
pred_pil = transforms.ToPILImage()(pred)
|
| 56 |
mask = pred_pil.resize(input_image.size)
|
|
|
|
| 58 |
image.putalpha(mask)
|
| 59 |
return image
|
| 60 |
|
| 61 |
+
# --- 3. 介面 ---
|
| 62 |
+
# 為了驗證,我們也在網頁上顯示版本
|
| 63 |
+
version_info = f"目前運行版本 - Gradio: {gr.__version__} | Torch: {torch.__version__}"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 64 |
|
| 65 |
+
with gr.Blocks(title="版本檢查") as app:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 66 |
|
| 67 |
+
gr.Markdown(f"## ✂️ AI 自動去背")
|
| 68 |
+
gr.Markdown(f"ℹ️ **{version_info}**") # 這裡會直接顯示在網頁上
|
| 69 |
+
|
| 70 |
+
with gr.Row():
|
| 71 |
+
with gr.Column():
|
| 72 |
+
input_img = gr.Image(type="pil", label="上傳圖片")
|
| 73 |
+
btn = gr.Button("開始去背")
|
| 74 |
+
with gr.Column():
|
| 75 |
+
output_img = gr.Image(type="pil", label="去背結果")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 76 |
|
| 77 |
btn.click(fn=process_image, inputs=input_img, outputs=output_img)
|
| 78 |
|