tumuyan2 commited on
Commit
55f5f0f
·
1 Parent(s): 6b67881

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +19 -18
app.py CHANGED
@@ -90,21 +90,21 @@ def download_file2folder(url: str, folder: str, filesize_max: int, filesize_min:
90
  return None
91
 
92
  async def _process_model(model_input: Union[str, gr.File], tilesize: int, output_dir: str,task_id:int,fp16:bool):
93
- log = ('初始化日志记录...')
94
  print_log(task_id, '初始化日志记录', '开始')
95
  yield [],[], log
96
 
97
  if isinstance(model_input, str):
98
  input_path = model_input
99
- log += f'使用文件: {input_path}'
100
  else:
101
  input_path = model_input.name
102
- log += f'已上传文件: {input_path}'
103
  print_log(task_id, log.split('\n')[-1], '开始')
104
  yield [], [], log
105
 
106
  if not input_path:
107
- log += ( f'未获得正确的模型文件')
108
  print_log(task_id, f'未获得正确的模型文件', '错误')
109
  yield [],[], log
110
  return
@@ -112,17 +112,17 @@ async def _process_model(model_input: Union[str, gr.File], tilesize: int, output
112
 
113
  if input_path.endswith('.onnx'):
114
  onnx_path = input_path
115
- log += ( '输入已是 ONNX 模型,直接使用...')
116
- print_log(task_id, '输入已是 ONNX 模型,直接使用', '开始')
117
  yield [],[], log
118
  else:
119
  print_log(task_id, f'转换 PTH 模型为 ONNX, folder={output_dir}', '开始')
120
  onnx_path = convert_pth_to_onnx(input_path, tilesize=tilesize, output_folder=output_dir,use_fp16=fp16)
121
  if onnx_path:
122
- log += ( f'成功生成ONNX模型: {onnx_path}')
123
  print_log(task_id, f'生成ONNX模型: {onnx_path}', '完成')
124
  else:
125
- log += ( '生成ONNX模型失败')
126
  print_log(task_id, '生成ONNX模型', '错误')
127
  yield [], [], log
128
  return
@@ -132,12 +132,12 @@ async def _process_model(model_input: Union[str, gr.File], tilesize: int, output
132
  output_name= os.path.splitext(os.path.basename(onnx_path))[0]
133
  mnn_path = os.path.join(output_dir, f'{output_name}.mnn')
134
  try:
135
- log += ( '正在将 ONNX 模型转换为 MNN 格式...')
136
  print_log(task_id, '正在将 ONNX 模型转换为 MNN 格式', '开始')
137
  convertmnn(onnx_path, mnn_path,fp16)
138
  yield onnx_path,[], log
139
  except Exception as e:
140
- log += ( f'转换 MNN 模型时出错: {str(e)}')
141
  print_log(task_id, f'转换 MNN 模型时出错: {str(e)}', '错误')
142
  yield onnx_path,[], log
143
 
@@ -145,9 +145,9 @@ async def _process_model(model_input: Union[str, gr.File], tilesize: int, output
145
 
146
  # 转换为 MNN 模型后对文件检查
147
  if os.path.exists(mnn_path) and os.path.getsize(mnn_path) > 1024: # 1KB = 1024 bytes
148
- log += ( f'MNN 模型已保存到: {mnn_path}')
149
  else:
150
- log += ( 'MNN 模型生成失败或文件大小不足1KB')
151
  mnn_path = None
152
 
153
  yield onnx_path, mnn_path, log
@@ -160,7 +160,6 @@ with gr.Blocks() as demo:
160
  url_input = gr.Textbox(label='模型链接')
161
  file_input = gr.File(label='上传模型文件', visible=False)
162
 
163
-
164
  def show_input(input_type):
165
  if input_type == '模型链接':
166
  return gr.update(visible=True), gr.update(visible=False)
@@ -185,10 +184,11 @@ with gr.Blocks() as demo:
185
  task_counter += 1
186
  output_dir = os.path.join(os.getcwd(), f"output_{task_counter}")
187
  os.makedirs(output_dir, exist_ok=True)
 
188
 
189
  if input_type == '模型链接' and url_input:
190
  # 新增:下载模型文件到 output_dir
191
- log = f'正在下载模型文件: {url_input}'
192
  print_log(task_counter, f'正在下载模型文件: {url_input}', '开始')
193
  yield None, None, log
194
 
@@ -199,7 +199,7 @@ with gr.Blocks() as demo:
199
  filesize_min=1024 # 1KB
200
  )
201
  model_input = os.path.join(output_dir, filename)
202
- log += f'\n模型文件已下载到: {model_input}'
203
  print_log(task_counter, f'模型文件已下载到: {model_input}', '完成')
204
  yield None, None, log
205
  elif input_type == '上传模型文件' and file_input:
@@ -216,12 +216,13 @@ with gr.Blocks() as demo:
216
  # 调用重命名后的函数
217
  async for result in _process_model(model_input, int(tilesize), output_dir, task_counter, fp16):
218
  if isinstance(result, tuple) and len(result) == 3:
219
- onnx_path, mnn_path, log_box = result
 
220
  elif isinstance(result, tuple) and len(result) == 2:
221
  # 处理纯日志yield
222
  _, process_log = result
223
- yield None, None, process_log
224
- yield onnx_path, mnn_path, log_box
225
 
226
  convert_btn.click(
227
  process_model,
 
90
  return None
91
 
92
  async def _process_model(model_input: Union[str, gr.File], tilesize: int, output_dir: str,task_id:int,fp16:bool):
93
+ log = ('初始化日志记录...\n')
94
  print_log(task_id, '初始化日志记录', '开始')
95
  yield [],[], log
96
 
97
  if isinstance(model_input, str):
98
  input_path = model_input
99
+ log += f'使用文件: {input_path}\n'
100
  else:
101
  input_path = model_input.name
102
+ log += f'已上传文件: {input_path}\n'
103
  print_log(task_id, log.split('\n')[-1], '开始')
104
  yield [], [], log
105
 
106
  if not input_path:
107
+ log += ( f'未获得正确的模型文件\n')
108
  print_log(task_id, f'未获得正确的模型文件', '错误')
109
  yield [],[], log
110
  return
 
112
 
113
  if input_path.endswith('.onnx'):
114
  onnx_path = input_path
115
+ log += ( '输入已经是 ONNX 文件\n')
116
+ print_log(task_id, '输入已经是 ONNX 文件', '跳过')
117
  yield [],[], log
118
  else:
119
  print_log(task_id, f'转换 PTH 模型为 ONNX, folder={output_dir}', '开始')
120
  onnx_path = convert_pth_to_onnx(input_path, tilesize=tilesize, output_folder=output_dir,use_fp16=fp16)
121
  if onnx_path:
122
+ log += ( f'成功生成ONNX模型: {onnx_path}\n')
123
  print_log(task_id, f'生成ONNX模型: {onnx_path}', '完成')
124
  else:
125
+ log += ( '生成ONNX模型失败\n')
126
  print_log(task_id, '生成ONNX模型', '错误')
127
  yield [], [], log
128
  return
 
132
  output_name= os.path.splitext(os.path.basename(onnx_path))[0]
133
  mnn_path = os.path.join(output_dir, f'{output_name}.mnn')
134
  try:
135
+ log += ( '正在将 ONNX 模型转换为 MNN 格式...\n')
136
  print_log(task_id, '正在将 ONNX 模型转换为 MNN 格式', '开始')
137
  convertmnn(onnx_path, mnn_path,fp16)
138
  yield onnx_path,[], log
139
  except Exception as e:
140
+ log += ( f'转换 MNN 模型时出错: {str(e)}\n')
141
  print_log(task_id, f'转换 MNN 模型时出错: {str(e)}', '错误')
142
  yield onnx_path,[], log
143
 
 
145
 
146
  # 转换为 MNN 模型后对文件检查
147
  if os.path.exists(mnn_path) and os.path.getsize(mnn_path) > 1024: # 1KB = 1024 bytes
148
+ log += ( f'MNN 模型已保存到: {mnn_path}\n')
149
  else:
150
+ log += ( 'MNN 模型生成失败或文件大小不足1KB\n')
151
  mnn_path = None
152
 
153
  yield onnx_path, mnn_path, log
 
160
  url_input = gr.Textbox(label='模型链接')
161
  file_input = gr.File(label='上传模型文件', visible=False)
162
 
 
163
  def show_input(input_type):
164
  if input_type == '模型链接':
165
  return gr.update(visible=True), gr.update(visible=False)
 
184
  task_counter += 1
185
  output_dir = os.path.join(os.getcwd(), f"output_{task_counter}")
186
  os.makedirs(output_dir, exist_ok=True)
187
+ log=""
188
 
189
  if input_type == '模型链接' and url_input:
190
  # 新增:下载模型文件到 output_dir
191
+ log = f'正在下载模型文件: {url_input}\n'
192
  print_log(task_counter, f'正在下载模型文件: {url_input}', '开始')
193
  yield None, None, log
194
 
 
199
  filesize_min=1024 # 1KB
200
  )
201
  model_input = os.path.join(output_dir, filename)
202
+ log += f'\n模型文件已下载到: {model_input}\n'
203
  print_log(task_counter, f'模型文件已下载到: {model_input}', '完成')
204
  yield None, None, log
205
  elif input_type == '上传模型文件' and file_input:
 
216
  # 调用重命名后的函数
217
  async for result in _process_model(model_input, int(tilesize), output_dir, task_counter, fp16):
218
  if isinstance(result, tuple) and len(result) == 3:
219
+ onnx_path, mnn_path, process_log = result
220
+ yield onnx_path, mnn_path, log+process_log
221
  elif isinstance(result, tuple) and len(result) == 2:
222
  # 处理纯日志yield
223
  _, process_log = result
224
+ yield None, None, log+process_log
225
+ # yield onnx_path, mnn_path, log+process_log
226
 
227
  convert_btn.click(
228
  process_model,