File size: 4,046 Bytes
c26573b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
import base64
import io

import gradio as gr
from PIL import Image
from openai import OpenAI

# Initialize the OpenAI client
client = OpenAI(api_key='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')

def analyze_fabric_and_generate_design(image, clothing_item):
    # Convert numpy array to PIL Image
    pil_image = Image.fromarray(image)

    # Save the image to a BytesIO object
    buffer = io.BytesIO()
    pil_image.save(buffer, format='PNG')

    # Get the content of the BytesIO object
    byte_data = buffer.getvalue()

    # Encode to base64
    base64_str = base64.b64encode(byte_data).decode('utf-8')

    # Analyze the fabric using GPT-4 with vision
    fabric_analysis = client.chat.completions.create(
        model="gpt-4-vision-preview",
        messages=[
            {
                "role": "system",
                "content": [
                    {"type": "text",
                     "text": """As an AI fashion expert, examine the fabric shown in the image carefully. Your analysis should clearly categorize the fabric type, texture, color, and pattern. Provide a concise description, focusing on these aspects:
                            - **Fabric Type**: Specify the type of fabric (e.g., cotton, silk, wool).
                            - **Texture**: Describe the texture (e.g., smooth, rough, soft).
                            - **Color**: Mention the primary and any secondary colors visible.
                            - **Pattern**: Note any patterns (e.g., striped, floral, plaid).
                            - **Distinctive Features**: Highlight any unique features or elements that stand out.
                            This structured description will serve as a direct input for creating a realistic and professional image of a fashion item using the described fabric. Ensure the description is factual, direct, and free from embellishments to support precise image generation.
                            """}
                ]
            },
            {
                "role": "user",
                "content": [
                    {"type": "image_url", "image_url": "data:image/png;base64," + base64_str}
                ]
            }
        ],
    )
    # Extract the fabric description from the response
    fabric_description = fabric_analysis.choices[0].message.content
    print(fabric_description)
    # Construct the prompt for DALL·E 3 using the fabric description and selected clothing item
    dalle_prompt = f"""Create a realistic, 3D photographic of a detailed image of a {clothing_item}, crafted using a material described as: 
    {fabric_description}. The image should present the {clothing_item} against a gray background and emphasize the design and fabric details. 
    Ensure high fidelity in capturing the texture, color, and pattern of the fabric, aiming for a depiction that mirrors real life as closely as possible. 
    Strive for a professional, catalog-quality representation that effectively showcases the {clothing_item} in its entirety."""

    print(dalle_prompt)

    # Generate the design image using DALL·E 3
    design_image_response = client.images.generate(
        model="dall-e-3",
        prompt=dalle_prompt,
        size="1024x1024",
        quality="hd",
        n=1
    )

    # Extract the URL of the generated design image
    design_image_url = design_image_response.data[0].url

    return design_image_url


# Define clothing options for the dropdown
clothing_options = ["jacket", "pants", "t-shirt", "dress", "skirt", "blouse"]

# Create the Gradio interface
iface = gr.Interface(
    fn=analyze_fabric_and_generate_design,
    inputs=[
        gr.Image(type="numpy", label="Upload Fabric Image"),
        gr.Dropdown(choices=clothing_options, label="Select Clothing Item")
    ],
    outputs=gr.Image(label="Generated Fashion Design"),
    title="Fashion Design Generator",
    description="Upload an image of fabric and select a clothing item to generate a fashion design."
)

if __name__ == "__main__":
    iface.launch(share=True)