Pontonkid commited on
Commit
964392f
Β·
verified Β·
1 Parent(s): 45ba8e9

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +33 -45
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 Base model
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 and suggestion
15
  def generate_outfit(weather, activity, style):
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
- image = pipe(prompt, guidance_scale=7.5).images[0]
24
-
25
- # Simple outfit suggestion text
26
- suggestion_text = (
27
- f"πŸ’‘ Suggested outfit for {weather} weather, {activity} activity, {style} style:\n"
28
- "- Top: light breathable shirt or jacket depending on weather\n"
29
- "- Bottom: comfortable pants or shorts\n"
30
- "- Shoes: suitable for activity\n"
31
- "- Accessories: hat, sunglasses, or scarf depending on weather"
32
- )
33
- return image, suggestion_text
 
 
 
 
 
34
 
35
- # Custom CSS for polished UI
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
- ["Hot", "Cold", "Rainy", "Snowy", "Mild", "Humid"],
75
- label="🌀 Weather", value="Hot"
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
- # Connect button to function
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
+