admin commited on
Commit
c9e3c19
·
1 Parent(s): eb1f42e
Files changed (2) hide show
  1. app.py +59 -22
  2. requirements.txt +0 -3
app.py CHANGED
@@ -1,19 +1,46 @@
1
  import os
2
  import torch
 
 
3
  import gradio as gr
4
  from PIL import Image
5
  from torchvision.transforms import transforms
6
- from huggingface_hub import snapshot_download
7
 
8
- MODEL_DIR = snapshot_download("Genius-Society/HEp2", cache_dir="./__pycache__")
9
- CLASSES = [
10
- "Centromere",
11
- "Golgi",
12
- "Homogeneous",
13
- "NuMem",
14
- "Nucleolar",
15
- "Speckled",
16
- ]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
17
 
18
 
19
  def embeding(img_path: str):
@@ -31,15 +58,24 @@ def embeding(img_path: str):
31
 
32
 
33
  def infer(target: str):
34
- model = torch.load(f"{MODEL_DIR}/save.pt", map_location=torch.device("cpu"))
35
- if not target:
36
- return None, "Please upload a cell picture!"
 
 
 
 
 
 
 
 
 
 
 
 
 
37
 
38
- torch.cuda.empty_cache()
39
- input: torch.Tensor = embeding(target)
40
- output: torch.Tensor = model(input.unsqueeze(0))
41
- predict = torch.max(output.data, 1)[1]
42
- return os.path.basename(target), CLASSES[predict]
43
 
44
 
45
  if __name__ == "__main__":
@@ -50,12 +86,13 @@ if __name__ == "__main__":
50
  with gr.Blocks() as demo:
51
  gr.Interface(
52
  fn=infer,
53
- inputs=gr.Image(type="filepath", label="Upload a cell picture"),
54
  outputs=[
55
- gr.Textbox(label="Picture name", show_copy_button=True),
56
- gr.Textbox(label="Recognition result", show_copy_button=True),
 
57
  ],
58
- title="It is recommended to upload HEp2 cell images in PNG format.",
59
  examples=example_imgs,
60
  flagging_mode="never",
61
  cache_examples=False,
 
1
  import os
2
  import torch
3
+ import modelscope
4
+ import huggingface_hub
5
  import gradio as gr
6
  from PIL import Image
7
  from torchvision.transforms import transforms
 
8
 
9
+ EN_US = os.getenv("LANG") != "zh_CN.UTF-8"
10
+ ZH2EN = {
11
+ "上传细胞图像": "Upload a cell picture",
12
+ "状态栏": "Status",
13
+ "图片名": "Picture name",
14
+ "识别结果": "Recognition result",
15
+ "请上传 PNG 格式的 HEp2 细胞图片": "It is recommended to upload HEp2 cell images in PNG format.",
16
+ }
17
+
18
+
19
+ def _L(zh_txt: str):
20
+ return ZH2EN[zh_txt] if EN_US else zh_txt
21
+
22
+
23
+ MODEL_DIR = (
24
+ huggingface_hub.snapshot_download(
25
+ "Genius-Society/HEp2",
26
+ cache_dir="./__pycache__",
27
+ )
28
+ if EN_US
29
+ else modelscope.snapshot_download(
30
+ "Genius-Society/HEp2",
31
+ cache_dir="./__pycache__",
32
+ )
33
+ )
34
+
35
+ TRANSLATE = {
36
+ "Centromere": "着丝粒",
37
+ "Golgi": "高尔基体",
38
+ "Homogeneous": "同质",
39
+ "NuMem": "记忆体",
40
+ "Nucleolar": "核仁",
41
+ "Speckled": "斑核",
42
+ }
43
+ CLASSES = list(TRANSLATE.keys())
44
 
45
 
46
  def embeding(img_path: str):
 
58
 
59
 
60
  def infer(target: str):
61
+ status = "Success"
62
+ filename = result = None
63
+ try:
64
+ model = torch.load(f"{MODEL_DIR}/save.pt", map_location=torch.device("cpu"))
65
+ if not target:
66
+ raise ValueError("请上传细胞图片")
67
+
68
+ torch.cuda.empty_cache()
69
+ input: torch.Tensor = embeding(target)
70
+ output: torch.Tensor = model(input.unsqueeze(0))
71
+ predict = torch.max(output.data, 1)[1]
72
+ filename = os.path.basename(target)
73
+ result = CLASSES[predict] if EN_US else TRANSLATE[CLASSES[predict]]
74
+
75
+ except Exception as e:
76
+ status = f"{e}"
77
 
78
+ return status, filename, result
 
 
 
 
79
 
80
 
81
  if __name__ == "__main__":
 
86
  with gr.Blocks() as demo:
87
  gr.Interface(
88
  fn=infer,
89
+ inputs=gr.Image(type="filepath", label=_L("上传细胞图像")),
90
  outputs=[
91
+ gr.Textbox(label=_L("状态栏"), show_copy_button=True),
92
+ gr.Textbox(label=_L("图片名"), show_copy_button=True),
93
+ gr.Textbox(label=_L("识别结果"), show_copy_button=True),
94
  ],
95
+ title=_L("请上传 PNG 格式的 HEp2 细胞图片"),
96
  examples=example_imgs,
97
  flagging_mode="never",
98
  cache_examples=False,
requirements.txt CHANGED
@@ -1,6 +1,3 @@
1
- tqdm
2
- gradio
3
- pillow
4
  requests
5
  torchvision
6
  torch==2.3.1
 
 
 
 
1
  requests
2
  torchvision
3
  torch==2.3.1