Update app.py
Browse files
app.py
CHANGED
|
@@ -1,38 +1,42 @@
|
|
| 1 |
-
# ClothCast - AI Outfit Forecaster (Hackathon MVP)
|
| 2 |
import gradio as gr
|
| 3 |
import torch
|
| 4 |
from diffusers import StableDiffusionPipeline
|
| 5 |
|
| 6 |
-
# Load Stable Diffusion XL
|
| 7 |
model_id = "stabilityai/stable-diffusion-xl-base-1.0"
|
| 8 |
pipe = StableDiffusionPipeline.from_pretrained(
|
| 9 |
-
model_id,
|
| 10 |
-
torch_dtype=torch.float16
|
| 11 |
)
|
| 12 |
pipe = pipe.to("cuda" if torch.cuda.is_available() else "cpu")
|
| 13 |
|
| 14 |
-
# Function to generate outfit image
|
| 15 |
def generate_outfit(weather, activity, style):
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 34 |
|
| 35 |
-
#
|
| 36 |
custom_css = """
|
| 37 |
body {
|
| 38 |
background: linear-gradient(135deg, #232526, #414345);
|
|
@@ -63,37 +67,21 @@ h1, h2, h3 {
|
|
| 63 |
}
|
| 64 |
"""
|
| 65 |
|
| 66 |
-
# Gradio UI
|
| 67 |
with gr.Blocks(css=custom_css) as app:
|
| 68 |
gr.Markdown("<h1 style='text-align:center;'>π ClothCast - AI Outfit Forecaster</h1>")
|
| 69 |
gr.Markdown("<p style='text-align:center;'>Select your weather, activity, and style to generate an AI outfit!</p>")
|
| 70 |
|
| 71 |
with gr.Row():
|
| 72 |
with gr.Column(scale=1):
|
| 73 |
-
weather = gr.Dropdown(
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
)
|
| 77 |
-
activity = gr.Dropdown(
|
| 78 |
-
["Casual", "Work", "Party", "Sporty"],
|
| 79 |
-
label="π Activity", value="Casual"
|
| 80 |
-
)
|
| 81 |
-
style = gr.Dropdown(
|
| 82 |
-
["Casual", "Sporty", "Formal"],
|
| 83 |
-
label="π¨ Style", value="Casual"
|
| 84 |
-
)
|
| 85 |
generate_btn = gr.Button("Generate Outfit π", variant="primary")
|
| 86 |
-
|
| 87 |
with gr.Column(scale=1):
|
| 88 |
outfit_image = gr.Image(label="πΌ Generated Outfit")
|
| 89 |
outfit_text = gr.Textbox(label="π‘ Outfit Suggestion", lines=8)
|
| 90 |
|
| 91 |
-
|
| 92 |
-
generate_btn.click(
|
| 93 |
-
generate_outfit,
|
| 94 |
-
inputs=[weather, activity, style],
|
| 95 |
-
outputs=[outfit_image, outfit_text]
|
| 96 |
-
)
|
| 97 |
|
| 98 |
-
# Launch the app
|
| 99 |
app.launch(share=True)
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import torch
|
| 3 |
from diffusers import StableDiffusionPipeline
|
| 4 |
|
| 5 |
+
# Load the Stable Diffusion XL model
|
| 6 |
model_id = "stabilityai/stable-diffusion-xl-base-1.0"
|
| 7 |
pipe = StableDiffusionPipeline.from_pretrained(
|
| 8 |
+
model_id,
|
| 9 |
+
torch_dtype=torch.float16,
|
| 10 |
)
|
| 11 |
pipe = pipe.to("cuda" if torch.cuda.is_available() else "cpu")
|
| 12 |
|
| 13 |
+
# Function to generate outfit image + suggestion with error handling
|
| 14 |
def generate_outfit(weather, activity, style):
|
| 15 |
+
try:
|
| 16 |
+
# Build prompt
|
| 17 |
+
prompt = (
|
| 18 |
+
f"A realistic full-body outfit for {weather} weather, "
|
| 19 |
+
f"{activity} activity, {style} style, front view, 30-degree angle, 4k detail"
|
| 20 |
+
)
|
| 21 |
+
|
| 22 |
+
# Generate image
|
| 23 |
+
result = pipe(prompt, guidance_scale=7.5)
|
| 24 |
+
image = result.images[0] # Ensure we take the first PIL image
|
| 25 |
+
|
| 26 |
+
# Simple outfit suggestion text
|
| 27 |
+
suggestion_text = (
|
| 28 |
+
f"π‘ Suggested outfit for {weather} weather, {activity} activity, {style} style:\n"
|
| 29 |
+
"- Top: light breathable shirt or jacket depending on weather\n"
|
| 30 |
+
"- Bottom: comfortable pants or shorts\n"
|
| 31 |
+
"- Shoes: suitable for activity\n"
|
| 32 |
+
"- Accessories: hat, sunglasses, or scarf depending on weather"
|
| 33 |
+
)
|
| 34 |
+
return image, suggestion_text
|
| 35 |
+
except Exception as e:
|
| 36 |
+
# If something fails, return a placeholder and error text
|
| 37 |
+
return None, f"β Error generating outfit: {str(e)}"
|
| 38 |
|
| 39 |
+
# Gradio UI
|
| 40 |
custom_css = """
|
| 41 |
body {
|
| 42 |
background: linear-gradient(135deg, #232526, #414345);
|
|
|
|
| 67 |
}
|
| 68 |
"""
|
| 69 |
|
|
|
|
| 70 |
with gr.Blocks(css=custom_css) as app:
|
| 71 |
gr.Markdown("<h1 style='text-align:center;'>π ClothCast - AI Outfit Forecaster</h1>")
|
| 72 |
gr.Markdown("<p style='text-align:center;'>Select your weather, activity, and style to generate an AI outfit!</p>")
|
| 73 |
|
| 74 |
with gr.Row():
|
| 75 |
with gr.Column(scale=1):
|
| 76 |
+
weather = gr.Dropdown(["Hot", "Cold", "Rainy", "Snowy", "Mild", "Humid"], label="π€ Weather", value="Hot")
|
| 77 |
+
activity = gr.Dropdown(["Casual", "Work", "Party", "Sporty"], label="π Activity", value="Casual")
|
| 78 |
+
style = gr.Dropdown(["Casual", "Sporty", "Formal"], label="π¨ Style", value="Casual")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 79 |
generate_btn = gr.Button("Generate Outfit π", variant="primary")
|
|
|
|
| 80 |
with gr.Column(scale=1):
|
| 81 |
outfit_image = gr.Image(label="πΌ Generated Outfit")
|
| 82 |
outfit_text = gr.Textbox(label="π‘ Outfit Suggestion", lines=8)
|
| 83 |
|
| 84 |
+
generate_btn.click(generate_outfit, inputs=[weather, activity, style], outputs=[outfit_image, outfit_text])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 85 |
|
|
|
|
| 86 |
app.launch(share=True)
|
| 87 |
+
|