saliacoel commited on
Commit
eb36593
Β·
verified Β·
1 Parent(s): b5cf95e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +18 -66
app.py CHANGED
@@ -1,83 +1,35 @@
1
- # app.py ─────────────────────────────────────────────────────────────
2
- import os
3
  import gradio as gr
4
- import spaces # ← tells HF/ZeroGPU we need a card
 
5
 
6
- from detection import ( # your existing helper classes
7
- EyesDetection, FaceDetection, HeadDetection, PersonDetection,
8
- HandDetection, CensorDetection, HalfBodyDetection,
9
- NudeNetDetection, BooruYOLODetection,
10
- )
11
-
12
- # ---------------------------------------------------------------------
13
- # 0. Global style (unchanged)
14
- _GLOBAL_CSS = """
15
- .limit-height { max-height: 55vh; }
16
- """
17
-
18
- # ---------------------------------------------------------------------
19
- # 1. Thin, GPU-decorated function that Unity will call
20
- @spaces.GPU() # allocate GPU only while this runs
21
- def eyes_detect_api(image):
22
  """
23
  Args
24
  ----
25
- image : PIL.Image (gradio passes this automatically)
26
 
27
  Returns
28
  -------
29
- masks : list[PIL.Image] – one mask per eye
30
- boxes : list[list[int]] – [x1, y1, x2, y2] per eye
31
  """
32
- # β€” lazy-load & cache the model so we don't pay the cost each call
33
- global _EYE_DETECTOR
34
- if "_EYE_DETECTOR" not in globals():
35
- _EYE_DETECTOR = EyesDetection() # heavy init happens once
36
- masks, boxes = _EYE_DETECTOR.process_image(image)
37
  return masks, boxes
38
 
39
- # ---------------------------------------------------------------------
40
- # 2. Build the original UI
41
- with gr.Blocks(css=_GLOBAL_CSS) as demo:
42
- with gr.Row():
43
- with gr.Column():
44
- gr.HTML('<h2 style="text-align: center;">Object Detections For Anime</h2>')
45
- gr.Markdown(
46
- 'This is the online demo for detection functions of '
47
- '[imgutils.detect](https://dghs-imgutils.deepghs.org/main/api_doc/detect/index.html). '
48
- 'You can try them yourselves with `pip install dghs-imgutils`.'
49
- )
50
-
51
- with gr.Row():
52
- with gr.Tabs():
53
- with gr.Tab('Face Detection'):
54
- FaceDetection().make_ui()
55
- with gr.Tab('Head Detection'):
56
- HeadDetection().make_ui()
57
- with gr.Tab('Person Detection'):
58
- PersonDetection().make_ui()
59
- with gr.Tab('Half Body Detection'):
60
- HalfBodyDetection().make_ui()
61
- with gr.Tab('Eyes Detection'):
62
- EyesDetection().make_ui()
63
- with gr.Tab('Hand Detection'):
64
- HandDetection().make_ui()
65
- with gr.Tab('Censor Point Detection'):
66
- CensorDetection().make_ui()
67
- with gr.Tab('NudeNet'):
68
- NudeNetDetection().make_ui()
69
- with gr.Tab('BooruYOLO'):
70
- BooruYOLODetection().make_ui()
71
-
72
- # -----------------------------------------------------------------
73
- # 3. API-only endpoint (no UI) for Unity / curl
74
  demo.api(
75
- fn=eyes_detect_api,
76
  inputs=gr.Image(type="pil"),
77
  outputs=[gr.Gallery(label="Masks"), gr.JSON(label="Boxes")],
78
  name="eye_detect"
79
  )
80
 
81
- # ---------------------------------------------------------------------
82
- # 4. Queue & launch (unchanged except we let Gradio choose CPU threads)
83
- demo.queue(concurrency_count=os.cpu_count()).launch()
 
 
 
1
  import gradio as gr
2
+ import spaces
3
+ from detection import EyesDetection # comes from imgutils
4
 
5
+ # -------------------------------------------------------------------
6
+ # 1) GPU-decorated inference function
7
+ @spaces.GPU(estimate=60) # ask ZeroGPU for up to 60 s of GPU
8
+ def eye_detect(image):
 
 
 
 
 
 
 
 
 
 
 
 
9
  """
10
  Args
11
  ----
12
+ image : PIL.Image (gradio supplies this)
13
 
14
  Returns
15
  -------
16
+ masks : list[PIL.Image] – per-eye mask PNGs
17
+ boxes : list[list[int]] – [x1, y1, x2, y2] per eye
18
  """
19
+ global _DET
20
+ if "_DET" not in globals(): # lazy-load & cache model weights
21
+ _DET = EyesDetection()
22
+ masks, boxes = _DET.process_image(image)
 
23
  return masks, boxes
24
 
25
+ # -------------------------------------------------------------------
26
+ # 2) API-only Gradio app (no GUI at all)
27
+ with gr.Blocks() as demo:
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
28
  demo.api(
29
+ fn=eye_detect,
30
  inputs=gr.Image(type="pil"),
31
  outputs=[gr.Gallery(label="Masks"), gr.JSON(label="Boxes")],
32
  name="eye_detect"
33
  )
34
 
35
+ demo.queue(concurrency_count=1).launch()