|
import gradio as gr |
|
from openai import OpenAI |
|
from PIL import Image |
|
import numpy as np |
|
import os |
|
from datetime import datetime |
|
|
|
|
|
client = OpenAI() |
|
|
|
|
|
def array_to_image_path(image_array): |
|
if image_array is None: |
|
raise ValueError("No image provided. Please upload an image before submitting.") |
|
|
|
img = Image.fromarray(np.uint8(image_array)) |
|
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S") |
|
filename = f"image_{timestamp}.png" |
|
img.save(filename) |
|
full_path = os.path.abspath(filename) |
|
return full_path |
|
|
|
|
|
|
|
def generate_product_description(image, text_input=None): |
|
|
|
image_path = array_to_image_path(image) |
|
|
|
|
|
|
|
image_url = "https://example.com/" + os.path.basename(image_path) |
|
|
|
|
|
completion = client.chat.completions.create( |
|
model="gpt-4o", |
|
messages=[ |
|
{ |
|
"role": "user", |
|
"content": [ |
|
{"type": "text", "text": text_input or "What's in this image?"}, |
|
{ |
|
"type": "image_url", |
|
"image_url": {"url": image_url}, |
|
}, |
|
], |
|
} |
|
], |
|
) |
|
|
|
|
|
return completion.choices[0].message |
|
|
|
|
|
css = """ |
|
#output { |
|
height: 500px; |
|
overflow: auto; |
|
border: 1px solid #ccc; |
|
} |
|
""" |
|
|
|
with gr.Blocks(css=css) as demo: |
|
gr.Markdown("WordLift Product Description Generation - [GPT-4o-mini Demo]") |
|
with gr.Tab(label="WordLift Product Description Generation"): |
|
with gr.Row(): |
|
with gr.Column(): |
|
input_img = gr.Image(label="Input Picture") |
|
text_input = gr.Textbox(label="Additional Instructions (Optional)") |
|
submit_btn = gr.Button(value="Submit") |
|
with gr.Column(): |
|
output_text = gr.Textbox(label="Output Text") |
|
|
|
submit_btn.click( |
|
generate_product_description, [input_img, text_input], [output_text] |
|
) |
|
|
|
demo.queue(api_open=False) |
|
demo.launch(debug=True) |
|
|