Skip to content

Commit ef57fae

Browse files
committed
Sync the web app with IMLab server
1 parent 82f2e3f commit ef57fae

File tree

1 file changed

+49
-18
lines changed

1 file changed

+49
-18
lines changed

app.py

+49-18
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ def config():
125125
load_dotenv()
126126

127127
def call_gpt(paragraph, trigger):
128-
# openai.api_key = os.environ.get("GPT-API")
128+
openai.api_key = os.environ.get("GPT-API")
129129
tokenizer = BartTokenizer.from_pretrained("theQuert/NetKUp-tokenzier")
130130
inputs_for_gpt = f"""
131131
s an article writer, your task is to provide an updated paragraph in the length same as non-updated paragraph based on the given non-updated paragraph and a triggered news.Remember, the length of updated paragraph is restricted into a single paragraph.
@@ -267,9 +267,30 @@ def main(input_article, input_trigger):
267267
# merge updated paragraphs with non-updated paragraphs
268268
paragraphs_merged = data_test.copy()
269269
paragraphs_merged = [str(par).split(" -- ")[0] for par in paragraphs_merged]
270+
paragraphs_old = paragraphs_merged.copy()
270271
for idx in range(len(pos_ids)):
271272
paragraphs_merged[pos_ids[idx]] = updated_paragraphs[idx]
272-
273+
global_pos = [pos_ids[idx] for idx in range(len(pos_ids))]
274+
275+
updated_color_sents = []
276+
old_color_sents = []
277+
for idx in range(len(paragraphs_merged)):
278+
if idx not in global_pos:
279+
# color_sents[paragraphs_merged]="white"
280+
tup = (paragraphs_merged[idx]+"\n", "Unchanged")
281+
updated_color_sents.append(tup)
282+
else:
283+
tup = (paragraphs_merged[idx]+"\n", "Updated")
284+
updated_color_sents.append(tup)
285+
for idx in range(len(paragraphs_old)):
286+
if idx not in global_pos:
287+
tup = (paragraphs_old[idx]+"\n", "Unchanged")
288+
old_color_sents.append(tup)
289+
else:
290+
tup = (paragraphs_old[idx]+"\n", "Modified")
291+
old_color_sents.append(tup)
292+
293+
273294
sep = "\n"
274295
# paragarphs_merged = ["".join(par.split(" -- ")[:-1]) for par in paragraphs_merged]
275296
updated_article = str(sep.join(paragraphs_merged))
@@ -285,9 +306,10 @@ def main(input_article, input_trigger):
285306

286307
# combine the predictions and paragraphs into csv format file
287308
merged_par_pred_df = pd.DataFrame({"paragraphs": data_test, "predictions": predictions}).to_csv("./util/experiments/par_with_class.csv")
288-
# return updated_article, modified, merged_par_pred_df
289-
modified_in_all = str(len(paragraphs_needed)) + " / " + str(len(data_test))
290-
return updated_article, modified_in_all
309+
# modified_in_all = str(len(paragraphs_needed)) + " / " + str(len(data_test))
310+
# return formatted_input, updated_article
311+
# return formatted_input, updated_color_sents
312+
return old_color_sents, updated_color_sents
291313

292314
def copy_to_clipboard(t):
293315
with open("./util/experiments/updated_article.txt", "r") as f:
@@ -328,17 +350,32 @@ def compare_versions():
328350
</div>"""
329351
)
330352
with gr.Tab("Article Updating"):
353+
gr.Markdown("### Reference examples can be found in the subsequent tab.")
331354
input_1 = gr.Textbox(label="Non-updated Article", lines=2, placeholder="Input the contexts...")
332355
input_2 = gr.Textbox(label="Triggered News Event", lines=1, placeholder="Input the triggered news event...")
333-
btn = gr.Button(value="Submit")
334356
with gr.Row():
335-
output_1 = gr.Textbox(label="Updated Article", lines=5)
336-
output_2 = gr.Textbox(label="#MODIFIED / #ALL")
357+
gr.ClearButton([input_1, input_2])
358+
btn = gr.Button(value="Submit")
359+
with gr.Row():
360+
# output_1 = gr.Textbox(label="Non-updated Article", lines=5, placeholder="Please fill the textboxes above, then click 'Submit'")
361+
output_1 = gr.HighlightedText(label="Non-updated Article")
362+
# output_2 = gr.Textbox(label="Updated Article", lines=5)
363+
output_2 = gr.HighlightedText(label="Updated Article")
337364
btn.click(fn=main, inputs=[input_1, input_2], outputs=[output_1, output_2])
338-
btn_copy = gr.Button(value="Copy Updated Article to Clipboard")
339-
btn_copy.click(fn=copy_to_clipboard, inputs=[output_1], outputs=[])
365+
btn_copy = gr.Button(value="Copy Updated Article to clipboard")
366+
btn_copy.click(fn=copy_to_clipboard, inputs=[output_2], outputs=[])
367+
368+
com_1_value, com_2_value = "Pls finish article updating, then click the button above", "Pls finish article updating, then click the button above."
369+
# with gr.Tab("Compare between versions"):
370+
# btn_com = gr.Button(value="Differences Highlighting")
371+
# with gr.Row():
372+
# com_1 = gr.Textbox(label="Non-update Paragraphs", value=com_1_value, lines=15)
373+
# com_2 = gr.Textbox(label="Updated Paragraphs", value=com_2_value, lines=15)
374+
# btn_com.click(fn=compare_versions, inputs=[], outputs=[com_1, com_2])
375+
with gr.Tab("Examples"):
340376
gr.Markdown("## Input Examples")
341-
gr.Markdown("### There are 2 examples below, click them to test inputs automatically!")
377+
gr.Markdown("### Two examples are provided below; please select one to auto-populate the inputs.")
378+
gr.Markdown("### Kindly select one of the provided examples, then return to the 'Article Updating' mode to assess the outcomes.")
342379
gr.Examples(
343380
examples=[[exin_1, trigger_1], [exin_2, trigger_2]],
344381
fn=main,
@@ -347,13 +384,7 @@ def compare_versions():
347384
# cache_examples=True,
348385
# run_on_click=True,
349386
),
350-
com_1_value, com_2_value = "Pls finish article updating, then click the button above", "Pls finish article updating, then click the button above."
351-
with gr.Tab("Compare between versions"):
352-
btn_com = gr.Button(value="Differences Highlighting")
353-
with gr.Row():
354-
com_1 = gr.Textbox(label="Non-update Article", value=com_1_value, lines=15)
355-
com_2 = gr.Textbox(label="Updated Article", value=com_2_value, lines=15)
356-
btn_com.click(fn=compare_versions, inputs=[], outputs=[com_1, com_2])
387+
357388
gr.HTML("""
358389
<div align="center">
359390
<p>

0 commit comments

Comments
 (0)