DeepLearning101 commited on
Commit
0ae976a
·
verified ·
1 Parent(s): 50e7434

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +16 -9
app.py CHANGED
@@ -6,10 +6,8 @@ import difflib
6
 
7
  def compare_texts(correct_file, wrong_file):
8
  """讀取並返回文件內容"""
9
- with open(correct_file.name, "r", encoding="utf-8") as f:
10
- correct_text = f.read()
11
- with open(wrong_file.name, "r", encoding="utf-8") as f:
12
- wrong_text = f.read()
13
 
14
  # 比較兩個文本並找出不同的位置
15
  s = difflib.SequenceMatcher(None, wrong_text, correct_text)
@@ -54,8 +52,9 @@ def generate_json(file_id, correct_text, wrong_text, wrong_ids):
54
 
55
  with gr.Blocks() as demo:
56
  file_id_input = gr.Textbox(label="請輸入文件ID")
57
- correct_file = gr.File(label="上傳校正過的逐字稿文本文件", type="file")
58
- wrong_file = gr.File(label="上傳未校正的ASR辨識文本文件", type="file")
 
59
 
60
  compare_button = gr.Button("比較文本")
61
  correct_text_output = gr.TextArea(label="校正過的逐字稿文本內容")
@@ -66,7 +65,15 @@ with gr.Blocks() as demo:
66
  json_output = gr.Text(label="JSON 輸出")
67
  json_download_link = gr.File(label="下載 JSON 文件")
68
 
69
- compare_button.click(compare_texts, inputs=[correct_file, wrong_file], outputs=[correct_text_output, wrong_text_output, wrong_ids_output])
70
- generate_button.click(generate_json, inputs=[file_id_input, correct_text_output, wrong_text_output, wrong_ids_output], outputs=[json_output, json_download_link])
 
 
 
 
 
 
 
 
71
 
72
- demo.launch(share=True)
 
6
 
7
  def compare_texts(correct_file, wrong_file):
8
  """讀取並返回文件內容"""
9
+ correct_text = correct_file.read().decode('utf-8') # 解碼二進制數據
10
+ wrong_text = wrong_file.read().decode('utf-8') # 解碼二進制數據
 
 
11
 
12
  # 比較兩個文本並找出不同的位置
13
  s = difflib.SequenceMatcher(None, wrong_text, correct_text)
 
52
 
53
  with gr.Blocks() as demo:
54
  file_id_input = gr.Textbox(label="請輸入文件ID")
55
+ # 使用 binary 作为文件类型,这样上传的文件将以二进制形式传递给函数
56
+ correct_file = gr.File(label="上傳校正過的逐字稿文本文件", type="binary")
57
+ wrong_file = gr.File(label="上傳未校正的ASR辨識文本文件", type="binary")
58
 
59
  compare_button = gr.Button("比較文本")
60
  correct_text_output = gr.TextArea(label="校正過的逐字稿文本內容")
 
65
  json_output = gr.Text(label="JSON 輸出")
66
  json_download_link = gr.File(label="下載 JSON 文件")
67
 
68
+ compare_button.click(
69
+ compare_texts,
70
+ inputs=[correct_file, wrong_file],
71
+ outputs=[correct_text_output, wrong_text_output, wrong_ids_output]
72
+ )
73
+ generate_button.click(
74
+ generate_json,
75
+ inputs=[file_id_input, correct_text_output, wrong_text_output, wrong_ids_output],
76
+ outputs=[json_output, json_download_link]
77
+ )
78
 
79
+ demo.launch(share=True)