File size: 2,396 Bytes
c7dae24
a2d1710
c7dae24
 
 
a2d1710
c7dae24
a2d1710
 
c7dae24
 
 
 
 
 
a2d1710
c7dae24
 
 
 
 
 
 
a2d1710
 
 
c7dae24
 
a2d1710
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c7dae24
 
a2d1710
 
c7dae24
 
 
 
 
 
 
 
 
 
 
a2d1710
21ebaa7
c7dae24
 
 
a2d1710
c7dae24
 
 
 
 
a2d1710
c7dae24
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
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)