Hoang Thanh Tung commited on
Commit
dd59603
·
1 Parent(s): a4c28a5

Add languages, json. Fix 'here is your copy'

Browse files
Files changed (1) hide show
  1. app.py +96 -29
app.py CHANGED
@@ -22,7 +22,7 @@ structure = \
22
 
23
  ## Features and Benefits
24
  {% for feature in features %}
25
- ### Feature {{ loop.index }}: {{ feature.name | dynamic, direct, 5-6 words }}
26
  {{ feature.details | energetic, clear, 3-4 sentences }}
27
  {% endfor %}
28
 
@@ -36,6 +36,24 @@ reference = " "
36
 
37
  garment_type = "all"
38
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
39
  import base64
40
  import requests
41
 
@@ -61,7 +79,7 @@ def get_json(text: str):
61
  return json.loads(text)
62
 
63
 
64
- def detect_features(image_paths, garment_type):
65
  # Path to your image
66
  # image_path = "path_to_your_image.jpg"
67
 
@@ -87,13 +105,14 @@ def detect_features(image_paths, garment_type):
87
  "text": """Describe the features of the %s in the photos in less than 100 words.
88
  What is the intended use of the %s in this image, use at most 5 words for intended use?
89
  Generate alt text for each of the images.
 
90
  If the photo does not contain a garment, return 'No garment detected'.
91
  If the photo contains a garment, return the result in in the following JSON format without any preceding or trailing text:
92
  {
93
  "features": [list of comma separated features],
94
  "intended_use": [list of comma separated intended uses],
95
  "alt_text": [alt text for image 1, alt text for image 2]
96
- }""" % (garment_type, garment_type)
97
  },
98
  ] + [{
99
  "type": "image_url",
@@ -119,9 +138,9 @@ def detect_features(image_paths, garment_type):
119
  return "", []
120
 
121
 
122
- def generate(features, image, garment_type, structure, reference, model, temperature):
123
- print(f"features {features},\n image {image},\n structure{structure},\n model{model},\n temperature {temperature},\n reference {reference}")
124
- image_features, base64_images = detect_features(image, garment_type)
125
  detected_features = ""
126
  intended_use = ""
127
  alt_texts = []
@@ -136,40 +155,88 @@ def generate(features, image, garment_type, structure, reference, model, tempera
136
  chat = ChatAnthropic(model_name=model, anthropic_api_key=os.environ["ANTHROPIC_API_KEY"])
137
  else:
138
  chat = ChatGroq(model_name=model, api_key=os.environ["GROQ_API_KEY"])
139
- messages = [HumanMessage(content=f"""Write a product description of about 200 words for a product with the following key properties.
 
 
 
 
 
 
 
 
 
 
 
 
140
  Make sure that the description follows the structure of the reference structure below.
141
  Make sure to use markdown format for the output.
142
- Output the product description only.
 
143
  Use creative language that is suitable for e-commerce websites. Use a consistent tone of voice throughout the text.
144
  If the reference text is not empty, produce the product description in the tone of voice and structure of the reference text.
145
  \n\n {features + detected_features} \n{intended_use} \nReference structure: {structure}\n Reference text: {reference}""")]
146
- chat_response = chat.invoke(messages, temperature=temperature)
147
- print(messages[0].content)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
148
  alt_texts_str = '\n\n### Alt text\n\n' + '\n- ' + '\n- '.join(alt_texts) if len(alt_texts) > 0 else ""
149
- result_text = chat_response.content
150
- result_md = chat_response.content + alt_texts_str + '\n'.join([f'![Product photo](data:image/png;base64,{base64_image} "{alt_text}")' if base64_image != "" else "" for (base64_image, alt_text) in zip(base64_images, alt_texts)])
151
- return result_md, result_text
 
 
152
 
153
 
154
- demo = gr.Interface(
155
- fn=generate,
156
- inputs=[gr.Textbox(features, label="Features"),
157
- gr.Gallery(label="Product image(s)", type="filepath"),
158
- gr.Textbox(garment_type, label="Garment type"),
159
- gr.Textbox(structure, label="Structure"),
160
- gr.Textbox(reference, label="Reference copy"),
161
- gr.Dropdown(["gpt-4-turbo", "gpt-3.5-turbo", "claude-3-sonnet-20240229", "claude-3-opus-20240229", "llama3-70b-8192"], value="llama3-70b-8192", label="Model"),
162
- gr.Slider(minimum=0., maximum=1.0, value=0.5, label="Temperature")],
163
- outputs=[gr.Markdown(label="Markdown"), gr.Textbox(label="Raw text")],
164
- )
165
 
166
- # demo.launch()
167
 
168
- import argparse
 
 
 
 
 
 
 
 
 
 
169
 
170
- parser = argparse.ArgumentParser(description='Run the Gradio demo')
171
- parser.add_argument('--public', action='store_true', help='Expose the demo to the public')
172
- args = parser.parse_args()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
173
 
174
  # if args.public:
175
  # demo.launch(share=True)
 
22
 
23
  ## Features and Benefits
24
  {% for feature in features %}
25
+ ### {{ loop.index }}: {{ feature.name | dynamic, direct, 5-6 words }}
26
  {{ feature.details | energetic, clear, 3-4 sentences }}
27
  {% endfor %}
28
 
 
36
 
37
  garment_type = "all"
38
 
39
+ languages = ["American English",
40
+ "British English",
41
+ "German",
42
+ "French",
43
+ "Chinese",
44
+ "Spanish",
45
+ "Dutch",
46
+ "Italian",
47
+ "Japanese",
48
+ "Polish",
49
+ "Portuguese"]
50
+
51
+ models = ["gpt-4-turbo",
52
+ "gpt-3.5-turbo",
53
+ "claude-3-sonnet-20240229",
54
+ "claude-3-opus-20240229",
55
+ "llama3-70b-8192"]
56
+
57
  import base64
58
  import requests
59
 
 
79
  return json.loads(text)
80
 
81
 
82
+ def detect_features(image_paths, garment_type, language):
83
  # Path to your image
84
  # image_path = "path_to_your_image.jpg"
85
 
 
105
  "text": """Describe the features of the %s in the photos in less than 100 words.
106
  What is the intended use of the %s in this image, use at most 5 words for intended use?
107
  Generate alt text for each of the images.
108
+ Make sure to output the alt text in %s language.
109
  If the photo does not contain a garment, return 'No garment detected'.
110
  If the photo contains a garment, return the result in in the following JSON format without any preceding or trailing text:
111
  {
112
  "features": [list of comma separated features],
113
  "intended_use": [list of comma separated intended uses],
114
  "alt_text": [alt text for image 1, alt text for image 2]
115
+ }""" % (garment_type, garment_type, language)
116
  },
117
  ] + [{
118
  "type": "image_url",
 
138
  return "", []
139
 
140
 
141
+ def generate(features, image, garment_type, structure, reference, model, language, output_types, temperature):
142
+ print(f"features {features},\n image {image},\n structure{structure},\n model{model},\n language{language},\n temperature {temperature},\n reference {reference}")
143
+ image_features, base64_images = detect_features(image, garment_type, language)
144
  detected_features = ""
145
  intended_use = ""
146
  alt_texts = []
 
155
  chat = ChatAnthropic(model_name=model, anthropic_api_key=os.environ["ANTHROPIC_API_KEY"])
156
  else:
157
  chat = ChatGroq(model_name=model, api_key=os.environ["GROQ_API_KEY"])
158
+ desc_messages = [
159
+ SystemMessage(content=f"""You are a helpful assistant that writes product descriptions for ecommerce websites. You write in {language} language."""),
160
+ HumanMessage(content=f"""Write a product description of about 200 words for a product with the following key properties.
161
+ Make sure that the description follows the structure of the reference structure below.
162
+ Make sure to use markdown format for the output.
163
+ Make sure that the entire output is written entirely in {language} language.
164
+ Output the product description only, do not include any preceeding text like "Here is your product description".
165
+ Use creative language that is suitable for e-commerce websites. Use a consistent tone of voice throughout the text.
166
+ If the reference text is not empty, produce the product description in the tone of voice and structure of the reference text.
167
+ \n\n {features + detected_features} \n{intended_use} \nReference structure: {structure}\n Reference text: {reference}""")]
168
+ news_messages = [
169
+ SystemMessage(content=f"""You are a helpful assistant that writes news letters for ecommerce websites. You write in {language} language."""),
170
+ HumanMessage(content=f"""Write a news letter of about 200 words for a product with the following key properties.
171
  Make sure that the description follows the structure of the reference structure below.
172
  Make sure to use markdown format for the output.
173
+ Make sure that the entire output is written entirely in {language} language.
174
+ Output the product description only, do not include any preceeding text like "Here is your product description".
175
  Use creative language that is suitable for e-commerce websites. Use a consistent tone of voice throughout the text.
176
  If the reference text is not empty, produce the product description in the tone of voice and structure of the reference text.
177
  \n\n {features + detected_features} \n{intended_use} \nReference structure: {structure}\n Reference text: {reference}""")]
178
+ all_messages = [
179
+ SystemMessage(content=f"""You are a helpful assistant that writes news letters for ecommerce websites. You write in {language} language."""),
180
+ HumanMessage(content=f"""Write a news letter and a product description of about 200 words for a product with the following key properties.
181
+ Make sure that the description follows the structure of the reference structure below.
182
+ Make sure to use markdown format for the output.
183
+ Make sure that the entire output is written entirely in {language} language.
184
+ Output the product description only, do not include any preceeding text like "Here is your product description".
185
+ Use creative language that is suitable for e-commerce websites. Use a consistent tone of voice throughout the text.
186
+ If the reference text is not empty, produce the news letter and product description in the tone of voice and structure of the reference text.
187
+ Return the result in the following JSON format: {{"description": Product description, "news_letter": News letter}}.
188
+ \n\n {features + detected_features} \n{intended_use} \nReference structure: {structure}\n Reference text: {reference}""")]
189
+
190
+ print(output_types)
191
+
192
+ description = ""
193
+
194
+ print(all_messages[1].content)
195
+
196
+ response = chat.invoke(desc_messages, temperature=temperature)
197
+ description = response.content
198
+ md_content = description
199
+
200
  alt_texts_str = '\n\n### Alt text\n\n' + '\n- ' + '\n- '.join(alt_texts) if len(alt_texts) > 0 else ""
201
+
202
+ alt_text_dict = {k[0]: v for (k, v) in zip(image, alt_texts)} if len(alt_texts) > 0 else {}
203
+ result_json = {"description": description, "alt_text": alt_text_dict, "language": language}
204
+ result_md = md_content + alt_texts_str + '\n'.join([f'![Product photo](data:image/png;base64,{base64_image} "{alt_text}")' if base64_image != "" else "" for (base64_image, alt_text) in zip(base64_images, alt_texts)])
205
+ return result_md, result_json
206
 
207
 
 
 
 
 
 
 
 
 
 
 
 
208
 
 
209
 
210
+ # demo = gr.Interface(
211
+ # fn=generate,
212
+ # inputs=[gr.Textbox(features, label="Features"),
213
+ # gr.Gallery(label="Product image(s)", type="filepath"),
214
+ # gr.Textbox(garment_type, label="Garment type"),
215
+ # gr.Textbox(structure, label="Structure"),
216
+ # gr.Textbox(reference, label="Reference copy"),
217
+ # gr.Dropdown(["gpt-4-turbo", "gpt-3.5-turbo", "claude-3-sonnet-20240229", "claude-3-opus-20240229", "llama3-70b-8192"], value="llama3-70b-8192", label="Model"),
218
+ # gr.Slider(minimum=0., maximum=1.0, value=0.5, label="Temperature")],
219
+ # outputs=[gr.Markdown(label="Markdown"), gr.Textbox(label="Raw text")],
220
+ # )
221
 
222
+ with gr.Blocks() as demo:
223
+ inputs=[gr.Textbox(features, label="Features", lines=3, interactive=True),
224
+ gr.Gallery(label="Product image(s)", type="filepath"),
225
+ gr.Textbox(garment_type, label="Garment type"),
226
+ gr.Textbox(structure, label="Structure", lines=10, interactive=True),
227
+ gr.Textbox(reference, label="Reference copy", lines=3, interactive=True),
228
+ gr.Dropdown(models, value="claude-3-sonnet-20240229", label="Model"),
229
+ gr.Dropdown(languages, label="Language", value="American English"),
230
+ gr.Slider(minimum=0., maximum=1.0, value=0.5, label="Temperature")]
231
+ outputs=[gr.Markdown(label="Markdown"), gr.JSON(label="JSON")]
232
+ submit_button = gr.Button("Submit")
233
+ submit_button.click(fn=generate, inputs=inputs, outputs=outputs)
234
+
235
+ # import argparse
236
+
237
+ # parser = argparse.ArgumentParser(description='Run the Gradio demo')
238
+ # parser.add_argument('--public', action='store_true', help='Expose the demo to the public')
239
+ # args = parser.parse_args()
240
 
241
  # if args.public:
242
  # demo.launch(share=True)