admin commited on
Commit
4b55707
·
1 Parent(s): 5491606

try upd req

Browse files
Files changed (2) hide show
  1. app.py +39 -11
  2. requirements.txt +2 -1
app.py CHANGED
@@ -2,21 +2,46 @@ import os
2
  import torch
3
  import random
4
  import warnings
 
 
5
  import gradio as gr
6
  from PIL import Image
7
  from model import Model
8
  from torchvision import transforms
9
- from huggingface_hub import snapshot_download
10
 
 
11
 
12
- MODEL_DIR = snapshot_download("Genius-Society/svhn", cache_dir="./__pycache__")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
13
 
14
 
15
  def infer(input_img: str, checkpoint_file: str):
 
 
16
  try:
17
  model = Model()
18
  model.restore(f"{MODEL_DIR}/{checkpoint_file}")
19
- outstr = ""
20
  with torch.no_grad():
21
  transform = transforms.Compose(
22
  [
@@ -55,10 +80,10 @@ def infer(input_img: str, checkpoint_file: str):
55
  for i in range(length_prediction.item()):
56
  outstr += str(output[i])
57
 
58
- return outstr
59
-
60
  except Exception as e:
61
- return f"{e}"
 
 
62
 
63
 
64
  def get_files(dir_path=MODEL_DIR, ext=".pth"):
@@ -87,16 +112,19 @@ if __name__ == "__main__":
87
  gr.Interface(
88
  fn=infer,
89
  inputs=[
90
- gr.Image(label="Upload an image", type="filepath"),
91
  gr.Dropdown(
92
- label="Select a model",
93
  choices=models,
94
  value=models[0],
95
  ),
96
  ],
97
- outputs=gr.Textbox(label="Recognition result", show_copy_button=True),
 
 
 
98
  examples=samples,
99
- title="Door Number Recognition",
100
  flagging_mode="never",
101
  cache_examples=False,
102
- ).launch()
 
2
  import torch
3
  import random
4
  import warnings
5
+ import modelscope
6
+ import huggingface_hub
7
  import gradio as gr
8
  from PIL import Image
9
  from model import Model
10
  from torchvision import transforms
 
11
 
12
+ EN_US = os.getenv("LANG") != "zh_CN.UTF-8"
13
 
14
+ MODEL_DIR = (
15
+ huggingface_hub.snapshot_download(
16
+ "Genius-Society/svhn",
17
+ cache_dir="./__pycache__",
18
+ )
19
+ if EN_US
20
+ else modelscope.snapshot_download(
21
+ "Genius-Society/svhn",
22
+ cache_dir="./__pycache__",
23
+ )
24
+ )
25
+
26
+ ZH2EN = {
27
+ "上传图片": "Upload an image",
28
+ "状态栏": "Status",
29
+ "选择模型": "Select a model",
30
+ "识别结果": "Recognition result",
31
+ "门牌号识别": "Door Number Recognition",
32
+ }
33
+
34
+
35
+ def _L(zh_txt: str):
36
+ return ZH2EN[zh_txt] if EN_US else zh_txt
37
 
38
 
39
  def infer(input_img: str, checkpoint_file: str):
40
+ status = "Success"
41
+ outstr = None
42
  try:
43
  model = Model()
44
  model.restore(f"{MODEL_DIR}/{checkpoint_file}")
 
45
  with torch.no_grad():
46
  transform = transforms.Compose(
47
  [
 
80
  for i in range(length_prediction.item()):
81
  outstr += str(output[i])
82
 
 
 
83
  except Exception as e:
84
+ status = f"{e}"
85
+
86
+ return status, outstr
87
 
88
 
89
  def get_files(dir_path=MODEL_DIR, ext=".pth"):
 
112
  gr.Interface(
113
  fn=infer,
114
  inputs=[
115
+ gr.Image(label=_L("上传图片"), type="filepath"),
116
  gr.Dropdown(
117
+ label=_L("选择模型"),
118
  choices=models,
119
  value=models[0],
120
  ),
121
  ],
122
+ outputs=[
123
+ gr.Textbox(label=_L("状态栏"), show_copy_button=True),
124
+ gr.Textbox(label=_L("识别结果"), show_copy_button=True),
125
+ ],
126
  examples=samples,
127
+ title=_L("门牌号识别"),
128
  flagging_mode="never",
129
  cache_examples=False,
130
+ ).launch(ssr_mode=False)
requirements.txt CHANGED
@@ -1,3 +1,4 @@
1
  torch==2.6.0+cu118
2
  -f https://download.pytorch.org/whl/torch
3
- torchvision
 
 
1
  torch==2.6.0+cu118
2
  -f https://download.pytorch.org/whl/torch
3
+ torchvision==0.21.0+cu118
4
+ -f https://download.pytorch.org/whl/torchvision