tunght commited on
Commit
f9cb324
·
1 Parent(s): efc50bb

fix get image features

Browse files
Files changed (1) hide show
  1. app.py +67 -43
app.py CHANGED
@@ -419,6 +419,9 @@ Return the product description only.
419
  </included_phrases>"""
420
 
421
 
 
 
 
422
  import base64
423
  import requests
424
  from PIL import Image
@@ -433,6 +436,7 @@ import io
433
 
434
 
435
  def encode_image(image_path, img_size=1024):
 
436
  with open(image_path, "rb") as image_file:
437
  # b64_img = base64.b64encode(image_file.read()).decode('utf-8')
438
  # b64_img = base64.b64decode(base64.b64encode(response.content))
@@ -475,49 +479,69 @@ def detect_features(image_paths, garment_type, language="English"):
475
  if garment_type == "" or garment_type == "all":
476
  garment_type = "garment"
477
 
478
- headers = {
479
- "Content-Type": "application/json",
480
- "Authorization": f"Bearer {openai.api_key}"
481
- }
482
-
483
- payload = {
484
- "model": "gpt-4o",
485
- # "model": "gpt-4-turbo",
486
- "messages": [
487
- {
488
- "role": "user",
489
- "content": [
490
- {
491
- "type": "text",
492
- "text": f"""Describe the features of the {garment_type} in the photos.
493
- Return the result in in the following JSON format without any preceding or trailing text:
494
-
495
- {{
496
- "features": [list of comma separated features],
497
- "intended_use": [list of comma separated intended uses],
498
- "alt_text": [list of alt text for image 1, alt text for image 2]
499
- }}"""
500
- },
501
- ] + [{
502
- "type": "image_url",
503
- "detail": "low",
504
- "image_url": {
505
- "url": f"data:image/jpeg;base64,{base64_image}"
506
- }
507
- } for base64_image in base64_images]
508
- }
509
- ],
510
- "temperature": 0.0,
511
- "max_tokens": 300
512
- }
513
-
514
- response = requests.post("https://api.openai.com/v1/chat/completions", headers=headers, json=payload)
515
-
516
- print(response)
517
-
518
- response = response.json()
519
- print("image features", response["choices"][0]['message']['content'])
520
- jresponse = get_json(response["choices"][0]['message']['content'])
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
521
  return jresponse, base64_images
522
  except Exception as e:
523
  print(e.__class__, e)
 
419
  </included_phrases>"""
420
 
421
 
422
+ detect_feature_prompt = """Describe the features of the {garment_type} in the photo in less than 150 words.\nReturn the result in in the following JSON format without any preceding or trailing text:\n\n{{\n \"features\": [list of comma separated features],\n \"intended_use\": [list of comma separated intended uses],\n \"alt_text\": alt text for the image,\n \"category\": the category of the garment, for example 'dress', 'shirt', 'pants', 'shoes', etc\n}}"""
423
+
424
+
425
  import base64
426
  import requests
427
  from PIL import Image
 
436
 
437
 
438
  def encode_image(image_path, img_size=1024):
439
+ print("Encoding image", image_path)
440
  with open(image_path, "rb") as image_file:
441
  # b64_img = base64.b64encode(image_file.read()).decode('utf-8')
442
  # b64_img = base64.b64decode(base64.b64encode(response.content))
 
479
  if garment_type == "" or garment_type == "all":
480
  garment_type = "garment"
481
 
482
+ # headers = {
483
+ # "Content-Type": "application/json",
484
+ # "Authorization": f"Bearer {openai.api_key}"
485
+ # }
486
+
487
+ # payload = {
488
+ # "model": "gpt-4o",
489
+ # "messages": [
490
+ # {
491
+ # "role": "user",
492
+ # "content": [
493
+ # {
494
+ # "type": "text",
495
+ # "text": f"""Describe the features of the {garment_type} in the photos.
496
+ # Return the result in in the following JSON format without any preceding or trailing text:
497
+
498
+ # {{
499
+ # "features": [list of comma separated features],
500
+ # "intended_use": [list of comma separated intended uses],
501
+ # "alt_text": [list of alt text for image 1, alt text for image 2]
502
+ # }}"""
503
+ # },
504
+ # ] + [{
505
+ # "type": "image_url",
506
+ # "detail": "low",
507
+ # "image_url": {
508
+ # "url": f"data:image/jpeg;base64,{base64_image}"
509
+ # }
510
+ # } for base64_image in base64_images]
511
+ # }
512
+ # ],
513
+ # "temperature": 0.0,
514
+ # "max_tokens": 300
515
+ # }
516
+
517
+ # response = requests.post("https://api.openai.com/v1/chat/completions", headers=headers, json=payload)
518
+
519
+ # print(response)
520
+
521
+ chat = get_model("gpt-4o", temperature=0.0)
522
+
523
+ messages = [[
524
+ {
525
+ "role": "user",
526
+ "content": [
527
+ {
528
+ "type": "text",
529
+ "text": detect_feature_prompt.format(garment_type=garment_type)
530
+ },
531
+ {
532
+ "type": "image_url",
533
+ "image_url": {
534
+ "url": f"data:image/jpeg;base64,{base64_image}"
535
+ }
536
+ }]
537
+ }
538
+ ] for base64_image in base64_images]
539
+
540
+ response = chat.batch(messages)
541
+
542
+ print("image features", response)
543
+ # print("image features", response["choices"][0]['message']['content'])
544
+ jresponse = get_json(response[0].content)
545
  return jresponse, base64_images
546
  except Exception as e:
547
  print(e.__class__, e)