Update app.py
Browse files
app.py
CHANGED
|
@@ -1,56 +1,63 @@
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
-
from openai import OpenAI
|
| 3 |
-
import os
|
| 4 |
from PIL import Image
|
| 5 |
import numpy as np
|
| 6 |
from datetime import datetime
|
|
|
|
| 7 |
|
| 8 |
-
#
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
def array_to_image_path(image_array):
|
| 13 |
-
if image_array is None:
|
| 14 |
-
raise ValueError("No image provided. Please upload an image before submitting.")
|
| 15 |
|
|
|
|
|
|
|
|
|
|
| 16 |
img = Image.fromarray(np.uint8(image_array))
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
|
| 24 |
# Function to generate product description using OpenAI API
|
| 25 |
def generate_product_description(image, text_input=None):
|
| 26 |
-
#
|
| 27 |
-
|
| 28 |
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
|
| 34 |
-
#
|
| 35 |
-
|
| 36 |
-
model
|
| 37 |
-
messages
|
| 38 |
{
|
| 39 |
"role": "user",
|
| 40 |
"content": [
|
| 41 |
{"type": "text", "text": text_input or "What's in this image?"},
|
| 42 |
{
|
| 43 |
"type": "image_url",
|
| 44 |
-
"image_url": {
|
| 45 |
-
|
| 46 |
-
|
|
|
|
|
|
|
| 47 |
}
|
| 48 |
],
|
| 49 |
-
|
|
|
|
| 50 |
|
| 51 |
-
|
| 52 |
-
|
| 53 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 54 |
|
| 55 |
css = """
|
| 56 |
#output {
|
|
@@ -75,6 +82,6 @@ with gr.Blocks(css=css) as demo:
|
|
| 75 |
generate_product_description, [input_img, text_input], [output_text]
|
| 76 |
)
|
| 77 |
|
| 78 |
-
#
|
| 79 |
demo.queue(api_open=False)
|
| 80 |
-
demo.launch(debug=True
|
|
|
|
| 1 |
+
import base64
|
| 2 |
+
import requests
|
| 3 |
import gradio as gr
|
|
|
|
|
|
|
| 4 |
from PIL import Image
|
| 5 |
import numpy as np
|
| 6 |
from datetime import datetime
|
| 7 |
+
import os
|
| 8 |
|
| 9 |
+
# OpenAI API Key
|
| 10 |
+
api_key = os.getenv("OPENAI_API_KEY")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
|
| 12 |
+
# Function to encode the image
|
| 13 |
+
def encode_image(image_array):
|
| 14 |
+
# Convert numpy array to an image file and encode it in base64
|
| 15 |
img = Image.fromarray(np.uint8(image_array))
|
| 16 |
+
img_buffer = os.path.join("/tmp", f"temp_image_{datetime.now().strftime('%Y%m%d_%H%M%S')}.jpg")
|
| 17 |
+
img.save(img_buffer, format="JPEG")
|
| 18 |
+
|
| 19 |
+
with open(img_buffer, "rb") as image_file:
|
| 20 |
+
return base64.b64encode(image_file.read()).decode("utf-8")
|
|
|
|
| 21 |
|
| 22 |
# Function to generate product description using OpenAI API
|
| 23 |
def generate_product_description(image, text_input=None):
|
| 24 |
+
# Encode the uploaded image
|
| 25 |
+
base64_image = encode_image(image)
|
| 26 |
|
| 27 |
+
headers = {
|
| 28 |
+
"Content-Type": "application/json",
|
| 29 |
+
"Authorization": f"Bearer {api_key}"
|
| 30 |
+
}
|
| 31 |
|
| 32 |
+
# Payload with base64 encoded image as a Data URL
|
| 33 |
+
payload = {
|
| 34 |
+
"model": "gpt-4o-mini",
|
| 35 |
+
"messages": [
|
| 36 |
{
|
| 37 |
"role": "user",
|
| 38 |
"content": [
|
| 39 |
{"type": "text", "text": text_input or "What's in this image?"},
|
| 40 |
{
|
| 41 |
"type": "image_url",
|
| 42 |
+
"image_url": {
|
| 43 |
+
"url": f"data:image/jpeg;base64,{base64_image}"
|
| 44 |
+
}
|
| 45 |
+
}
|
| 46 |
+
]
|
| 47 |
}
|
| 48 |
],
|
| 49 |
+
"max_tokens": 300
|
| 50 |
+
}
|
| 51 |
|
| 52 |
+
response = requests.post("https://api.openai.com/v1/chat/completions", headers=headers, json=payload)
|
| 53 |
+
response_data = response.json()
|
| 54 |
|
| 55 |
+
# Handle errors
|
| 56 |
+
if response.status_code != 200:
|
| 57 |
+
raise ValueError(f"OpenAI API Error: {response_data.get('error', {}).get('message', 'Unknown Error')}")
|
| 58 |
+
|
| 59 |
+
# Extract and return the generated message
|
| 60 |
+
return response_data["choices"][0]["message"]
|
| 61 |
|
| 62 |
css = """
|
| 63 |
#output {
|
|
|
|
| 82 |
generate_product_description, [input_img, text_input], [output_text]
|
| 83 |
)
|
| 84 |
|
| 85 |
+
# Launch Gradio app
|
| 86 |
demo.queue(api_open=False)
|
| 87 |
+
demo.launch(debug=True)
|