breezedeus commited on
Commit
0aff688
1 Parent(s): 23d56fe

compatible with pix2text==1.1.1

Browse files
Files changed (2) hide show
  1. app.py +35 -38
  2. requirements.txt +1 -1
app.py CHANGED
@@ -50,18 +50,8 @@ def prepare_mfd_model():
50
 
51
 
52
  def get_p2t_model(lan_list: list, mfd_model_name: str, mfr_model_name: str):
53
- mfd_config = {}
54
- if 'yolov7_tiny' not in mfd_model_name:
55
- mfd_fp = prepare_mfd_model()
56
- mfd_config = dict( # 声明 LayoutAnalyzer 的初始化参数
57
- model_type='yolov7', # 表示使用的是 YoloV7 模型,而不是 YoloV7_Tiny 模型
58
- model_fp=mfd_fp, # 注:修改成你的模型文件所存储的路径
59
- )
60
- formula_config = {}
61
- if 'mfr-pro' in mfr_model_name:
62
- formula_config = dict( # 声明 LayoutAnalyzer 的初始化参数
63
- model_name='mfr-pro', model_backend='onnx',
64
- )
65
  text_formula_config = dict(
66
  languages=lan_list, mfd=mfd_config, formula=formula_config,
67
  )
@@ -90,11 +80,7 @@ def recognize(
90
  OUTPUT_RESULT_DIR.mkdir(exist_ok=True)
91
 
92
  out_det_fp = './docs/no-det-res.jpg'
93
- kwargs = dict(
94
- resized_shape=resized_shape,
95
- return_text = True,
96
- auto_line_break = True,
97
- )
98
  if rec_type == 'page':
99
  suffix = list(string.ascii_letters)
100
  random.shuffle(suffix)
@@ -114,20 +100,20 @@ def recognize(
114
  out_text = out
115
  if rec_type == 'page':
116
  out_text = out.to_markdown(output_dir)
117
- out_det_fp =kwargs['save_debug_res'] / 'layout_res.jpg'
118
  elif rec_type == 'text_formula':
119
  out_det_fp = kwargs['save_analysis_res']
120
 
121
  return out_text, out_det_fp
122
 
123
 
124
- def example_func(lang_list, rec_type, image_file):
125
  return recognize(
126
  lang_list,
127
- mfd_model_name='yolov7 (paid)',
128
- mfr_model_name='mfr-pro (paid)',
129
  rec_type=rec_type,
130
- resized_shape=768,
131
  image_file=image_file,
132
  )
133
 
@@ -138,24 +124,35 @@ def main():
138
 
139
  title = ': a Free Alternative to Mathpix'
140
  examples = [
141
- [['English'], 'page', 'docs/examples/page.png',],
142
- [['English'], 'text_formula', 'docs/examples/mixed-en.jpg',],
143
- [['English', 'Chinese Simplified'], 'text_formula', 'docs/examples/mixed-ch_sim.jpg',],
 
 
 
 
 
144
  [
145
  ['English', 'Chinese Traditional'],
146
  'text_formula',
 
147
  'docs/examples/mixed-ch_tra.jpg',
148
  ],
149
- [['English', 'Vietnamese'], 'text_formula', 'docs/examples/mixed-vietnamese.jpg',],
150
- [['English'], 'formula', 'docs/examples/formula1.png'],
151
- [['English'], 'formula', 'docs/examples/formula2.jpg'],
152
- [['English'], 'formula', 'docs/examples/hw-formula.png'],
153
- [['English', 'Chinese Simplified'], 'text', 'docs/examples/pure-text.jpg',],
 
 
 
 
 
154
  ]
155
 
156
  table_desc = """
157
  <div align="center">
158
- <img src="https://pix2text.readthedocs.io/zh/latest/figs/p2t-logo.png" width="120px"/>
159
 
160
  [![Visitors](https://api.visitorbadge.io/api/visitors?path=https%3A%2F%2Fhuggingface.co%2Fspaces%2Fbreezedeus%2Fpix2text-demo&labelColor=%23697689&countColor=%23f5c791&style=flat&labelStyle=upper)](https://visitorbadge.io/status?path=https%3A%2F%2Fhuggingface.co%2Fspaces%2Fbreezedeus%2Fpix2text-demo)
161
 
@@ -175,7 +172,7 @@ If useful, please help to **star 🌟 [Pix2Text](https://github.com/breezedeus/p
175
 
176
  with gr.Blocks() as demo:
177
  gr.HTML(
178
- f'<h1 style="text-align: center; margin-bottom: 1rem;"><a href="https://github.com/breezedeus/pix2text" target="_blank">Pix2Text V1.1</a>{title}</h1>'
179
  )
180
  with gr.Row(equal_height=False):
181
  with gr.Column(min_width=200, variant='panel', scale=3):
@@ -189,16 +186,16 @@ If useful, please help to **star 🌟 [Pix2Text](https://github.com/breezedeus/p
189
  )
190
  mfd_model_name = gr.Dropdown(
191
  label='MFD Models',
192
- choices=['yolov7_tiny (free)', 'yolov7 (paid)'],
193
- value='yolov7 (paid)',
194
  )
195
  mfr_model_name = gr.Dropdown(
196
  label='MFR Models',
197
- choices=['mfr (free)', 'mfr-pro (paid)'],
198
- value='mfr-pro (paid)',
199
  )
200
  rec_type = gr.Dropdown(
201
- label='File Type',
202
  choices=['page', 'text_formula', 'formula', 'text'],
203
  value='text_formula',
204
  # info='Which type of image to be recognized.',
@@ -257,7 +254,7 @@ If useful, please help to **star 🌟 [Pix2Text](https://github.com/breezedeus/p
257
  gr.Examples(
258
  label='Examples',
259
  examples=examples,
260
- inputs=[lang_list, rec_type, image_file,],
261
  outputs=[rec_result, det_result],
262
  fn=example_func,
263
  cache_examples=os.getenv('CACHE_EXAMPLES') == '1',
 
50
 
51
 
52
  def get_p2t_model(lan_list: list, mfd_model_name: str, mfr_model_name: str):
53
+ mfd_config = dict(model_name=mfd_model_name, model_backend='onnx') # 声明 MFD 的初始化参数
54
+ formula_config = dict(model_name=mfr_model_name, model_backend='onnx') # 声明 MFR 的初始化参数
 
 
 
 
 
 
 
 
 
 
55
  text_formula_config = dict(
56
  languages=lan_list, mfd=mfd_config, formula=formula_config,
57
  )
 
80
  OUTPUT_RESULT_DIR.mkdir(exist_ok=True)
81
 
82
  out_det_fp = './docs/no-det-res.jpg'
83
+ kwargs = dict(resized_shape=resized_shape, return_text=True, auto_line_break=True,)
 
 
 
 
84
  if rec_type == 'page':
85
  suffix = list(string.ascii_letters)
86
  random.shuffle(suffix)
 
100
  out_text = out
101
  if rec_type == 'page':
102
  out_text = out.to_markdown(output_dir)
103
+ out_det_fp = kwargs['save_debug_res'] / 'layout_res.jpg'
104
  elif rec_type == 'text_formula':
105
  out_det_fp = kwargs['save_analysis_res']
106
 
107
  return out_text, out_det_fp
108
 
109
 
110
+ def example_func(lang_list, rec_type, resized_shape, image_file):
111
  return recognize(
112
  lang_list,
113
+ mfd_model_name='mfd-pro',
114
+ mfr_model_name='mfr-pro',
115
  rec_type=rec_type,
116
+ resized_shape=resized_shape,
117
  image_file=image_file,
118
  )
119
 
 
124
 
125
  title = ': a Free Alternative to Mathpix'
126
  examples = [
127
+ [['English'], 'page', 768, 'docs/examples/page.png',],
128
+ [['English'], 'text_formula', 768, 'docs/examples/mixed-en.jpg',],
129
+ [
130
+ ['English', 'Chinese Simplified'],
131
+ 'text_formula',
132
+ 768,
133
+ 'docs/examples/mixed-ch_sim.jpg',
134
+ ],
135
  [
136
  ['English', 'Chinese Traditional'],
137
  'text_formula',
138
+ 768,
139
  'docs/examples/mixed-ch_tra.jpg',
140
  ],
141
+ [
142
+ ['English', 'Vietnamese'],
143
+ 'text_formula',
144
+ 608,
145
+ 'docs/examples/mixed-vietnamese.jpg',
146
+ ],
147
+ [['English'], 'formula', '-', 'docs/examples/formula1.png'],
148
+ [['English'], 'formula', '-', 'docs/examples/formula2.jpg'],
149
+ [['English'], 'formula', '-', 'docs/examples/hw-formula.png'],
150
+ [['English', 'Chinese Simplified'], 'text', '-', 'docs/examples/pure-text.jpg',],
151
  ]
152
 
153
  table_desc = """
154
  <div align="center">
155
+ <img src="https://pix2text.readthedocs.io/zh-cn/stable/figs/p2t-logo.png" width="120px"/>
156
 
157
  [![Visitors](https://api.visitorbadge.io/api/visitors?path=https%3A%2F%2Fhuggingface.co%2Fspaces%2Fbreezedeus%2Fpix2text-demo&labelColor=%23697689&countColor=%23f5c791&style=flat&labelStyle=upper)](https://visitorbadge.io/status?path=https%3A%2F%2Fhuggingface.co%2Fspaces%2Fbreezedeus%2Fpix2text-demo)
158
 
 
172
 
173
  with gr.Blocks() as demo:
174
  gr.HTML(
175
+ f'<h1 style="text-align: center; margin-bottom: 1rem;"><a href="https://github.com/breezedeus/pix2text" target="_blank">Pix2Text V1.1.1</a>{title}</h1>'
176
  )
177
  with gr.Row(equal_height=False):
178
  with gr.Column(min_width=200, variant='panel', scale=3):
 
186
  )
187
  mfd_model_name = gr.Dropdown(
188
  label='MFD Models',
189
+ choices=['mfd', 'mfd-advanced', 'mfd-pro'],
190
+ value='mfd-pro',
191
  )
192
  mfr_model_name = gr.Dropdown(
193
  label='MFR Models',
194
+ choices=['mfr', 'mfr-pro', 'mfr-plus'],
195
+ value='mfr-pro',
196
  )
197
  rec_type = gr.Dropdown(
198
+ label='file_type',
199
  choices=['page', 'text_formula', 'formula', 'text'],
200
  value='text_formula',
201
  # info='Which type of image to be recognized.',
 
254
  gr.Examples(
255
  label='Examples',
256
  examples=examples,
257
+ inputs=[lang_list, rec_type, resized_shape, image_file,],
258
  outputs=[rec_result, det_result],
259
  fn=example_func,
260
  cache_examples=os.getenv('CACHE_EXAMPLES') == '1',
requirements.txt CHANGED
@@ -1,4 +1,4 @@
1
  --extra-index-url https://pypi.org/simple
2
 
3
  pyyaml
4
- pix2text[multilingual]>=1.1.0.6
 
1
  --extra-index-url https://pypi.org/simple
2
 
3
  pyyaml
4
+ pix2text[multilingual]>=1.1.1