First Blood
Browse files
app.py
ADDED
@@ -0,0 +1,72 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import json
|
3 |
+
import tempfile
|
4 |
+
import os
|
5 |
+
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)
|
16 |
+
diff_positions = []
|
17 |
+
correct_text_with_positions = ""
|
18 |
+
wrong_text_with_positions = ""
|
19 |
+
wrong_ids = []
|
20 |
+
|
21 |
+
for i, (tag, i1, i2, j1, j2) in enumerate(s.get_opcodes()):
|
22 |
+
if tag == "equal":
|
23 |
+
correct_text_with_positions += correct_text[j1:j2]
|
24 |
+
wrong_text_with_positions += wrong_text[i1:i2]
|
25 |
+
else:
|
26 |
+
correct_text_segment = correct_text[j1:j2]
|
27 |
+
wrong_text_segment = wrong_text[i1:i2]
|
28 |
+
# 對於不一緻的部分,添加標記
|
29 |
+
wrong_ids.extend(range(i1, i2))
|
30 |
+
correct_text_with_positions += f"[{correct_text_segment}]"
|
31 |
+
wrong_text_with_positions += f"[{wrong_text_segment}]"
|
32 |
+
|
33 |
+
wrong_ids_str = ",".join(map(str, wrong_ids))
|
34 |
+
|
35 |
+
return correct_text_with_positions, wrong_text_with_positions, wrong_ids_str
|
36 |
+
|
37 |
+
def generate_json(file_id, correct_text, wrong_text, wrong_ids):
|
38 |
+
"""根據輸入生成 JSON 數據,並保存為文件"""
|
39 |
+
wrong_ids_list = [int(x) for x in wrong_ids.split(',')]
|
40 |
+
data = {
|
41 |
+
"id": file_id,
|
42 |
+
"original_text": wrong_text,
|
43 |
+
"wrong_ids": wrong_ids_list,
|
44 |
+
"correct_text": correct_text
|
45 |
+
}
|
46 |
+
json_data = json.dumps([data], ensure_ascii=False, indent=4)
|
47 |
+
|
48 |
+
# 創建臨時文件來保存 JSON 數據
|
49 |
+
temp_file = tempfile.NamedTemporaryFile(delete=False, suffix=".json", mode='w', encoding='utf-8')
|
50 |
+
temp_file.write(json_data)
|
51 |
+
temp_file.close()
|
52 |
+
|
53 |
+
return json_data, temp_file.name
|
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="校正過的逐字稿文本內容")
|
62 |
+
wrong_text_output = gr.TextArea(label="未校正的ASR辨識文本內容")
|
63 |
+
wrong_ids_output = gr.Textbox(label="錯誤的文字位置")
|
64 |
+
|
65 |
+
generate_button = gr.Button("生成 JSON 文件")
|
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)
|