jackkuo commited on
Commit
6b73056
·
verified ·
1 Parent(s): a2af6b1

Update app.py

Browse files

update download fun

Files changed (1) hide show
  1. app.py +56 -1
app.py CHANGED
@@ -1,3 +1,6 @@
 
 
 
1
  from openai import OpenAI
2
  from ocr_mathpix import extract_pdf_mathpix
3
  import gradio as gr
@@ -242,6 +245,52 @@ def update_input():
242
  return en_1
243
 
244
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
245
  CSV_FILE_PATH_Golden_Benchmark_Enzyme = "static/Golden Benchmark for Enzyme Kinetics.csv"
246
  CSV_FILE_PATH_Golden_Benchmark_Ribozyme = "static/Golden Benchmark for Ribozyme Kinetics.csv"
247
  CSV_FILE_PATH_LLENKA_Dataset = "static/3450_merged_data_2000_lines.csv"
@@ -350,6 +399,11 @@ with gr.Blocks(title="Automated Enzyme Kinetics Extractor") as demo:
350
  | Enzyme3 | Homo sapiens | Substrate_C | 6.9 | mM | 15.6 | s^-1 | 43000 | µM^-1s^-1 | 65°C | 8.0 | T253S | NAD^+ |
351
 
352
  """)
 
 
 
 
 
353
  with gr.Tab("Golden Benchmark for Enzyme Kinetics"):
354
  gr.Markdown(
355
  '''<h1 align="center"> Golden Benchmark Viewer with Advanced Search </h1>
@@ -459,7 +513,8 @@ with gr.Blocks(title="Automated Enzyme Kinetics Extractor") as demo:
459
  extract_button_md.click(extract_pdf_md, inputs=file_input, outputs=text_output)
460
  extract_button_text.click(extract_pdf_pypdf, inputs=file_input, outputs=text_output)
461
  exp.click(update_input, outputs=model_input)
462
- gen.click(fn=predict, inputs=[model_input, text_output, model_dropdown, temp_slider], outputs=outputs)
 
463
  clr.click(fn=lambda: [gr.update(value=""), gr.update(value="")], inputs=None, outputs=[model_input, outputs])
464
  viewer_button.click(display_pdf_images, inputs=file_input, outputs=file_out)
465
 
 
1
+ import datetime
2
+ import tempfile
3
+ import os
4
  from openai import OpenAI
5
  from ocr_mathpix import extract_pdf_mathpix
6
  import gradio as gr
 
245
  return en_1
246
 
247
 
248
+ # 包装函数:把 outputs 的内容写入文件,并显示 File 组件
249
+ def reveal_download_file(content):
250
+ path = download_markdown(content)
251
+ if not path:
252
+ return gr.update(visible=False, value=None)
253
+ return gr.update(visible=True, value=path)
254
+
255
+
256
+ def download_markdown(content):
257
+ """Save the markdown content to a temporary file with a timestamp."""
258
+ if not content or content.strip() == "":
259
+ return None # 如果内容为空,则不生成文件
260
+
261
+ # 生成带时间戳的文件名
262
+ timestamp = datetime.datetime.now().strftime("%Y%m%d_%H%M%S")
263
+ filename = f"enzyme_kinetics_extract_{timestamp}.md"
264
+
265
+ # 直接创建带时间戳的文件,避免重命名操作
266
+ file_path = os.path.join(tempfile.gettempdir(), filename)
267
+
268
+ # 增强文件创建的健壮性,添加重试机制和更完善的错误处理
269
+ max_retries = 3
270
+ for attempt in range(max_retries):
271
+ try:
272
+ # 检查文件是否已存在,如果存在则添加序号
273
+ final_path = file_path
274
+ if os.path.exists(final_path):
275
+ name, ext = os.path.splitext(file_path)
276
+ counter = 1
277
+ while os.path.exists(f"{name}_{counter}{ext}"):
278
+ counter += 1
279
+ final_path = f"{name}_{counter}{ext}"
280
+
281
+ with open(final_path, 'w', encoding='utf-8') as f:
282
+ f.write(content)
283
+ return final_path
284
+ except Exception as e:
285
+ print(f"Attempt {attempt + 1} failed to create download file: {e}")
286
+ if attempt == max_retries - 1:
287
+ print(f"Error creating download file after {max_retries} attempts: {e}")
288
+ return None # 返回None表示创建失败
289
+ # 等待一段时间后重试
290
+ import time
291
+ time.sleep(0.1)
292
+
293
+
294
  CSV_FILE_PATH_Golden_Benchmark_Enzyme = "static/Golden Benchmark for Enzyme Kinetics.csv"
295
  CSV_FILE_PATH_Golden_Benchmark_Ribozyme = "static/Golden Benchmark for Ribozyme Kinetics.csv"
296
  CSV_FILE_PATH_LLENKA_Dataset = "static/3450_merged_data_2000_lines.csv"
 
399
  | Enzyme3 | Homo sapiens | Substrate_C | 6.9 | mM | 15.6 | s^-1 | 43000 | µM^-1s^-1 | 65°C | 8.0 | T253S | NAD^+ |
400
 
401
  """)
402
+ # 添加下载提取结果按钮和文件组件
403
+ with gr.Row():
404
+ download_file = gr.File(label="Download Extract Result File", interactive=False, visible=False)
405
+
406
+ # 添加下载 Markdown 按钮和文件组件
407
  with gr.Tab("Golden Benchmark for Enzyme Kinetics"):
408
  gr.Markdown(
409
  '''<h1 align="center"> Golden Benchmark Viewer with Advanced Search </h1>
 
513
  extract_button_md.click(extract_pdf_md, inputs=file_input, outputs=text_output)
514
  extract_button_text.click(extract_pdf_pypdf, inputs=file_input, outputs=text_output)
515
  exp.click(update_input, outputs=model_input)
516
+ gen.click(fn=predict, inputs=[model_input, text_output, model_dropdown, temp_slider], outputs=outputs) \
517
+ .then(fn=reveal_download_file, inputs=outputs, outputs=download_file)
518
  clr.click(fn=lambda: [gr.update(value=""), gr.update(value="")], inputs=None, outputs=[model_input, outputs])
519
  viewer_button.click(display_pdf_images, inputs=file_input, outputs=file_out)
520