A123123 commited on
Commit
f5473ed
·
verified ·
1 Parent(s): 3d36685

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +5 -13
app.py CHANGED
@@ -5,20 +5,18 @@ import onnxruntime as ort
5
  import gradio as gr
6
  from huggingface_hub import hf_hub_download
7
 
8
- # --- 1. Configuration & Model Loading ---
9
- # Replace with your "username/repo-name"
10
  REPO_ID = "A123123/AnimeAutoCensor"
11
- # Retrieved from Space's Secrets
12
  HF_TOKEN = os.getenv("HF_TOKEN")
13
 
14
  try:
15
- # Download .onnx file
16
  model_path = hf_hub_download(
17
  repo_id=REPO_ID,
18
  filename="model.onnx",
19
  token=HF_TOKEN
20
  )
21
- # Download .onnx_data file (ONNX Runtime automatically looks for this in the same directory)
22
  hf_hub_download(
23
  repo_id=REPO_ID,
24
  filename="model.onnx_data",
@@ -28,11 +26,11 @@ try:
28
  # Initialize Inference Engine (Hugging Face Free Tier supports CPU only)
29
  session = ort.InferenceSession(model_path, providers=['CPUExecutionProvider'])
30
  input_name = session.get_inputs()[0].name
31
- target_size = 640 # Your model training size
32
  except Exception as e:
33
  print(f"Model loading failed. Please check Token and Repository Path: {e}")
34
 
35
- # --- 2. Core Processing Functions ---
36
  def apply_mosaic_mask(image_rgb, mask, mosaic_level=16):
37
  h, w = image_rgb.shape[:2]
38
  mask = (mask > 0).astype(np.uint8)
@@ -46,10 +44,8 @@ def process_image(input_img):
46
  if input_img is None:
47
  return None
48
 
49
- # Gradio passes images as RGB numpy arrays
50
  h_orig, w_orig = input_img.shape[:2]
51
 
52
- # A. Letterbox Preprocessing
53
  scale = target_size / max(h_orig, w_orig)
54
  new_h, new_w = int(h_orig * scale), int(w_orig * scale)
55
  img_resized = cv2.resize(input_img, (new_w, new_h))
@@ -59,7 +55,6 @@ def process_image(input_img):
59
  pad_x = (target_size - new_w) // 2
60
  canvas[pad_y:pad_y+new_h, pad_x:pad_x+new_w] = img_resized
61
 
62
- # B. Normalization & Inference
63
  input_tensor = canvas.astype(np.float32) / 255.0
64
  input_tensor = (input_tensor - [0.485, 0.456, 0.406]) / [0.229, 0.224, 0.225]
65
  input_tensor = input_tensor.transpose(2, 0, 1)[np.newaxis, ...].astype(np.float32)
@@ -67,16 +62,13 @@ def process_image(input_img):
67
  outputs = session.run(None, {input_name: input_tensor})
68
  pred = outputs[0][0][0]
69
 
70
- # C. Post-processing (Restore Size)
71
  mask_valid = pred[pad_y:pad_y+new_h, pad_x:pad_x+new_w]
72
  mask_final = cv2.resize(mask_valid, (w_orig, h_orig))
73
  binary_mask = (mask_final > 0.5).astype(np.uint8)
74
 
75
- # D. Apply Mosaic
76
  result_rgb = apply_mosaic_mask(input_img, binary_mask)
77
  return result_rgb
78
 
79
- # --- 3. Gradio Interface ---
80
  with gr.Blocks(title="AI Anime Auto-Censor") as demo:
81
  gr.Markdown("# 🎨 AI Anime Auto-Censor (Trial Version)")
82
  gr.Markdown("This tool uses AI to automatically detect and censor specific content. The model weights are protected and not accessible to the public.")
 
5
  import gradio as gr
6
  from huggingface_hub import hf_hub_download
7
 
 
 
8
  REPO_ID = "A123123/AnimeAutoCensor"
9
+
10
  HF_TOKEN = os.getenv("HF_TOKEN")
11
 
12
  try:
13
+
14
  model_path = hf_hub_download(
15
  repo_id=REPO_ID,
16
  filename="model.onnx",
17
  token=HF_TOKEN
18
  )
19
+
20
  hf_hub_download(
21
  repo_id=REPO_ID,
22
  filename="model.onnx_data",
 
26
  # Initialize Inference Engine (Hugging Face Free Tier supports CPU only)
27
  session = ort.InferenceSession(model_path, providers=['CPUExecutionProvider'])
28
  input_name = session.get_inputs()[0].name
29
+ target_size = 640
30
  except Exception as e:
31
  print(f"Model loading failed. Please check Token and Repository Path: {e}")
32
 
33
+
34
  def apply_mosaic_mask(image_rgb, mask, mosaic_level=16):
35
  h, w = image_rgb.shape[:2]
36
  mask = (mask > 0).astype(np.uint8)
 
44
  if input_img is None:
45
  return None
46
 
 
47
  h_orig, w_orig = input_img.shape[:2]
48
 
 
49
  scale = target_size / max(h_orig, w_orig)
50
  new_h, new_w = int(h_orig * scale), int(w_orig * scale)
51
  img_resized = cv2.resize(input_img, (new_w, new_h))
 
55
  pad_x = (target_size - new_w) // 2
56
  canvas[pad_y:pad_y+new_h, pad_x:pad_x+new_w] = img_resized
57
 
 
58
  input_tensor = canvas.astype(np.float32) / 255.0
59
  input_tensor = (input_tensor - [0.485, 0.456, 0.406]) / [0.229, 0.224, 0.225]
60
  input_tensor = input_tensor.transpose(2, 0, 1)[np.newaxis, ...].astype(np.float32)
 
62
  outputs = session.run(None, {input_name: input_tensor})
63
  pred = outputs[0][0][0]
64
 
 
65
  mask_valid = pred[pad_y:pad_y+new_h, pad_x:pad_x+new_w]
66
  mask_final = cv2.resize(mask_valid, (w_orig, h_orig))
67
  binary_mask = (mask_final > 0.5).astype(np.uint8)
68
 
 
69
  result_rgb = apply_mosaic_mask(input_img, binary_mask)
70
  return result_rgb
71
 
 
72
  with gr.Blocks(title="AI Anime Auto-Censor") as demo:
73
  gr.Markdown("# 🎨 AI Anime Auto-Censor (Trial Version)")
74
  gr.Markdown("This tool uses AI to automatically detect and censor specific content. The model weights are protected and not accessible to the public.")