Spaces:
Paused
Paused
fix get image features
Browse files
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 |
-
# "
|
| 486 |
-
|
| 487 |
-
|
| 488 |
-
|
| 489 |
-
|
| 490 |
-
|
| 491 |
-
|
| 492 |
-
|
| 493 |
-
|
| 494 |
-
|
| 495 |
-
|
| 496 |
-
|
| 497 |
-
|
| 498 |
-
|
| 499 |
-
|
| 500 |
-
|
| 501 |
-
|
| 502 |
-
|
| 503 |
-
|
| 504 |
-
|
| 505 |
-
|
| 506 |
-
|
| 507 |
-
|
| 508 |
-
|
| 509 |
-
|
| 510 |
-
"
|
| 511 |
-
|
| 512 |
-
|
| 513 |
-
|
| 514 |
-
|
| 515 |
-
|
| 516 |
-
|
| 517 |
-
|
| 518 |
-
|
| 519 |
-
|
| 520 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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)
|