cyberandy's picture
update app
a2d1710
raw
history blame
2.4 kB
import gradio as gr
from openai import OpenAI
from PIL import Image
import numpy as np
import os
from datetime import datetime
# Initialize OpenAI client
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
# Function to generate product description using OpenAI API
def generate_product_description(image, text_input=None):
# Convert the image to a path (optional, could directly send the image as a URL if available)
image_path = array_to_image_path(image)
# Assuming the image is hosted online, replace the path with the URL.
# In practice, you'd need a public URL to share the image with the API.
image_url = "https://example.com/" + os.path.basename(image_path)
# API request
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},
},
],
}
],
)
# Extract and return the generated message
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)