Spaces:
Runtime error
Runtime error
Update heights
Browse files
app.py
CHANGED
@@ -113,16 +113,18 @@ def decode(paragraphs_needed):
|
|
113 |
def split_article(article, trigger):
|
114 |
if article.split("\n"): article = article.replace("\n", "\\\\c\\\\c")
|
115 |
paragraphs = article.replace("\\c\\c", "\c\c").split("\\\\c\\\\c")
|
|
|
116 |
pars = [str(par) + " -- " + str(trigger) for par in paragraphs]
|
117 |
# pd.DataFrame({"paragraph": pars}).to_csv("./util/experiments/input_paragraphs.csv")
|
118 |
-
|
|
|
119 |
|
120 |
def config():
|
121 |
load_dotenv()
|
122 |
|
123 |
def call_gpt(paragraph, trigger):
|
124 |
# openai.api_key = os.environ.get("GPT_API")
|
125 |
-
openai.api_key = "sk-
|
126 |
tokenizer = BartTokenizer.from_pretrained("theQuert/NetKUp-tokenzier")
|
127 |
inputs_for_gpt = f"""
|
128 |
As 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.
|
@@ -163,9 +165,9 @@ As an article writer, your task is to provide an updated paragraph in the length
|
|
163 |
|
164 |
|
165 |
def main(input_article, input_trigger):
|
166 |
-
|
167 |
-
|
168 |
-
|
169 |
modified = "TRUE"
|
170 |
# device = "cuda" if torch.cuda.is_available() else "cpu"
|
171 |
device="cpu"
|
@@ -179,7 +181,9 @@ def main(input_article, input_trigger):
|
|
179 |
)
|
180 |
|
181 |
# split the input article to paragraphs in tmp csv format
|
182 |
-
data_test = split_article(input_article, input_trigger)
|
|
|
|
|
183 |
|
184 |
seed_val = 42
|
185 |
random.seed(seed_val)
|
@@ -264,6 +268,11 @@ def main(input_article, input_trigger):
|
|
264 |
merged_par_pred_df = pd.DataFrame({"paragraphs": data_test, "predictions": predictions}).to_csv("./util/experiments/par_with_class.csv")
|
265 |
# return updated_article, modified, merged_par_pred_df
|
266 |
modified_in_all = str(len(paragraphs_needed)) + " / " + str(len(data_test))
|
|
|
|
|
|
|
|
|
|
|
267 |
return updated_article, modified_in_all
|
268 |
|
269 |
def copy_to_clipboard(t):
|
@@ -271,6 +280,13 @@ def copy_to_clipboard(t):
|
|
271 |
t = f.read()
|
272 |
pyperclip.copy(t)
|
273 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
274 |
"""
|
275 |
demo = gr.Interface(
|
276 |
main,
|
@@ -329,25 +345,34 @@ with gr.Blocks() as demo:
|
|
329 |
input_2 = gr.Textbox(label="Triggered News Event", lines=2, placeholder="Input the triggered news event...")
|
330 |
btn = gr.Button(value="Submit")
|
331 |
with gr.Row():
|
332 |
-
output_1 = gr.Textbox(label="Updated Article", lines=
|
333 |
output_2 = gr.Textbox(label="#MODIFIED / #ALL")
|
334 |
btn.click(fn=main, inputs=[input_1, input_2], outputs=[output_1, output_2])
|
335 |
btn_copy = gr.Button(value="Copy Updated Article to Clipboard")
|
336 |
btn_copy.click(fn=copy_to_clipboard, inputs=[output_1], outputs=[])
|
337 |
gr.Markdown("## Input Examples")
|
|
|
338 |
gr.Examples(
|
339 |
examples=[[exin_1, trigger_1], [exin_2, trigger_2]],
|
340 |
fn=main,
|
341 |
inputs=[input_1, input_2],
|
342 |
outputs=[output_1, output_2],
|
343 |
-
# cache_examples=True
|
|
|
344 |
),
|
|
|
345 |
with gr.Tab("Compare between versions"):
|
346 |
btn_com = gr.Button(value="Differences Highlighting")
|
347 |
-
btn_com.click(fn=copy_to_clipboard, inputs=[output_1], outputs=[])
|
348 |
with gr.Row():
|
349 |
-
com_1 = gr.Textbox(label="Non-update Article", lines=
|
350 |
-
com_2 = gr.Textbox(label="Updated Article", lines=
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
351 |
gr.HTML("""
|
352 |
<div align="center">
|
353 |
<p>
|
|
|
113 |
def split_article(article, trigger):
|
114 |
if article.split("\n"): article = article.replace("\n", "\\\\c\\\\c")
|
115 |
paragraphs = article.replace("\\c\\c", "\c\c").split("\\\\c\\\\c")
|
116 |
+
format_pars = [par for par in paragraphs]
|
117 |
pars = [str(par) + " -- " + str(trigger) for par in paragraphs]
|
118 |
# pd.DataFrame({"paragraph": pars}).to_csv("./util/experiments/input_paragraphs.csv")
|
119 |
+
formatted_input = "\n".join(format_pars)
|
120 |
+
return pars, formatted_input
|
121 |
|
122 |
def config():
|
123 |
load_dotenv()
|
124 |
|
125 |
def call_gpt(paragraph, trigger):
|
126 |
# openai.api_key = os.environ.get("GPT_API")
|
127 |
+
openai.api_key = "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"
|
128 |
tokenizer = BartTokenizer.from_pretrained("theQuert/NetKUp-tokenzier")
|
129 |
inputs_for_gpt = f"""
|
130 |
As 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.
|
|
|
165 |
|
166 |
|
167 |
def main(input_article, input_trigger):
|
168 |
+
csv_path = "./util/experiments/input_paragraphs.csv"
|
169 |
+
if os.path.isfile(csv_path):
|
170 |
+
os.remove(csv_path)
|
171 |
modified = "TRUE"
|
172 |
# device = "cuda" if torch.cuda.is_available() else "cpu"
|
173 |
device="cpu"
|
|
|
181 |
)
|
182 |
|
183 |
# split the input article to paragraphs in tmp csv format
|
184 |
+
data_test, formatted_input = split_article(input_article, input_trigger)
|
185 |
+
with open("./util/experiments/formatted_input.txt", "w") as f:
|
186 |
+
f.write(formatted_input)
|
187 |
|
188 |
seed_val = 42
|
189 |
random.seed(seed_val)
|
|
|
268 |
merged_par_pred_df = pd.DataFrame({"paragraphs": data_test, "predictions": predictions}).to_csv("./util/experiments/par_with_class.csv")
|
269 |
# return updated_article, modified, merged_par_pred_df
|
270 |
modified_in_all = str(len(paragraphs_needed)) + " / " + str(len(data_test))
|
271 |
+
|
272 |
+
try:
|
273 |
+
os.remove("./util/experiments/classification.csv")
|
274 |
+
os.remove("./util/experiments/paragraphs_needed.csv")
|
275 |
+
except: pass
|
276 |
return updated_article, modified_in_all
|
277 |
|
278 |
def copy_to_clipboard(t):
|
|
|
280 |
t = f.read()
|
281 |
pyperclip.copy(t)
|
282 |
|
283 |
+
def compare_versions():
|
284 |
+
old, new = [], []
|
285 |
+
with open("./util/experiments/formatted_input.txt", "r") as f:
|
286 |
+
old = f.read()
|
287 |
+
with open("./util/experiments/updated_article.txt", "r") as f:
|
288 |
+
new = f.read()
|
289 |
+
return old, new
|
290 |
"""
|
291 |
demo = gr.Interface(
|
292 |
main,
|
|
|
345 |
input_2 = gr.Textbox(label="Triggered News Event", lines=2, placeholder="Input the triggered news event...")
|
346 |
btn = gr.Button(value="Submit")
|
347 |
with gr.Row():
|
348 |
+
output_1 = gr.Textbox(label="Updated Article", lines=5)
|
349 |
output_2 = gr.Textbox(label="#MODIFIED / #ALL")
|
350 |
btn.click(fn=main, inputs=[input_1, input_2], outputs=[output_1, output_2])
|
351 |
btn_copy = gr.Button(value="Copy Updated Article to Clipboard")
|
352 |
btn_copy.click(fn=copy_to_clipboard, inputs=[output_1], outputs=[])
|
353 |
gr.Markdown("## Input Examples")
|
354 |
+
gr.Markdown("### There are 2 examples below, click them to test inputs automatically!")
|
355 |
gr.Examples(
|
356 |
examples=[[exin_1, trigger_1], [exin_2, trigger_2]],
|
357 |
fn=main,
|
358 |
inputs=[input_1, input_2],
|
359 |
outputs=[output_1, output_2],
|
360 |
+
# cache_examples=True,
|
361 |
+
# run_on_click=True,
|
362 |
),
|
363 |
+
com_1_value, com_2_value = "Pls finish article updating, then click the button above", "Pls finish article updating, then click the button above."
|
364 |
with gr.Tab("Compare between versions"):
|
365 |
btn_com = gr.Button(value="Differences Highlighting")
|
|
|
366 |
with gr.Row():
|
367 |
+
com_1 = gr.Textbox(label="Non-update Article", value=com_1_value, lines=15)
|
368 |
+
com_2 = gr.Textbox(label="Updated Article", value=com_2_value, lines=15)
|
369 |
+
btn_com.click(fn=compare_versions, inputs=[], outputs=[com_1, com_2])
|
370 |
+
path_formatted_input = "./util/experiments/formatted_input.txt"
|
371 |
+
path_updated_article = "./util/experiments/updated_article.txt"
|
372 |
+
if os.path.isfile(path_formatted_input):
|
373 |
+
os.remove(path_formatted_input)
|
374 |
+
if os.path.isfile(path_updated_article):
|
375 |
+
os.remove(path_updated_article)
|
376 |
gr.HTML("""
|
377 |
<div align="center">
|
378 |
<p>
|