Commit
·
1532744
1
Parent(s):
7a34200
make final tranlation be copied
Browse files
app.py
CHANGED
@@ -24,18 +24,26 @@ def diff_texts(text1, text2, lang):
|
|
24 |
ic(lang)
|
25 |
if lang == '中文':
|
26 |
return [
|
27 |
-
(token[2:],
|
|
|
|
|
|
|
|
|
28 |
for token in d.compare(text1, text2)
|
29 |
-
if token[0] in ["+", " "]
|
30 |
]
|
31 |
else:
|
32 |
words1 = re.findall(r'\S+|\s+', text1)
|
33 |
words2 = re.findall(r'\S+|\s+', text2)
|
34 |
|
35 |
return [
|
36 |
-
(token[2:],
|
|
|
|
|
|
|
|
|
37 |
for token in d.compare(words1, words2)
|
38 |
-
if token[0] in ["+", " "]
|
39 |
]
|
40 |
|
41 |
|
@@ -51,18 +59,18 @@ def translate_text(source_lang, target_lang, source_text, country, max_tokens=MA
|
|
51 |
translation_1 = one_chunk_initial_translation(
|
52 |
source_lang, target_lang, source_text
|
53 |
)
|
54 |
-
yield translation_1, None, None
|
55 |
|
56 |
reflection = one_chunk_reflect_on_translation(
|
57 |
source_lang, target_lang, source_text, translation_1, country
|
58 |
)
|
59 |
-
yield translation_1, reflection, None
|
60 |
|
61 |
translation_2 = one_chunk_improve_translation(
|
62 |
source_lang, target_lang, source_text, translation_1, reflection
|
63 |
)
|
64 |
translation_diff = diff_texts(translation_1, translation_2, target_lang)
|
65 |
-
yield translation_1, reflection, translation_diff
|
66 |
|
67 |
else:
|
68 |
ic("Translating text as multiple chunks")
|
@@ -86,7 +94,7 @@ def translate_text(source_lang, target_lang, source_text, country, max_tokens=MA
|
|
86 |
)
|
87 |
ic(translation_1_chunks)
|
88 |
translation_1 = "".join(translation_1_chunks)
|
89 |
-
yield translation_1, None, None
|
90 |
|
91 |
reflection_chunks = multichunk_reflect_on_translation(
|
92 |
source_lang,
|
@@ -97,7 +105,7 @@ def translate_text(source_lang, target_lang, source_text, country, max_tokens=MA
|
|
97 |
)
|
98 |
ic(reflection_chunks)
|
99 |
reflection = "".join(reflection_chunks)
|
100 |
-
yield translation_1, reflection, None
|
101 |
|
102 |
translation_2_chunks = multichunk_improve_translation(
|
103 |
source_lang,
|
@@ -110,12 +118,11 @@ def translate_text(source_lang, target_lang, source_text, country, max_tokens=MA
|
|
110 |
translation_2 = "".join(translation_2_chunks)
|
111 |
translation_diff = diff_texts(translation_1, translation_2, target_lang)
|
112 |
|
113 |
-
yield translation_1, reflection, translation_diff
|
114 |
|
115 |
|
116 |
-
|
117 |
def update_ui(translation_1, reflection, translation_diff):
|
118 |
-
return gr.update(value=translation_1), gr.update(value=reflection), gr.update(value=translation_diff)
|
119 |
|
120 |
with gr.Blocks() as demo:
|
121 |
gr.Markdown("# Andrew Ng's Translation Agent ")
|
@@ -128,16 +135,22 @@ with gr.Blocks() as demo:
|
|
128 |
btn = gr.Button("Translate")
|
129 |
|
130 |
with gr.Row():
|
131 |
-
translation_1 = gr.Textbox(label="Initial Translation", lines=3)
|
132 |
-
reflection = gr.Textbox(label="Reflection", lines=3)
|
133 |
-
|
134 |
-
translation_diff = gr.HighlightedText
|
135 |
-
|
136 |
-
|
137 |
-
|
138 |
-
|
139 |
-
|
140 |
-
|
141 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
142 |
|
143 |
demo.launch()
|
|
|
24 |
ic(lang)
|
25 |
if lang == '中文':
|
26 |
return [
|
27 |
+
(token[2:],
|
28 |
+
"+" if token[0] == "+" else
|
29 |
+
"-" if token[0] == "-" else
|
30 |
+
"~" if token[0] == "?" else
|
31 |
+
None)
|
32 |
for token in d.compare(text1, text2)
|
33 |
+
if token[0] in ["+", "-", "?", " "]
|
34 |
]
|
35 |
else:
|
36 |
words1 = re.findall(r'\S+|\s+', text1)
|
37 |
words2 = re.findall(r'\S+|\s+', text2)
|
38 |
|
39 |
return [
|
40 |
+
(token[2:],
|
41 |
+
"+" if token[0] == "+" else
|
42 |
+
"-" if token[0] == "-" else
|
43 |
+
"~" if token[0] == "?" else
|
44 |
+
None)
|
45 |
for token in d.compare(words1, words2)
|
46 |
+
if token[0] in ["+", "-", "?", " "]
|
47 |
]
|
48 |
|
49 |
|
|
|
59 |
translation_1 = one_chunk_initial_translation(
|
60 |
source_lang, target_lang, source_text
|
61 |
)
|
62 |
+
yield translation_1, None, None, None
|
63 |
|
64 |
reflection = one_chunk_reflect_on_translation(
|
65 |
source_lang, target_lang, source_text, translation_1, country
|
66 |
)
|
67 |
+
yield translation_1, reflection, None, None
|
68 |
|
69 |
translation_2 = one_chunk_improve_translation(
|
70 |
source_lang, target_lang, source_text, translation_1, reflection
|
71 |
)
|
72 |
translation_diff = diff_texts(translation_1, translation_2, target_lang)
|
73 |
+
yield translation_1, reflection, translation_diff, translation_2
|
74 |
|
75 |
else:
|
76 |
ic("Translating text as multiple chunks")
|
|
|
94 |
)
|
95 |
ic(translation_1_chunks)
|
96 |
translation_1 = "".join(translation_1_chunks)
|
97 |
+
yield translation_1, None, None, None
|
98 |
|
99 |
reflection_chunks = multichunk_reflect_on_translation(
|
100 |
source_lang,
|
|
|
105 |
)
|
106 |
ic(reflection_chunks)
|
107 |
reflection = "".join(reflection_chunks)
|
108 |
+
yield translation_1, reflection, None, None
|
109 |
|
110 |
translation_2_chunks = multichunk_improve_translation(
|
111 |
source_lang,
|
|
|
118 |
translation_2 = "".join(translation_2_chunks)
|
119 |
translation_diff = diff_texts(translation_1, translation_2, target_lang)
|
120 |
|
121 |
+
yield translation_1, reflection, translation_diff, translation_2
|
122 |
|
123 |
|
|
|
124 |
def update_ui(translation_1, reflection, translation_diff):
|
125 |
+
return gr.update(value=translation_1), gr.update(value=reflection), gr.update(value=translation_diff), gr.update(value=translation_2)
|
126 |
|
127 |
with gr.Blocks() as demo:
|
128 |
gr.Markdown("# Andrew Ng's Translation Agent ")
|
|
|
135 |
btn = gr.Button("Translate")
|
136 |
|
137 |
with gr.Row():
|
138 |
+
translation_1 = gr.Textbox(label="Initial Translation", lines=3, show_copy_button=True)
|
139 |
+
reflection = gr.Textbox(label="Reflection", lines=3, show_copy_button=True)
|
140 |
+
|
141 |
+
translation_diff = gr.HighlightedText(
|
142 |
+
label="Comparison",
|
143 |
+
combine_adjacent=True,
|
144 |
+
show_legend=True,
|
145 |
+
color_map={
|
146 |
+
"+": "green", # 新增的文本显示为绿色
|
147 |
+
"-": "red", # 删除的文本显示为红色
|
148 |
+
"~": "yellow" # 修改的文本显示为黄色
|
149 |
+
}
|
150 |
+
)
|
151 |
+
translation_2 = gr.Textbox(label="Final Translation", lines=5, show_copy_button=True)
|
152 |
+
|
153 |
+
btn.click(translate_text, inputs=[source_lang, target_lang, source_text, country], outputs=[translation_1, reflection, translation_diff, translation_2], queue=True)
|
154 |
+
btn.click(update_ui, inputs=[translation_1, reflection, translation_diff], outputs=[translation_1, reflection, translation_diff, translation_2], queue=True)
|
155 |
|
156 |
demo.launch()
|