Spaces:
Sleeping
Sleeping
| 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) |