Spaces:
Paused
Paused
Add guidance prompt as a post-processing step
Browse files
app.py
CHANGED
|
@@ -77,6 +77,9 @@ structure_text_1 = """[type: UK website, style=true, language=English]
|
|
| 77 |
structure_text_2 = """[type: Japanese newsletter, style=true, language=Japanese]
|
| 78 |
{{ introduction_paragraph | motivational, passionate, 3-6 sentences }}"""
|
| 79 |
|
|
|
|
|
|
|
|
|
|
| 80 |
languages = ["American English",
|
| 81 |
"British English",
|
| 82 |
"German",
|
|
@@ -236,11 +239,24 @@ def get_language(struct_lang, copy_lang):
|
|
| 236 |
return "English"
|
| 237 |
|
| 238 |
|
| 239 |
-
def post_process(text):
|
| 240 |
-
|
| 241 |
-
|
| 242 |
-
|
| 243 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 244 |
return text
|
| 245 |
|
| 246 |
|
|
@@ -306,8 +322,8 @@ def generate(*data):
|
|
| 306 |
global visible
|
| 307 |
print("visible", visible)
|
| 308 |
|
| 309 |
-
nargs =
|
| 310 |
-
feature, image, garment_type, model, temperature, nversions, excluded_words, included_words, glossary_upload, debug = data[:nargs]
|
| 311 |
struct_ref = data[nargs:]
|
| 312 |
|
| 313 |
print(f"{feature=}")
|
|
@@ -572,7 +588,7 @@ Excluded words: {excluded_words}"""),]
|
|
| 572 |
bestid = jr["best_version"]["id"]
|
| 573 |
for d in jr["versions"]:
|
| 574 |
if d["id"] == bestid:
|
| 575 |
-
bestd =
|
| 576 |
bests = d["score"]
|
| 577 |
break
|
| 578 |
# bests = 0
|
|
@@ -603,7 +619,15 @@ Excluded words: {excluded_words}"""),]
|
|
| 603 |
|
| 604 |
alt_text_dict = {k[0]: v for (k, v) in zip(image, alt_texts)} if len(alt_texts) > 0 else {}
|
| 605 |
result_json = {"outputs": jresponse if debug else descriptions, "alt_text": alt_text_dict}
|
| 606 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 607 |
return result_md, result_json
|
| 608 |
|
| 609 |
|
|
@@ -668,6 +692,7 @@ with gr.Blocks() as demo:
|
|
| 668 |
|
| 669 |
with gr.Column():
|
| 670 |
visible = 0
|
|
|
|
| 671 |
struct_ref = [val for i in range(10) for val in
|
| 672 |
[gr.Textbox(label=f"Structure {i}", lines=10, value="", interactive=True, visible=i <= visible),
|
| 673 |
gr.Textbox(label=f"Reference copy {i}", lines=3, value="", interactive=True, visible=i <= visible)]]
|
|
@@ -683,7 +708,7 @@ with gr.Blocks() as demo:
|
|
| 683 |
md_output = gr.Markdown(label="Output", show_label=True, line_breaks=True)
|
| 684 |
json_output = gr.JSON(label="JSON Output")
|
| 685 |
submit.click(generate, inputs=[feature, image, garment_type, model, temperature, nversions,
|
| 686 |
-
excluded_words, included_words, glossary_upload, debug, *struct_ref],
|
| 687 |
outputs=[md_output, json_output])
|
| 688 |
# advanced.click(show_advanced, inputs=[], outputs=[model, temperature])
|
| 689 |
|
|
|
|
| 77 |
structure_text_2 = """[type: Japanese newsletter, style=true, language=Japanese]
|
| 78 |
{{ introduction_paragraph | motivational, passionate, 3-6 sentences }}"""
|
| 79 |
|
| 80 |
+
guidance_prompt_text = """If it is made of 100% cotton, say "pure cotton" instead.
|
| 81 |
+
If it is lightweight, say "featherlight" instead."""
|
| 82 |
+
|
| 83 |
languages = ["American English",
|
| 84 |
"British English",
|
| 85 |
"German",
|
|
|
|
| 239 |
return "English"
|
| 240 |
|
| 241 |
|
| 242 |
+
def post_process(text: str, guidance_prompt: str, chat: ChatOpenAI):
|
| 243 |
+
messages = [
|
| 244 |
+
SystemMessage(content=f"""You are a helpful assistant that edit documents based on the guidlines provided. Make sure to write in English language."""),
|
| 245 |
+
HumanMessage(content=f"""Given the following product description, your task is to
|
| 246 |
+
make minimal modification to the product description such that the resulting description
|
| 247 |
+
follows the rules defined in the guidlines. Make sure to preserve the structure of the
|
| 248 |
+
original text as much as possible. Do not modify the structure of the original text.
|
| 249 |
+
Output only the modified text in markdown format.
|
| 250 |
+
|
| 251 |
+
Text:
|
| 252 |
+
{text}
|
| 253 |
+
|
| 254 |
+
Guildlines:
|
| 255 |
+
{guidance_prompt}""")
|
| 256 |
+
]
|
| 257 |
+
|
| 258 |
+
response = chat.invoke(messages, temperature=0.0)
|
| 259 |
+
text = response.content
|
| 260 |
return text
|
| 261 |
|
| 262 |
|
|
|
|
| 322 |
global visible
|
| 323 |
print("visible", visible)
|
| 324 |
|
| 325 |
+
nargs = 11
|
| 326 |
+
feature, image, garment_type, model, temperature, nversions, excluded_words, included_words, glossary_upload, debug, guidance_prompt = data[:nargs]
|
| 327 |
struct_ref = data[nargs:]
|
| 328 |
|
| 329 |
print(f"{feature=}")
|
|
|
|
| 588 |
bestid = jr["best_version"]["id"]
|
| 589 |
for d in jr["versions"]:
|
| 590 |
if d["id"] == bestid:
|
| 591 |
+
bestd = d["content"] + (f"\n\nDebug info:\n\nScore: {d['score']}\n\nExplanation: {jr['best_version']['explanation']}" if debug else "")
|
| 592 |
bests = d["score"]
|
| 593 |
break
|
| 594 |
# bests = 0
|
|
|
|
| 619 |
|
| 620 |
alt_text_dict = {k[0]: v for (k, v) in zip(image, alt_texts)} if len(alt_texts) > 0 else {}
|
| 621 |
result_json = {"outputs": jresponse if debug else descriptions, "alt_text": alt_text_dict}
|
| 622 |
+
|
| 623 |
+
post_content = post_process(text=md_content, guidance_prompt=guidance_prompt, chat=chat)
|
| 624 |
+
|
| 625 |
+
result_md = "Original\n\n" + md_content + "\n\n---\n\nModified\n\n" + post_content + "\n\n" \
|
| 626 |
+
+ alt_texts_str \
|
| 627 |
+
+ '\n'.join([f'' if base64_image != "" else "" for (base64_image, alt_text) in zip(base64_images, alt_texts)])
|
| 628 |
+
|
| 629 |
+
|
| 630 |
+
|
| 631 |
return result_md, result_json
|
| 632 |
|
| 633 |
|
|
|
|
| 692 |
|
| 693 |
with gr.Column():
|
| 694 |
visible = 0
|
| 695 |
+
guidance_prompt = gr.Textbox(label="Guidance Prompt", value=guidance_prompt_text, lines=3, interactive=True)
|
| 696 |
struct_ref = [val for i in range(10) for val in
|
| 697 |
[gr.Textbox(label=f"Structure {i}", lines=10, value="", interactive=True, visible=i <= visible),
|
| 698 |
gr.Textbox(label=f"Reference copy {i}", lines=3, value="", interactive=True, visible=i <= visible)]]
|
|
|
|
| 708 |
md_output = gr.Markdown(label="Output", show_label=True, line_breaks=True)
|
| 709 |
json_output = gr.JSON(label="JSON Output")
|
| 710 |
submit.click(generate, inputs=[feature, image, garment_type, model, temperature, nversions,
|
| 711 |
+
excluded_words, included_words, glossary_upload, debug, guidance_prompt, *struct_ref],
|
| 712 |
outputs=[md_output, json_output])
|
| 713 |
# advanced.click(show_advanced, inputs=[], outputs=[model, temperature])
|
| 714 |
|