ahkamboh commited on
Commit
fdd10b2
·
verified ·
1 Parent(s): cc7a5f6

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +151 -0
app.py ADDED
@@ -0,0 +1,151 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from gradio_client import Client, handle_file
3
+ import re
4
+ import time
5
+ import os
6
+ from dotenv import load_dotenv
7
+
8
+ # Load environment variables
9
+ load_dotenv()
10
+
11
+ # Get Hugging Face token from environment variable
12
+ hf_token = os.getenv("HUGGING_FACE_HUB_TOKEN")
13
+
14
+ # Initialize client with auth
15
+ client = Client(
16
+ "levihsu/OOTDiffusion",
17
+ hf_token=hf_token
18
+ )
19
+
20
+
21
+ def generate_outfit(model_image, garment_image, n_samples=1, n_steps=20, image_scale=2, seed=-1):
22
+ if model_image is None or garment_image is None:
23
+ return None, "Please upload both model and garment images"
24
+
25
+ max_retries = 3
26
+ for attempt in range(max_retries):
27
+ try:
28
+ # Use the client to predict
29
+ result = client.predict(
30
+ vton_img=handle_file(model_image),
31
+ garm_img=handle_file(garment_image),
32
+ n_samples=n_samples,
33
+ n_steps=n_steps,
34
+ image_scale=image_scale,
35
+ seed=seed,
36
+ api_name="/process_hd"
37
+ )
38
+
39
+ # If result is a list, get the first item
40
+ if isinstance(result, list):
41
+ result = result[0]
42
+
43
+ # If result is a dictionary, try to get the image path
44
+ if isinstance(result, dict):
45
+ if 'image' in result:
46
+ return result['image'], None
47
+ else:
48
+ return None, "API returned unexpected format"
49
+
50
+ return result, None
51
+
52
+ except Exception as e:
53
+ error_msg = str(e)
54
+ if "exceeded your GPU quota" in error_msg:
55
+ wait_time_match = re.search(r'retry in (\d+:\d+:\d+)', error_msg)
56
+ wait_time = wait_time_match.group(1) if wait_time_match else "60:00" # Default to 1 hour
57
+ wait_seconds = sum(int(x) * 60 ** i for i, x in enumerate(reversed(wait_time.split(':')))) # Convert wait time to seconds
58
+ if attempt < max_retries - 1:
59
+ time.sleep(wait_seconds) # Wait before retrying
60
+ return None, f"GPU quota exceeded. Please wait {wait_time} before trying again."
61
+ else:
62
+ return None, f"Error: {str(e)}"
63
+
64
+ # Create Gradio interface
65
+ with gr.Blocks() as demo:
66
+ gr.Markdown("""
67
+ ## Outfit Diffusion - Try On Virtual Outfits
68
+
69
+ ⚠️ **Note**: This demo uses free GPU quota which is limited. To avoid errors:
70
+ - Use lower values for Steps (10-15) and Scale (1-2)
71
+ - Wait between attempts if you get a quota error
72
+ - Sign up for a Hugging Face account for more quota
73
+
74
+
75
+ """)
76
+
77
+ with gr.Row():
78
+ with gr.Column():
79
+ model_image = gr.Image(
80
+ label="Upload Model Image (person wearing clothes)",
81
+ type="filepath",
82
+ height=300
83
+
84
+ )
85
+ model_examples = [
86
+ "https://levihsu-ootdiffusion.hf.space/file=/tmp/gradio/ba5ba7978e7302e8ab5eb733cc7221394c4e6faf/model_5.png",
87
+ "https://levihsu-ootdiffusion.hf.space/file=/tmp/gradio/40dade4a04a827c0fdf63c6c70b42ef26480f391/01861_00.jpg",
88
+ "https://levihsu-ootdiffusion.hf.space/file=/tmp/gradio/3c4639c5fab3cdcd3239609dca5afee7b0677286/model_6.png",
89
+ "https://levihsu-ootdiffusion.hf.space/file=/tmp/gradio/0089171df270f4532eec3d80a8f36cc8218c6840/01008_00.jpg"
90
+ ]
91
+ gr.Examples(examples=model_examples, inputs=model_image)
92
+
93
+ garment_image = gr.Image(
94
+ label="Upload Garment Image (clothing item)",
95
+ type="filepath",
96
+ height=300
97
+ )
98
+ garment_examples = [
99
+ "https://levihsu-ootdiffusion.hf.space/file=/tmp/gradio/180d4e2a1139071a8685a5edee7ab24bcf1639f5/03244_00.jpg",
100
+ "https://levihsu-ootdiffusion.hf.space/file=/tmp/gradio/584dda2c5ee1d8271a6cd06225c07db89c79ca03/04825_00.jpg",
101
+ "https://levihsu-ootdiffusion.hf.space/file=/tmp/gradio/a51938ec99f13e548d365a9ca6d794b6fe7462af/049949_1.jpg",
102
+ "https://levihsu-ootdiffusion.hf.space/file=/tmp/gradio/2d64241101189251ce415df84dc9205cda9a36ca/03032_00.jpg",
103
+ "https://levihsu-ootdiffusion.hf.space/file=/tmp/gradio/44aee6b576cae51eeb979311306375b56b7e0d8b/02305_00.jpg",
104
+ "https://levihsu-ootdiffusion.hf.space/file=/tmp/gradio/578dfa869dedb649e91eccbe566fc76435bb6bbe/049920_1.jpg"
105
+ ]
106
+ gr.Examples(examples=garment_examples, inputs=garment_image)
107
+
108
+
109
+ with gr.Column():
110
+ output_image = gr.Image(label="Generated Output")
111
+ error_text = gr.Markdown() # Add error display
112
+
113
+ with gr.Row():
114
+ with gr.Column():
115
+ n_samples = gr.Slider(
116
+ label="Number of Samples",
117
+ minimum=1,
118
+ maximum=5,
119
+ step=1,
120
+ value=1
121
+ )
122
+ n_steps = gr.Slider(
123
+ label="Steps (lower = faster, try 10-15)",
124
+ minimum=1,
125
+ maximum=50,
126
+ step=1,
127
+ value=10 # Reduced default
128
+ )
129
+ image_scale = gr.Slider(
130
+ label="Scale (lower = faster, try 1-2)",
131
+ minimum=1,
132
+ maximum=5,
133
+ step=1,
134
+ value=1 # Reduced default
135
+ )
136
+ seed = gr.Number(
137
+ label="Random Seed (-1 for random)",
138
+ value=-1
139
+ )
140
+
141
+ generate_button = gr.Button("Generate Outfit")
142
+
143
+ # Set up the action for the button
144
+ generate_button.click(
145
+ fn=generate_outfit,
146
+ inputs=[model_image, garment_image, n_samples, n_steps, image_scale, seed],
147
+ outputs=[output_image, error_text]
148
+ )
149
+
150
+ # Launch the app
151
+ demo.launch()