Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,31 +1,35 @@
|
|
1 |
import openai
|
2 |
import gradio as gr
|
3 |
import os
|
|
|
4 |
|
5 |
# Set your OpenAI API Key
|
6 |
openai.api_key = "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" # Replace with your API key
|
7 |
|
8 |
def generate_caption(image):
|
9 |
"""Generate a caption for the uploaded image using OpenAI's GPT-4 Vision API."""
|
|
|
|
|
10 |
with open(image, "rb") as img_file:
|
11 |
-
|
12 |
-
model="gpt-4-turbo",
|
13 |
-
messages=[
|
14 |
-
{
|
15 |
-
"role": "system",
|
16 |
-
"content": "You are an AI assistant that describes images accurately."
|
17 |
-
},
|
18 |
-
{
|
19 |
-
"role": "user",
|
20 |
-
"content": [
|
21 |
-
{"type": "text", "text": "Describe the contents of this image in detail."},
|
22 |
-
{"type": "image_url", "image_url": {"url": "data:image/jpeg;base64," + img_file.read().decode()}},
|
23 |
-
],
|
24 |
-
},
|
25 |
-
],
|
26 |
-
max_tokens=100
|
27 |
-
)
|
28 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
29 |
caption = response.choices[0].message.content
|
30 |
return caption
|
31 |
|
|
|
1 |
import openai
|
2 |
import gradio as gr
|
3 |
import os
|
4 |
+
import base64
|
5 |
|
6 |
# Set your OpenAI API Key
|
7 |
openai.api_key = "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" # Replace with your API key
|
8 |
|
9 |
def generate_caption(image):
|
10 |
"""Generate a caption for the uploaded image using OpenAI's GPT-4 Vision API."""
|
11 |
+
|
12 |
+
# Convert image to Base64
|
13 |
with open(image, "rb") as img_file:
|
14 |
+
img_base64 = base64.b64encode(img_file.read()).decode("utf-8")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
15 |
|
16 |
+
# OpenAI API call
|
17 |
+
response = openai.chat.completions.create(
|
18 |
+
model="gpt-4-turbo",
|
19 |
+
messages=[
|
20 |
+
{"role": "system", "content": "You are an AI that describes images accurately."},
|
21 |
+
{
|
22 |
+
"role": "user",
|
23 |
+
"content": [
|
24 |
+
{"type": "text", "text": "Describe this image in detail."},
|
25 |
+
{"type": "image_url", "image_url": {"url": f"data:image/jpeg;base64,{img_base64}"}},
|
26 |
+
],
|
27 |
+
},
|
28 |
+
],
|
29 |
+
max_tokens=100
|
30 |
+
)
|
31 |
+
|
32 |
+
# Extract and return caption
|
33 |
caption = response.choices[0].message.content
|
34 |
return caption
|
35 |
|