Spaces:
Running
Running
Create desc.py
Browse files
desc.py
ADDED
@@ -0,0 +1,41 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import openai
|
2 |
+
import base64
|
3 |
+
|
4 |
+
def describe_image_with_gpt4o(image_path, api_key):
|
5 |
+
"""
|
6 |
+
Given an image file, sends it to GPT-4o Vision API and gets a detailed description.
|
7 |
+
Returns the description as a string.
|
8 |
+
"""
|
9 |
+
openai.api_key = api_key
|
10 |
+
|
11 |
+
# Read and encode image
|
12 |
+
with open(image_path, "rb") as img_file:
|
13 |
+
img_base64 = base64.b64encode(img_file.read()).decode("utf-8")
|
14 |
+
img_data_url = f"data:image/png;base64,{img_base64}"
|
15 |
+
|
16 |
+
# Compose the prompt
|
17 |
+
prompt = (
|
18 |
+
"Describe this face with as much detail as possible, "
|
19 |
+
"focusing on facial features, hairstyle, expression, accessories, and notable traits. "
|
20 |
+
"Be specific, provide detail on Clothing, style and background"
|
21 |
+
"Be specific, as if explaining to an artist who will draw a sticker of this person."
|
22 |
+
)
|
23 |
+
|
24 |
+
# Call GPT-4o Vision API
|
25 |
+
response = openai.chat.completions.create(
|
26 |
+
model="gpt-4o",
|
27 |
+
messages=[
|
28 |
+
{
|
29 |
+
"role": "user",
|
30 |
+
"content": [
|
31 |
+
{"type": "text", "text": prompt},
|
32 |
+
{"type": "image_url", "image_url": img_data_url},
|
33 |
+
],
|
34 |
+
}
|
35 |
+
],
|
36 |
+
max_tokens=256,
|
37 |
+
)
|
38 |
+
|
39 |
+
# Extract the description
|
40 |
+
description = response.choices[0].message.content.strip()
|
41 |
+
return description
|