-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrun_examples.py
More file actions
executable file
·157 lines (135 loc) · 4.99 KB
/
run_examples.py
File metadata and controls
executable file
·157 lines (135 loc) · 4.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
#!/usr/bin/env python3
"""
Example runner for IDM-VTON Virtual Try-On
Run different combinations of person and garment images easily
"""
from gradio_client import Client, handle_file
import os
from pathlib import Path
import time
# Try to load from .env file
env_file = Path(".env")
if env_file.exists():
with open(env_file, 'r') as f:
for line in f:
if line.strip() and not line.startswith('#'):
key, value = line.strip().split('=', 1)
os.environ[key] = value.strip('"')
# Get token from environment
hf_token = os.getenv("HUGGINGFACE_TOKEN")
if not hf_token:
print("Please set your Hugging Face token!")
print("You can either:")
print("1. Create .env file with: HUGGINGFACE_TOKEN=your_token_here")
print("2. Set environment variable: export HUGGINGFACE_TOKEN=your_token_here")
exit(1)
# Initialize client
print(" Connecting to your IDM-VTON space...")
client = Client("blackmamba2408/IDM-VTON", hf_token=hf_token)
print(" Connected successfully!")
# Available examples
EXAMPLES = {
"1": {
"person": "./examples/person_images/Arnav_A.jpg",
"garment": "./examples/garment_images/gucci upper.jpg",
"description": "Arnav wearing Gucci upper garment"
},
"2": {
"person": "./examples/person_images/korean girl.png",
"garment": "./examples/garment_images/gucci upper.jpg",
"description": "Korean girl wearing Gucci upper garment"
},
"3": {
"person": "./examples/person_images/will_smith.jpg",
"garment": "./examples/garment_images/gucci upper.jpg",
"description": "Will Smith wearing Gucci upper garment"
},
"4": {
"person": "./examples/person_images/Arnav_A.jpg",
"garment": "./examples/garment_images/upper_2.jpg",
"description": "Arnav wearing alternative upper garment"
},
"5": {
"person": "./examples/person_images/korean girl.png",
"garment": "./examples/garment_images/upper_2.jpg",
"description": "Korean girl wearing alternative upper garment"
}
}
def run_example(example_key):
"""Run a specific example"""
if example_key not in EXAMPLES:
print(f" Example {example_key} not found!")
return
example = EXAMPLES[example_key]
print(f"\n Running Example {example_key}: {example['description']}")
print(f"👤 Person: {example['person']}")
print(f"👕 Garment: {example['garment']}")
# Check if files exist
if not Path(example['person']).exists():
print(f" Person image not found: {example['person']}")
return
if not Path(example['garment']).exists():
print(f" Garment image not found: {example['garment']}")
return
print("⏳ Processing virtual try-on...")
start_time = time.time()
try:
result = client.predict(
dict={
"background": handle_file(example['person']),
"layers": [],
"composite": None
},
garm_img=handle_file(example['garment']),
garment_des=example['description'],
is_checked=True,
is_checked_crop=False,
denoise_steps=30,
seed=42,
api_name="/tryon"
)
end_time = time.time()
print(f" Completed in {end_time - start_time:.1f} seconds")
# Save results with meaningful names
timestamp = int(time.time())
result_name = f"example_{example_key}_{timestamp}"
# Copy results to examples/results folder
if len(result) >= 2:
import shutil
result_path = f"./examples/results/{result_name}_tryon.png"
mask_path = f"./examples/results/{result_name}_mask.png"
shutil.copy(result[0], result_path)
shutil.copy(result[1], mask_path)
print(f" Results saved:")
print(f" Main result: {result_path}")
print(f" Mask result: {mask_path}")
except Exception as e:
print(f" Error during processing: {e}")
def main():
"""Main interactive menu"""
print("\n" + "="*50)
print("IDM-VTON Example Runner")
print("="*50)
print("\nAvailable Examples:")
for key, example in EXAMPLES.items():
print(f"{key}. {example['description']}")
print("\nCommands:")
print("• Enter example number (1-5) to run")
print("• 'all' to run all examples")
print("• 'q' to quit")
while True:
choice = input("\nEnter your choice: ").strip().lower()
if choice == 'q':
print("👋 Goodbye!")
break
elif choice == 'all':
print(" Running all examples...")
for key in EXAMPLES.keys():
run_example(key)
print("-" * 30)
elif choice in EXAMPLES:
run_example(choice)
else:
print("Invalid choice. Please enter 1-5, 'all', or 'q'")
if __name__ == "__main__":
main()