aeon12 commited on
Commit
efaf372
·
verified ·
1 Parent(s): 0701c30

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +36 -49
app.py CHANGED
@@ -36,25 +36,33 @@ scene_options = [
36
  "Shopping Mall", "Concert", "Sports Stadium"
37
  ]
38
 
39
- def get_base64_from_url(url):
40
- response = requests.get(url)
41
- return base64.b64encode(response.content).decode('utf-8')
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
42
 
43
  def get_base64_from_image(image):
44
  buffered = io.BytesIO()
45
  image.save(buffered, format="PNG")
46
  return base64.b64encode(buffered.getvalue()).decode('utf-8')
47
 
48
- def generate_tryon(cloth_input, user_input, background):
49
- if isinstance(cloth_input, str): # URL selected
50
- cloth_base64 = get_base64_from_url(cloth_input)
51
- else: # Image uploaded
52
- cloth_base64 = get_base64_from_image(cloth_input)
53
-
54
- if isinstance(user_input, str): # URL selected
55
- user_base64 = get_base64_from_url(user_input)
56
- else: # Image uploaded
57
- user_base64 = get_base64_from_image(user_input)
58
 
59
  input_data = {
60
  "user_image": user_base64,
@@ -95,14 +103,10 @@ def generate_tryon(cloth_input, user_input, background):
95
  raise Exception(f"Job processing failed: {status_data}")
96
 
97
  time.sleep(2)
98
- def tryon_interface(cloth_selected, cloth_upload, user_selected, user_upload, scene_selection, custom_scene):
99
- # Use selected cloth if available, otherwise use uploaded cloth
100
- cloth = cloth_selected if cloth_selected is not None else cloth_upload
101
-
102
- # Use selected user image if available, otherwise use uploaded user image
103
- user = user_selected if user_selected is not None else user_upload
104
-
105
- # Use custom scene if provided, otherwise use selected scene
106
  background = custom_scene if custom_scene else scene_selection
107
 
108
  if cloth is None:
@@ -118,7 +122,6 @@ def tryon_interface(cloth_selected, cloth_upload, user_selected, user_upload, sc
118
  except Exception as e:
119
  return None, f"Error: {str(e)}"
120
 
121
-
122
  with gr.Blocks() as demo:
123
  gr.Markdown("# TryItOut.AI")
124
 
@@ -126,25 +129,24 @@ with gr.Blocks() as demo:
126
  with gr.Column():
127
  gr.Markdown("## Available Clothing")
128
  cloth_gallery = gr.Gallery(
129
- [img["url"] for img in cloth_images],
130
  label="Click to select clothing",
131
  columns=4,
132
  height=500
133
  )
134
- cloth_selected = gr.State(value=None)
135
- print(cloth_selected)
136
- cloth_upload = gr.Image(label="Or Upload Custom Clothing", type="numpy", visible=False)
137
 
138
  with gr.Column():
139
  gr.Markdown("## Available User Images")
140
  user_gallery = gr.Gallery(
141
- [img["url"] for img in user_images],
142
  label="Click to select user image",
143
  columns=3,
144
  height=500
145
  )
146
- user_selected = gr.State(value=None)
147
- user_upload = gr.Image(label="Or Upload Custom User Image", type="numpy", visible=False)
148
 
149
  with gr.Row():
150
  scene_selection = gr.Dropdown(choices=scene_options, label="Select Scene")
@@ -155,38 +157,23 @@ with gr.Blocks() as demo:
155
  output_image = gr.Image(label="Try-On Result")
156
  output_text = gr.Textbox(label="Status")
157
 
158
- def update_selected(evt: gr.SelectData, gallery_images):
159
- return gallery_images[evt.index]["url"]
160
-
161
- def toggle_upload(gallery_selected, upload_component):
162
- return gr.update(visible=gallery_selected is None)
163
 
164
  cloth_gallery.select(
165
  update_selected,
166
- inputs=[cloth_gallery],
167
- outputs=[cloth_selected]
168
- ).then(
169
- toggle_upload,
170
- inputs=[cloth_selected],
171
- outputs=[cloth_upload]
172
  )
173
 
174
  user_gallery.select(
175
  update_selected,
176
- inputs=[user_gallery],
177
- outputs=[user_selected]
178
- ).then(
179
- toggle_upload,
180
- inputs=[user_selected],
181
- outputs=[user_upload]
182
  )
183
 
184
-
185
  generate_button.click(
186
  tryon_interface,
187
- inputs=[cloth_selected, cloth_upload, user_selected, user_upload, scene_selection, custom_scene],
188
  outputs=[output_image, output_text]
189
  )
190
 
191
-
192
  demo.launch()
 
36
  "Shopping Mall", "Concert", "Sports Stadium"
37
  ]
38
 
39
+ def fetch_and_process_images(image_list):
40
+ processed_images = []
41
+ for img in image_list:
42
+ try:
43
+ response = requests.get(img['url'])
44
+ image = Image.open(io.BytesIO(response.content))
45
+ processed_images.append({
46
+ "image": image,
47
+ "label": img['label'],
48
+ "url": img['url']
49
+ })
50
+ except Exception as e:
51
+ print(f"Error processing image {img['url']}: {str(e)}")
52
+ return processed_images
53
+
54
+ # Fetch and process images at startup
55
+ processed_cloth_images = fetch_and_process_images(cloth_images)
56
+ processed_user_images = fetch_and_process_images(user_images)
57
 
58
  def get_base64_from_image(image):
59
  buffered = io.BytesIO()
60
  image.save(buffered, format="PNG")
61
  return base64.b64encode(buffered.getvalue()).decode('utf-8')
62
 
63
+ def generate_tryon(cloth_image, user_image, background):
64
+ cloth_base64 = get_base64_from_image(cloth_image)
65
+ user_base64 = get_base64_from_image(user_image)
 
 
 
 
 
 
 
66
 
67
  input_data = {
68
  "user_image": user_base64,
 
103
  raise Exception(f"Job processing failed: {status_data}")
104
 
105
  time.sleep(2)
106
+
107
+ def tryon_interface(cloth_index, cloth_upload, user_index, user_upload, scene_selection, custom_scene):
108
+ cloth = processed_cloth_images[cloth_index]['image'] if cloth_index is not None else cloth_upload
109
+ user = processed_user_images[user_index]['image'] if user_index is not None else user_upload
 
 
 
 
110
  background = custom_scene if custom_scene else scene_selection
111
 
112
  if cloth is None:
 
122
  except Exception as e:
123
  return None, f"Error: {str(e)}"
124
 
 
125
  with gr.Blocks() as demo:
126
  gr.Markdown("# TryItOut.AI")
127
 
 
129
  with gr.Column():
130
  gr.Markdown("## Available Clothing")
131
  cloth_gallery = gr.Gallery(
132
+ [img["url"] for img in processed_cloth_images],
133
  label="Click to select clothing",
134
  columns=4,
135
  height=500
136
  )
137
+ cloth_index = gr.State(value=None)
138
+ cloth_upload = gr.Image(label="Or Upload Custom Clothing", type="pil")
 
139
 
140
  with gr.Column():
141
  gr.Markdown("## Available User Images")
142
  user_gallery = gr.Gallery(
143
+ [img["url"] for img in processed_user_images],
144
  label="Click to select user image",
145
  columns=3,
146
  height=500
147
  )
148
+ user_index = gr.State(value=None)
149
+ user_upload = gr.Image(label="Or Upload Custom User Image", type="pil")
150
 
151
  with gr.Row():
152
  scene_selection = gr.Dropdown(choices=scene_options, label="Select Scene")
 
157
  output_image = gr.Image(label="Try-On Result")
158
  output_text = gr.Textbox(label="Status")
159
 
160
+ def update_selected(evt: gr.SelectData):
161
+ return evt.index
 
 
 
162
 
163
  cloth_gallery.select(
164
  update_selected,
165
+ outputs=[cloth_index]
 
 
 
 
 
166
  )
167
 
168
  user_gallery.select(
169
  update_selected,
170
+ outputs=[user_index]
 
 
 
 
 
171
  )
172
 
 
173
  generate_button.click(
174
  tryon_interface,
175
+ inputs=[cloth_index, cloth_upload, user_index, user_upload, scene_selection, custom_scene],
176
  outputs=[output_image, output_text]
177
  )
178
 
 
179
  demo.launch()