Spaces:
Sleeping
Sleeping
Greg commited on
Commit 路
c26573b
1
Parent(s): ebbc7c4
Add application file
Browse files
app.py
ADDED
|
@@ -0,0 +1,94 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import base64
|
| 2 |
+
import io
|
| 3 |
+
|
| 4 |
+
import gradio as gr
|
| 5 |
+
from PIL import Image
|
| 6 |
+
from openai import OpenAI
|
| 7 |
+
|
| 8 |
+
# Initialize the OpenAI client
|
| 9 |
+
client = OpenAI(api_key='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')
|
| 10 |
+
|
| 11 |
+
|
| 12 |
+
def analyze_fabric_and_generate_design(image, clothing_item):
|
| 13 |
+
# Convert numpy array to PIL Image
|
| 14 |
+
pil_image = Image.fromarray(image)
|
| 15 |
+
|
| 16 |
+
# Save the image to a BytesIO object
|
| 17 |
+
buffer = io.BytesIO()
|
| 18 |
+
pil_image.save(buffer, format='PNG')
|
| 19 |
+
|
| 20 |
+
# Get the content of the BytesIO object
|
| 21 |
+
byte_data = buffer.getvalue()
|
| 22 |
+
|
| 23 |
+
# Encode to base64
|
| 24 |
+
base64_str = base64.b64encode(byte_data).decode('utf-8')
|
| 25 |
+
|
| 26 |
+
# Analyze the fabric using GPT-4 with vision
|
| 27 |
+
fabric_analysis = client.chat.completions.create(
|
| 28 |
+
model="gpt-4-vision-preview",
|
| 29 |
+
messages=[
|
| 30 |
+
{
|
| 31 |
+
"role": "system",
|
| 32 |
+
"content": [
|
| 33 |
+
{"type": "text",
|
| 34 |
+
"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:
|
| 35 |
+
- **Fabric Type**: Specify the type of fabric (e.g., cotton, silk, wool).
|
| 36 |
+
- **Texture**: Describe the texture (e.g., smooth, rough, soft).
|
| 37 |
+
- **Color**: Mention the primary and any secondary colors visible.
|
| 38 |
+
- **Pattern**: Note any patterns (e.g., striped, floral, plaid).
|
| 39 |
+
- **Distinctive Features**: Highlight any unique features or elements that stand out.
|
| 40 |
+
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.
|
| 41 |
+
"""}
|
| 42 |
+
]
|
| 43 |
+
},
|
| 44 |
+
{
|
| 45 |
+
"role": "user",
|
| 46 |
+
"content": [
|
| 47 |
+
{"type": "image_url", "image_url": "data:image/png;base64," + base64_str}
|
| 48 |
+
]
|
| 49 |
+
}
|
| 50 |
+
],
|
| 51 |
+
)
|
| 52 |
+
# Extract the fabric description from the response
|
| 53 |
+
fabric_description = fabric_analysis.choices[0].message.content
|
| 54 |
+
print(fabric_description)
|
| 55 |
+
# Construct the prompt for DALL路E 3 using the fabric description and selected clothing item
|
| 56 |
+
dalle_prompt = f"""Create a realistic, 3D photographic of a detailed image of a {clothing_item}, crafted using a material described as:
|
| 57 |
+
{fabric_description}. The image should present the {clothing_item} against a gray background and emphasize the design and fabric details.
|
| 58 |
+
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.
|
| 59 |
+
Strive for a professional, catalog-quality representation that effectively showcases the {clothing_item} in its entirety."""
|
| 60 |
+
|
| 61 |
+
print(dalle_prompt)
|
| 62 |
+
|
| 63 |
+
# Generate the design image using DALL路E 3
|
| 64 |
+
design_image_response = client.images.generate(
|
| 65 |
+
model="dall-e-3",
|
| 66 |
+
prompt=dalle_prompt,
|
| 67 |
+
size="1024x1024",
|
| 68 |
+
quality="hd",
|
| 69 |
+
n=1
|
| 70 |
+
)
|
| 71 |
+
|
| 72 |
+
# Extract the URL of the generated design image
|
| 73 |
+
design_image_url = design_image_response.data[0].url
|
| 74 |
+
|
| 75 |
+
return design_image_url
|
| 76 |
+
|
| 77 |
+
|
| 78 |
+
# Define clothing options for the dropdown
|
| 79 |
+
clothing_options = ["jacket", "pants", "t-shirt", "dress", "skirt", "blouse"]
|
| 80 |
+
|
| 81 |
+
# Create the Gradio interface
|
| 82 |
+
iface = gr.Interface(
|
| 83 |
+
fn=analyze_fabric_and_generate_design,
|
| 84 |
+
inputs=[
|
| 85 |
+
gr.Image(type="numpy", label="Upload Fabric Image"),
|
| 86 |
+
gr.Dropdown(choices=clothing_options, label="Select Clothing Item")
|
| 87 |
+
],
|
| 88 |
+
outputs=gr.Image(label="Generated Fashion Design"),
|
| 89 |
+
title="Fashion Design Generator",
|
| 90 |
+
description="Upload an image of fabric and select a clothing item to generate a fashion design."
|
| 91 |
+
)
|
| 92 |
+
|
| 93 |
+
if __name__ == "__main__":
|
| 94 |
+
iface.launch(share=True)
|