tunght commited on
Commit
b8505ee
·
1 Parent(s): f2bbca9

Add auto structure improvement

Browse files
Files changed (2) hide show
  1. app.py +22 -4
  2. str2escaped.py +18 -7
app.py CHANGED
@@ -286,6 +286,13 @@ Included words: {included_phrases}
286
  Excluded words: {excluded_phrases}"""
287
 
288
 
 
 
 
 
 
 
 
289
  import base64
290
  import requests
291
 
@@ -433,7 +440,7 @@ Do not add any preceding or trailing text to the output.
433
  Product description:
434
  {text}
435
 
436
- Guildlines:
437
  {guidance_prompt}""")
438
  ]
439
 
@@ -500,6 +507,15 @@ Text to rewrite:
500
  return ""
501
 
502
 
 
 
 
 
 
 
 
 
 
503
  def generate(*data):
504
  global visible
505
  print("visible", visible)
@@ -564,6 +580,8 @@ def generate(*data):
564
  if len(copy.strip()) > 0 and len(structure.strip()) > 0:
565
  print('------------')
566
  print("Using both copy and structure")
 
 
567
  messages = [
568
  SystemMessage(content=f"""You are a helpful assistant that writes about products for ecommerce websites. Make sure to write in {languages[i]} language."""),
569
  HumanMessage(content=struct_copy_prompt.format(nversions=nversions, min_length=min_length, max_length=max_length, key_features=key_features, structure=structure, copy=copy, included_phrases=included_phrases, excluded_phrases=excluded_phrases)),]
@@ -748,7 +766,7 @@ def authf(username, password):
748
  if __name__ == '__main__':
749
  # demo.launch(server_name="0.0.0.0", auth=authf)
750
  with open("formatted_prompts.txt", "w") as f:
751
- f.writelines('"write_struct_copy": "' + str2escaped(struct_copy_prompt).strip() + '",')
752
- f.writelines('"write_copy": "' + str2escaped(copy_prompt).strip() + '",')
753
- f.writelines('"write_struct": "' + str2escaped(struct_prompt).strip() + '",')
754
  demo.launch()
 
286
  Excluded words: {excluded_phrases}"""
287
 
288
 
289
+ improve_structure_prompt = """You are given a structure for a product description.
290
+ Reformat the structure so that it is easier to read and understand for human and AI.
291
+ Return the reformatted structure only. Do not add any preceding or trailing characters.
292
+
293
+ <structure>/n{structure}</structure>"""
294
+
295
+
296
  import base64
297
  import requests
298
 
 
440
  Product description:
441
  {text}
442
 
443
+ Guidelines:
444
  {guidance_prompt}""")
445
  ]
446
 
 
507
  return ""
508
 
509
 
510
+ def improve_structure(chat: ChatOpenAI, structure: str):
511
+ messages = [
512
+ HumanMessage(content=improve_structure_prompt.format(structure=structure)),]
513
+ response = chat.invoke(messages)
514
+ print("Original structure: ", structure)
515
+ print("Improved structure: ", response.content)
516
+ return response.content
517
+
518
+
519
  def generate(*data):
520
  global visible
521
  print("visible", visible)
 
580
  if len(copy.strip()) > 0 and len(structure.strip()) > 0:
581
  print('------------')
582
  print("Using both copy and structure")
583
+ print("Improving structure")
584
+ structure = improve_structure(chat=chat, structure=structure)
585
  messages = [
586
  SystemMessage(content=f"""You are a helpful assistant that writes about products for ecommerce websites. Make sure to write in {languages[i]} language."""),
587
  HumanMessage(content=struct_copy_prompt.format(nversions=nversions, min_length=min_length, max_length=max_length, key_features=key_features, structure=structure, copy=copy, included_phrases=included_phrases, excluded_phrases=excluded_phrases)),]
 
766
  if __name__ == '__main__':
767
  # demo.launch(server_name="0.0.0.0", auth=authf)
768
  with open("formatted_prompts.txt", "w") as f:
769
+ f.writelines('"write_struct_copy": "' + str2escaped(struct_copy_prompt).strip() + '",\n')
770
+ f.writelines('"write_copy": "' + str2escaped(copy_prompt).strip() + '",\n')
771
+ f.writelines('"write_struct": "' + str2escaped(struct_prompt).strip() + '",\n')
772
  demo.launch()
str2escaped.py CHANGED
@@ -193,15 +193,26 @@ Excluded words: {excluded_words}"""
193
 
194
  test_prompt = "Generate {nversions} versions of the product description for a product with the following information.\nWrite in a way that target the customer.\nFor each version, write at least {min_length} words and at most {max_length} words.\nMake sure that the structure of each output follows the reference structure. Do not add any additional sentences or structures that are not in the reference structure.\nMake sure to use the tone of voice, rythm, cadence and style of the reference copy for each output.\nUse markdown format for each output.\nDo not include any part of the reference structure in the output.\nThe structure of the output should follow the reference structure.\nDo not use the structure of the reference copy in the output.\nDo not use any of the excluded words in the output.\nInclude all included words in the output.\nDo not hallucinate any information. Do not hallucinate information about size and weight. Write about size and weight only if it is available in the list of features.\nUse creative language in each output, do not use the common ways of starting product descriptions.\nAvoid common phrases and cliches such as \"Step into something\", \"Elevate something\", \"Discover something\", \"Unleash something\", \"Embrace something\", and similar phrases.\nFor each version, try to write in different style.\nPut each item in the list on a separate line. Use '-' character to start each item. \nRate the quality of each version based on the following criteria:\n - how well it follows the reference structure.\n - how well it follows the tone of voice, rythm, cadence and style of the reference copy.\n - how faithful it describes the product features.\n - how well it avoid the excluded words.\n - how well it includes the included words.\n - how creative the language is.\nThe score should be a number between 0 and 10 with 10 being the best quality.\nReturn the result in the following JSON format:\n{{\n \"versions\": [\n {{\n \"id\": 1,\n \"content\": The first product description,\n \"explanation\": A less than 20 word explanation of the score of the first product description,\n \"score\": The score of the first product description\n }},\n {{\n \"id\": 2,\n \"content\": The second product description,\n \"explanation\": A less than 20 word explanation of the score of the first product description,\n \"score\": The score of the second product description\n }},\n ...\n ],\n \"best_version\": {{\n \"explanation\": Explanation for why this version is the best,\n \"id\": The id of the best version\n }}\n}}\nMake sure that the output is in JSON format, no extra text should be included in the output.\n\nProduct information:\nKey features: {key_features}\n\nReference structure: {structure}\n\nReference copy: {copy}\n\nIncluded words: {included_phrases}\n\nExcluded words: {excluded_phrases}"
195
 
 
 
 
 
 
 
 
 
 
 
196
  import codecs
197
  if __name__ == "__main__":
198
- print()
199
- print('"write_struct_copy": "' + str2escaped(both).strip() + '",')
200
- print()
201
- print('"write_copy": "' + str2escaped(copy).strip() + '",')
202
- print()
203
- print('"write_struct": "' + str2escaped(struct).strip() + '",')
204
- print()
 
205
  # print('test\n', str2escaped(test))
206
 
207
 
 
193
 
194
  test_prompt = "Generate {nversions} versions of the product description for a product with the following information.\nWrite in a way that target the customer.\nFor each version, write at least {min_length} words and at most {max_length} words.\nMake sure that the structure of each output follows the reference structure. Do not add any additional sentences or structures that are not in the reference structure.\nMake sure to use the tone of voice, rythm, cadence and style of the reference copy for each output.\nUse markdown format for each output.\nDo not include any part of the reference structure in the output.\nThe structure of the output should follow the reference structure.\nDo not use the structure of the reference copy in the output.\nDo not use any of the excluded words in the output.\nInclude all included words in the output.\nDo not hallucinate any information. Do not hallucinate information about size and weight. Write about size and weight only if it is available in the list of features.\nUse creative language in each output, do not use the common ways of starting product descriptions.\nAvoid common phrases and cliches such as \"Step into something\", \"Elevate something\", \"Discover something\", \"Unleash something\", \"Embrace something\", and similar phrases.\nFor each version, try to write in different style.\nPut each item in the list on a separate line. Use '-' character to start each item. \nRate the quality of each version based on the following criteria:\n - how well it follows the reference structure.\n - how well it follows the tone of voice, rythm, cadence and style of the reference copy.\n - how faithful it describes the product features.\n - how well it avoid the excluded words.\n - how well it includes the included words.\n - how creative the language is.\nThe score should be a number between 0 and 10 with 10 being the best quality.\nReturn the result in the following JSON format:\n{{\n \"versions\": [\n {{\n \"id\": 1,\n \"content\": The first product description,\n \"explanation\": A less than 20 word explanation of the score of the first product description,\n \"score\": The score of the first product description\n }},\n {{\n \"id\": 2,\n \"content\": The second product description,\n \"explanation\": A less than 20 word explanation of the score of the first product description,\n \"score\": The score of the second product description\n }},\n ...\n ],\n \"best_version\": {{\n \"explanation\": Explanation for why this version is the best,\n \"id\": The id of the best version\n }}\n}}\nMake sure that the output is in JSON format, no extra text should be included in the output.\n\nProduct information:\nKey features: {key_features}\n\nReference structure: {structure}\n\nReference copy: {copy}\n\nIncluded words: {included_phrases}\n\nExcluded words: {excluded_phrases}"
195
 
196
+ detect_feature = """Describe the features of the {garment_type} in the photo in less than 150 words.
197
+ Return the result in in the following JSON format without any preceding or trailing text:
198
+
199
+ {{
200
+ "features": [list of comma separated features],
201
+ "intended_use": [list of comma separated intended uses],
202
+ "alt_text": alt text for the image,
203
+ "category": the category of the garment, for example 'dress', 'shirt', 'pants', 'shoes', etc
204
+ }}"""
205
+
206
  import codecs
207
  if __name__ == "__main__":
208
+ print(str2escaped(detect_feature))
209
+ # print()
210
+ # print('"write_struct_copy": "' + str2escaped(both).strip() + '",')
211
+ # print()
212
+ # print('"write_copy": "' + str2escaped(copy).strip() + '",')
213
+ # print()
214
+ # print('"write_struct": "' + str2escaped(struct).strip() + '",')
215
+ # print()
216
  # print('test\n', str2escaped(test))
217
 
218