tunght commited on
Commit
744894f
1 Parent(s): 5a20b9e

add glossary function

Browse files
Files changed (2) hide show
  1. app.py +32 -20
  2. fashion_glossary.csv +1 -0
app.py CHANGED
@@ -216,7 +216,7 @@ def get_language(struct_lang, copy_lang):
216
 
217
  def get_model(model_name):
218
  if model_name.startswith("gpt"):
219
- chat = ChatOpenAI(model=model_name, max_tokens=8192)
220
  elif model_name.startswith("claude"):
221
  chat = ChatAnthropic(model_name=model_name, anthropic_api_key=os.environ["ANTHROPIC_API_KEY"], max_tokens_to_sample=4096)
222
  else:
@@ -237,29 +237,37 @@ def build_glossary(glossary_file, fieldnames=None) -> VectorStoreRetriever:
237
  return retriever
238
 
239
 
240
- def glossary_rewrite(glossary: VectorStoreRetriever, text: str):
241
  try:
242
  terms = glossary.invoke(input=text)
243
  print("\n".join([d.page_content for d in terms]))
244
  glossary_str = "\n\n".join([d.page_content.replace('\n', '. ') for d in terms])
245
- except Exception as e:
246
- print(e.__class__, e)
247
- traceback.print_exc()
248
- terms = []
249
 
250
- if len(terms) > 0:
251
- messages = [
252
- SystemMessage(content=f"""You are a helpful assistant that writes about products for ecommerce websites. Make sure to write in English language."""),
253
- HumanMessage(content=f"""Rewrite the following text using the terms in the glossary.
254
  Preserve the original text as much as possible.
255
  Replace the terms in original text that match the definition with the corresponding terms in the glossary.
 
256
 
257
  Terms, Definitions
258
- {glossary_str}
259
- """),
260
- ]
 
 
 
 
 
 
 
 
 
 
 
261
 
262
- print(f"HumanMessage={messages[1].content}")
263
 
264
 
265
  def generate(*data):
@@ -307,11 +315,11 @@ def generate(*data):
307
  intended_use = "Intended use: " + ", ".join(image_features["intended_use"])
308
  print(f"Detected features: {detected_features}, Intended use: {intended_use}, Alt text: {alt_texts}")
309
 
310
- if glossary:
311
- print("Getting terms")
312
- terms = glossary.invoke(input=feature + detected_features)
313
- for term in terms:
314
- print(term)
315
 
316
  batch = []
317
  for i in range(visible + 1):
@@ -518,7 +526,11 @@ Excluded words: {excluded_words}"""),]
518
 
519
  descriptions.append(bestd)
520
 
521
- # glossary_rewrite(glossary, descriptions[0])
 
 
 
 
522
 
523
  md_content = "\n\n---\n\n".join(descriptions)
524
 
 
216
 
217
  def get_model(model_name):
218
  if model_name.startswith("gpt"):
219
+ chat = ChatOpenAI(model=model_name, max_tokens=4096)
220
  elif model_name.startswith("claude"):
221
  chat = ChatAnthropic(model_name=model_name, anthropic_api_key=os.environ["ANTHROPIC_API_KEY"], max_tokens_to_sample=4096)
222
  else:
 
237
  return retriever
238
 
239
 
240
+ def glossary_rewrite(chat: ChatOpenAI, glossary: VectorStoreRetriever, text: str):
241
  try:
242
  terms = glossary.invoke(input=text)
243
  print("\n".join([d.page_content for d in terms]))
244
  glossary_str = "\n\n".join([d.page_content.replace('\n', '. ') for d in terms])
 
 
 
 
245
 
246
+ if len(terms) > 0:
247
+ messages = [
248
+ SystemMessage(content=f"""You are a helpful assistant that writes about products for ecommerce websites. Make sure to write in English language."""),
249
+ HumanMessage(content=f"""Rewrite the following text using the terms in the glossary.
250
  Preserve the original text as much as possible.
251
  Replace the terms in original text that match the definition with the corresponding terms in the glossary.
252
+ Output only the rewritten text in markdown format.
253
 
254
  Terms, Definitions
255
+ {glossary_str}
256
+
257
+ Text to rewrite:
258
+ {text}
259
+ """),]
260
+
261
+ print(f"HumanMessage={messages[1].content}")
262
+ response = chat.invoke(messages, temperature=0.0)
263
+ print(f"Response=\n{response.content}")
264
+ return response.content
265
+ except Exception as e:
266
+ print(e.__class__, e)
267
+ traceback.print_exc()
268
+ terms = []
269
 
270
+ return ""
271
 
272
 
273
  def generate(*data):
 
315
  intended_use = "Intended use: " + ", ".join(image_features["intended_use"])
316
  print(f"Detected features: {detected_features}, Intended use: {intended_use}, Alt text: {alt_texts}")
317
 
318
+ # if glossary:
319
+ # print("Getting terms")
320
+ # terms = glossary.invoke(input=feature + detected_features)
321
+ # for term in terms:
322
+ # print(term)
323
 
324
  batch = []
325
  for i in range(visible + 1):
 
526
 
527
  descriptions.append(bestd)
528
 
529
+ print("\n\nRewriting with glossary")
530
+ rewrite = glossary_rewrite(chat=chat, glossary=glossary, text=descriptions[0])
531
+ if rewrite != "":
532
+ descriptions[0] = "Original:\n\n" + descriptions[0] + "\n\nRewritten:\n\n" + rewrite
533
+ print("\n\nDone rewriting with glossary\n\n")
534
 
535
  md_content = "\n\n---\n\n".join(descriptions)
536
 
fashion_glossary.csv CHANGED
@@ -283,3 +283,4 @@ Watteau back,"The back of a garment in which box pleats are placed at the center
283
  weave types,"Fabrics made from yarns that are woven on a loom can be constructed in various ways. Lengthwise or warp yarns are placed on the loom first, and crosswise or weft (also called filling) yarns are interlaced with the warps. There are three basic weaves: plain weave, twill weave, and satin weave. In the plain weave, yarns in the first row of the weft cross over one warp yarn, under one, over one, etc., across the width of the fabric. In the second row, the weft passed under the first warp, over the second, under the third, etc. The third row follows the same pattern as the first. This is the simplest weave. By varying the kinds of yarns used, color of yarns, and size of yarns, many different fabrics can be produced. If several rows of weft yarns cross over several rows of warp yarns at a time, a variation of the plain weave called the basket weave can be made. In the twill weave, weft yarns interlace with warp yarns in a specified regular pattern. The resulting fabric has a diagonal line (called a wale) on the surface. For the satin weave, weft yarns float over then interlace with warp yarns in patterns that do not produce a diagonal line. By using loosely twisted filament fibers with high luster, the characteristic sheen of satin fabrics is produced. More complicated patterns produced on special looms, such as the dobby or jacquard, utilize combinations of the basic weaves to achieve very decorative fabrics."
284
  weft and warp knits,"Knitting is the formation of fabrics from yarns by creating interconnected loops. Knits may be made from one long, continuous yarn (as in hand knitting) or from sets of yarns. In weft knits, yarns run and interlock across the fabric. In warp knits, the yarns run or interlock in the lengthwise direction. Hand knitting is a form of weft knitting, but weft knits are also made on machines. Weft knits stretch more than warp knits. If stitches break, a run or ladder forms in the lengthwise direction. The most common types of weft knits are jersey (also called single or plain knits) in which all stitches are pulled to the same side of the fabric, so that one side of the fabric is smooth and the other side has loops. Knitted velour, terrycloth, fleece, imitation furs, plush, and other knitted pile fabrics are weft knits, as are doubleknits. Warp knits are more stable and resist runs. The most common fabrics made by warp knitting are tricots, which are made in varying weights, and raschel knits which can be made in complicated patterns that simulate lace or crochet."
285
  yarn types,"Yarns are made by twisting or otherwise binding fibers together and are used to construct fabrics. The fibers that are made into yarns can be long, continuous strands, called filaments, which can be twisted together loosely or more tightly. Filament yarns are made from either silk or manufactured fibers. Short, staple fibers (cotton, wool, linen, or manufactured fibers or silk cut into short lengths) must be twisted so that they will hold together to form a yarn. Yarns may be classified according to the number of parts. A single yarn is just one yarn, a ply yarn consists of two or more single yarns twisted together, and a cord yarn is made of two or more ply yarns twisted together. Yarns with a regular surface and diameter are called simple yarns. Those made to create decorative effects are known as novelty or fancy yarns. A novelty yarn can be a single yarn, such as a slub yarn in which some parts of the yarn are twisted tightly and other parts more loosely, or a ply yarn such as a bouclé yarn in which a decorative yarn makes irregular, decorative loops around a base yarn. By varying the structure of yarns, an enormous number of different types of yarns and decorative effects can be created."
 
 
283
  weave types,"Fabrics made from yarns that are woven on a loom can be constructed in various ways. Lengthwise or warp yarns are placed on the loom first, and crosswise or weft (also called filling) yarns are interlaced with the warps. There are three basic weaves: plain weave, twill weave, and satin weave. In the plain weave, yarns in the first row of the weft cross over one warp yarn, under one, over one, etc., across the width of the fabric. In the second row, the weft passed under the first warp, over the second, under the third, etc. The third row follows the same pattern as the first. This is the simplest weave. By varying the kinds of yarns used, color of yarns, and size of yarns, many different fabrics can be produced. If several rows of weft yarns cross over several rows of warp yarns at a time, a variation of the plain weave called the basket weave can be made. In the twill weave, weft yarns interlace with warp yarns in a specified regular pattern. The resulting fabric has a diagonal line (called a wale) on the surface. For the satin weave, weft yarns float over then interlace with warp yarns in patterns that do not produce a diagonal line. By using loosely twisted filament fibers with high luster, the characteristic sheen of satin fabrics is produced. More complicated patterns produced on special looms, such as the dobby or jacquard, utilize combinations of the basic weaves to achieve very decorative fabrics."
284
  weft and warp knits,"Knitting is the formation of fabrics from yarns by creating interconnected loops. Knits may be made from one long, continuous yarn (as in hand knitting) or from sets of yarns. In weft knits, yarns run and interlock across the fabric. In warp knits, the yarns run or interlock in the lengthwise direction. Hand knitting is a form of weft knitting, but weft knits are also made on machines. Weft knits stretch more than warp knits. If stitches break, a run or ladder forms in the lengthwise direction. The most common types of weft knits are jersey (also called single or plain knits) in which all stitches are pulled to the same side of the fabric, so that one side of the fabric is smooth and the other side has loops. Knitted velour, terrycloth, fleece, imitation furs, plush, and other knitted pile fabrics are weft knits, as are doubleknits. Warp knits are more stable and resist runs. The most common fabrics made by warp knitting are tricots, which are made in varying weights, and raschel knits which can be made in complicated patterns that simulate lace or crochet."
285
  yarn types,"Yarns are made by twisting or otherwise binding fibers together and are used to construct fabrics. The fibers that are made into yarns can be long, continuous strands, called filaments, which can be twisted together loosely or more tightly. Filament yarns are made from either silk or manufactured fibers. Short, staple fibers (cotton, wool, linen, or manufactured fibers or silk cut into short lengths) must be twisted so that they will hold together to form a yarn. Yarns may be classified according to the number of parts. A single yarn is just one yarn, a ply yarn consists of two or more single yarns twisted together, and a cord yarn is made of two or more ply yarns twisted together. Yarns with a regular surface and diameter are called simple yarns. Those made to create decorative effects are known as novelty or fancy yarns. A novelty yarn can be a single yarn, such as a slub yarn in which some parts of the yarn are twisted tightly and other parts more loosely, or a ply yarn such as a bouclé yarn in which a decorative yarn makes irregular, decorative loops around a base yarn. By varying the structure of yarns, an enormous number of different types of yarns and decorative effects can be created."
286
+ footware,"shoes, boots, sandals, and other items worn on the feet"