tunght commited on
Commit
6c45139
·
1 Parent(s): 46fcd8d

update reasoning_prompt

Browse files
Files changed (1) hide show
  1. app.py +59 -29
app.py CHANGED
@@ -24,6 +24,7 @@ from langchain_core.vectorstores import VectorStoreRetriever
24
 
25
  import time
26
  import weave
 
27
 
28
  from str2escaped import str2escaped
29
 
@@ -391,18 +392,34 @@ Return the reformatted structure only. Do not add any preceding or trailing char
391
 
392
  <structure>/n{structure}</structure>"""
393
 
394
- reasoning_prompt = """
395
- You are given information of a product, a reference structure, and a reference copy.
396
- Please analyze the structure, make a plan on how to follow the structure correctly.
397
- Use the tone of voice of the reference copy if available for the generated description.
398
- Do not hallucinate, do not add information that is not in the product information.
399
- Try your best to avoid using the excluded words and phrases.
400
- Try your best to include the included words and phrases in the product description.
401
- Make sure to keep to the requirements of the structure, writing no more or less than specified.
402
- If the structure does not specify a length, write a description that is between {min_length} and {max_length} words.
403
- Do not enclose the output in html tags, quotes, braces, brackets or anything.
404
- If there is a list in the output, put each item in the list on a separate line and use '-' character to start each item in a list.
405
- Return the product description only.
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
406
 
407
  <product_information>
408
  {key_features}
@@ -487,12 +504,16 @@ def get_json(text: str):
487
  return json.loads(text)
488
 
489
 
 
 
 
490
  @weave.op
491
  def detect_features(image_paths, garment_type, key_features, language="English"):
492
  # Path to your image
493
  # image_path = "path_to_your_image.jpg"
494
 
495
  # Getting the base64 string
 
496
  try:
497
  base64_images = [encode_image(image_path[0]) for image_path in image_paths]
498
  if garment_type == "" or garment_type == "all":
@@ -520,6 +541,30 @@ def detect_features(image_paths, garment_type, key_features, language="English")
520
  ] for base64_image in base64_images]
521
 
522
  response = chat.batch(messages)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
523
 
524
  print("image features")
525
  jresponse = {}
@@ -722,7 +767,8 @@ def generate(*data):
722
  # print("Languages--------------------------------------------\n", languages)
723
  # print("Types--------------------------------------------", types)
724
 
725
- image_features, base64_images = detect_features(image_paths=image,
 
726
  garment_type=garment_type,
727
  key_features=feature)
728
  detected_features = ""
@@ -993,24 +1039,8 @@ if __name__ == '__main__':
993
  print("detect_feature_prompt\n", str2escaped(detect_feature_prompt))
994
  print("-----------------------------------------------------------------")
995
 
996
-
997
- weave.init("Copywriting - Hugging Face", settings={"capture_code": False})
998
-
999
  with open("formatted_prompts.txt", "w") as f:
1000
  f.writelines('"write_struct_copy": "' + str2escaped(struct_copy_prompt).strip() + '",\n')
1001
  f.writelines('"write_copy": "' + str2escaped(copy_prompt).strip() + '",\n')
1002
  f.writelines('"write_struct": "' + str2escaped(struct_prompt).strip() + '",\n')
1003
  demo.launch()
1004
-
1005
- # print("write_prompt\n", str2escaped(write_prompt))
1006
- print("-----------------------------------------------------------------")
1007
- print("struct_copy_prompt\n", str2escaped(struct_copy_prompt))
1008
- print()
1009
- print("copy_prompt\n", str2escaped(copy_prompt))
1010
- print()
1011
- print("struct_prompt\n", str2escaped(struct_prompt))
1012
- print()
1013
- print("reasoning_prompt\n", str2escaped(reasoning_prompt))
1014
- print()
1015
- print("detect_feature_prompt\n", str2escaped(detect_feature_prompt))
1016
- print("-----------------------------------------------------------------")
 
24
 
25
  import time
26
  import weave
27
+ client = weave.init("Copywriting - Hugging Face", settings={"capture_code": False})
28
 
29
  from str2escaped import str2escaped
30
 
 
392
 
393
  <structure>/n{structure}</structure>"""
394
 
395
+ reasoning_prompt = """You are given:
396
+ - Product information
397
+ - A reference structure block written in a custom templating format
398
+ - Optional reference copy to match tone
399
+ - Lists of excluded and included phrases
400
+
401
+ Your task is to generate the content for the given block by strictly following its instructions.
402
+
403
+ ### How the structure block works:
404
+ - Content to be written is enclosed in double curly braces: {{ ... }}
405
+ - Control logic (like loops) is enclosed in {{% ... %}} . Logic should not be printed but just used as a control structure.
406
+ - Each block may define attributes such as tone, format, and length using a pipe: {{ block_name | tone, format, length }}
407
+ - Always follow the format (e.g., paragraph, bulleted list, inline list) as defined
408
+ - Tone and length constraints must be respected
409
+ - Lists must be formatted in standard Markdown (e.g., "- Item")
410
+ - There might also be other instructions inside the blocks, you should follow these as well
411
+
412
+ ### Tone
413
+ - Pay attention to the tone of voice instructions in the blocks.
414
+ - If a reference copy is provided, match its tone of voice, choice of words and sentence structure.
415
+ - If the reference and the tone instructions give a direction in tone and structure that is not compatible, rely on the structure more.
416
+
417
+ ### Rules:
418
+ - Use only information provided in the product data. Do not hallucinate or invent facts.
419
+ - Use as many of the included phrases as appropriate.
420
+ - Avoid all excluded phrases.
421
+ - Return only the generated content for the current block — no commentary or extra formatting.
422
+ - Do not wrap the output in quotes, HTML, brackets, or any other symbols.
423
 
424
  <product_information>
425
  {key_features}
 
504
  return json.loads(text)
505
 
506
 
507
+ from weave.trace.context.call_context import set_tracing_enabled
508
+
509
+
510
  @weave.op
511
  def detect_features(image_paths, garment_type, key_features, language="English"):
512
  # Path to your image
513
  # image_path = "path_to_your_image.jpg"
514
 
515
  # Getting the base64 string
516
+
517
  try:
518
  base64_images = [encode_image(image_path[0]) for image_path in image_paths]
519
  if garment_type == "" or garment_type == "all":
 
541
  ] for base64_image in base64_images]
542
 
543
  response = chat.batch(messages)
544
+
545
+ with set_tracing_enabled(True):
546
+ messages_no_images = [[
547
+ {
548
+ "role": "user",
549
+ "content": [
550
+ {
551
+ "type": "text",
552
+ "text": detect_feature_prompt.format(
553
+ garment_type=garment_type,
554
+ key_features=key_features)
555
+ },
556
+ {
557
+ "type": "image_url",
558
+ "image_url": {
559
+ "url": f"{image_path[0]}"
560
+ }
561
+ }]
562
+ }
563
+ ] for image_path in image_paths]
564
+
565
+ call = client.create_call("detect_features_prompts",
566
+ inputs={"messages": messages_no_images})
567
+ client.finish_call(call=call, output=response)
568
 
569
  print("image features")
570
  jresponse = {}
 
767
  # print("Languages--------------------------------------------\n", languages)
768
  # print("Types--------------------------------------------", types)
769
 
770
+ with set_tracing_enabled(False):
771
+ image_features, base64_images = detect_features(image_paths=image,
772
  garment_type=garment_type,
773
  key_features=feature)
774
  detected_features = ""
 
1039
  print("detect_feature_prompt\n", str2escaped(detect_feature_prompt))
1040
  print("-----------------------------------------------------------------")
1041
 
 
 
 
1042
  with open("formatted_prompts.txt", "w") as f:
1043
  f.writelines('"write_struct_copy": "' + str2escaped(struct_copy_prompt).strip() + '",\n')
1044
  f.writelines('"write_copy": "' + str2escaped(copy_prompt).strip() + '",\n')
1045
  f.writelines('"write_struct": "' + str2escaped(struct_prompt).strip() + '",\n')
1046
  demo.launch()