alon-astria commited on
Commit
616f770
·
verified ·
1 Parent(s): 61c8220

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +80 -10
app.py CHANGED
@@ -45,7 +45,7 @@ def virtual_tryon(human_img_path, garment_img_path, garment_type):
45
  "tune[title]": f"Gradio VTO - {int(time.time())}",
46
  "tune[name]": garment_type,
47
  "tune[model_type]": "faceid",
48
- "tune[base_tune_id]": "1504944",
49
  "tune[prompts_attributes][][text]": f"",
50
  "tune[prompts_attributes][][num_images]": 1,
51
  "tune[prompts_attributes][][denoising_strength]": "0.0",
@@ -64,7 +64,6 @@ def virtual_tryon(human_img_path, garment_img_path, garment_type):
64
  raise gr.Error(f"Failed to get prompt ID from tune creation. API response: {tune_data}")
65
 
66
  except requests.exceptions.RequestException as e:
67
- # UPDATED: Add response text to the error message for better debugging.
68
  error_message = f"API request failed: {e}"
69
  if hasattr(e, 'response') and e.response is not None:
70
  error_message += f". Response: {e.response.text}"
@@ -98,11 +97,9 @@ def virtual_tryon(human_img_path, garment_img_path, garment_type):
98
  else:
99
  yield None, "Step 2/3: Model is training. Checking again in 10 seconds..."
100
 
101
-
102
  time.sleep(10) # Wait before polling again
103
 
104
  except requests.exceptions.RequestException as e:
105
- # UPDATED: Add response text to the error message for better debugging.
106
  error_message = f"Failed to poll for status: {e}"
107
  if hasattr(e, 'response') and e.response is not None:
108
  error_message += f". Response: {e.response.text}"
@@ -112,18 +109,81 @@ def virtual_tryon(human_img_path, garment_img_path, garment_type):
112
 
113
  raise gr.Error("Generation timed out after 6 minutes. Please try again.")
114
 
115
-
116
  # --- Gradio User Interface Definition ---
117
- css = ".container { max-width: 730px; margin: auto; padding-top: 1.5rem; }"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
118
 
119
  with gr.Blocks(css=css, theme=gr.themes.Soft()) as demo:
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
120
  gr.Markdown(
121
  """
122
- # Virtual Fashion Try-On with Astria API
123
- Upload a photo of a person and a photo of a piece of clothing to see them wear it.
124
  **Important:** This Space requires an `ASTRIA_API_KEY`. You can get one from [Astria.ai](https://www.astria.ai/) and set it as a secret in the repository settings.
125
  """
126
  )
 
 
127
  with gr.Row():
128
  with gr.Column(scale=1):
129
  human_image = gr.Image(type="filepath", label="Human Image", height=300)
@@ -136,9 +196,19 @@ with gr.Blocks(css=css, theme=gr.themes.Soft()) as demo:
136
  )
137
  submit_btn = gr.Button("Generate", variant="primary")
138
  with gr.Column(scale=1):
139
- status_text = gr.Textbox(label="Status", interactive=False, lines=2)
140
  result_image = gr.Image(label="Result", height=615, interactive=False)
141
 
 
 
 
 
 
 
 
 
 
 
142
  submit_btn.click(
143
  fn=virtual_tryon,
144
  inputs=[human_image, garment_image, garment_type],
@@ -146,4 +216,4 @@ with gr.Blocks(css=css, theme=gr.themes.Soft()) as demo:
146
  )
147
 
148
  if __name__ == "__main__":
149
- demo.launch()
 
45
  "tune[title]": f"Gradio VTO - {int(time.time())}",
46
  "tune[name]": garment_type,
47
  "tune[model_type]": "faceid",
48
+ "tune[base_tune_id]": "1504944", # This is the base model ID for Virtual Try-On
49
  "tune[prompts_attributes][][text]": f"",
50
  "tune[prompts_attributes][][num_images]": 1,
51
  "tune[prompts_attributes][][denoising_strength]": "0.0",
 
64
  raise gr.Error(f"Failed to get prompt ID from tune creation. API response: {tune_data}")
65
 
66
  except requests.exceptions.RequestException as e:
 
67
  error_message = f"API request failed: {e}"
68
  if hasattr(e, 'response') and e.response is not None:
69
  error_message += f". Response: {e.response.text}"
 
97
  else:
98
  yield None, "Step 2/3: Model is training. Checking again in 10 seconds..."
99
 
 
100
  time.sleep(10) # Wait before polling again
101
 
102
  except requests.exceptions.RequestException as e:
 
103
  error_message = f"Failed to poll for status: {e}"
104
  if hasattr(e, 'response') and e.response is not None:
105
  error_message += f". Response: {e.response.text}"
 
109
 
110
  raise gr.Error("Generation timed out after 6 minutes. Please try again.")
111
 
 
112
  # --- Gradio User Interface Definition ---
113
+ css = ".container { max-width: 800px; margin: auto; padding-top: 1.5rem; }"
114
+
115
+ # --- Python code snippet for the API docs ---
116
+ code_snippet = """
117
+ import requests
118
+ import os
119
+ import time
120
+
121
+ # --- Configuration ---
122
+ ASTRIA_API_BASE_URL = "https://api.astria.ai"
123
+ ASTRIA_API_KEY = os.environ.get("ASTRIA_API_KEY") # Recommended: Load from environment
124
+
125
+ # --- Image and Parameter Setup ---
126
+ human_img_path = "path/to/your/human.jpg"
127
+ garment_img_path = "path/to/your/garment.jpg"
128
+ garment_type = "shirt" # Can be "shirt", "pants", or "dress"
129
+
130
+ headers = {"Authorization": f"Bearer {ASTRIA_API_KEY}"}
131
+
132
+ # --- API Request ---
133
+ with open(human_img_path, "rb") as human_f, open(garment_img_path, "rb") as garment_f:
134
+ files = [
135
+ # The garment image is a regular tune image
136
+ ("tune[images][]", ("garment.jpg", garment_f.read(), "image/jpeg")),
137
+ # The human image is the input_image for the prompt
138
+ ("tune[prompts_attributes][][input_image]", ("human.jpg", human_f.read(), "image/jpeg")),
139
+ ]
140
+ data = {
141
+ "tune[title]": f"My VTO API Test - {int(time.time())}",
142
+ "tune[name]": garment_type,
143
+ "tune[model_type]": "faceid",
144
+ "tune[base_tune_id]": "1504944", # VTO Base Model ID
145
+ "tune[prompts_attributes][][text]": "", # No text prompt needed for VTO
146
+ "tune[prompts_attributes][][num_images]": 1,
147
+ "tune[prompts_attributes][][denoising_strength]": "0.0", # Must be 0 for VTO
148
+ }
149
+ # Create the tune and prompt in one call
150
+ response = requests.post(f"{ASTRIA_API_BASE_URL}/tunes", files=files, data=data, headers=headers)
151
+
152
+ print("--- Initial Response ---")
153
+ print(response.json())
154
+
155
+ # You would then poll the prompt ID from the response above to get the final image.
156
+ # See this Space's app.py for a full polling example.
157
+ """
158
 
159
  with gr.Blocks(css=css, theme=gr.themes.Soft()) as demo:
160
+ # --- Banner and Main Title ---
161
+ gr.HTML(
162
+ """
163
+ <div style="text-align: center; padding: 20px; background-color: #f0f8ff; border-radius: 10px; margin-bottom: 20px;">
164
+ <h2 style="color: #333;">Powered by the Astria.ai API</h2>
165
+ <p style="color: #555; font-size: 1.1em;">This Virtual Try-On demo uses a powerful, single-call API endpoint.
166
+ <br>
167
+ You can integrate this functionality directly into your own applications.
168
+ </p>
169
+ <a href="https://docs.astria.ai/docs/use-cases/virtual-try-on/" target="_blank" style="text-decoration: none;">
170
+ <button style="padding: 10px 20px; font-size: 1em; color: white; background-color: #007bff; border: none; border-radius: 5px; cursor: pointer;">
171
+ Read the Full API Documentation
172
+ </button>
173
+ </a>
174
+ </div>
175
+ """
176
+ )
177
+ gr.Markdown("# Virtual Fashion Try-On")
178
  gr.Markdown(
179
  """
180
+ **Instructions:** Upload a photo of a person and a photo of a piece of clothing to see them wear it.
181
+
182
  **Important:** This Space requires an `ASTRIA_API_KEY`. You can get one from [Astria.ai](https://www.astria.ai/) and set it as a secret in the repository settings.
183
  """
184
  )
185
+
186
+ # --- Main Application UI ---
187
  with gr.Row():
188
  with gr.Column(scale=1):
189
  human_image = gr.Image(type="filepath", label="Human Image", height=300)
 
196
  )
197
  submit_btn = gr.Button("Generate", variant="primary")
198
  with gr.Column(scale=1):
199
+ status_text = gr.Textbox(label="Status", interactive=False, lines=2, placeholder="Upload images and click Generate...")
200
  result_image = gr.Image(label="Result", height=615, interactive=False)
201
 
202
+ # --- API Usage Code Snippet ---
203
+ with gr.Accordion("Show API Usage Code (Python)", open=False):
204
+ gr.Code(
205
+ value=code_snippet,
206
+ language="python",
207
+ label="Python Request Example",
208
+ interactive=False
209
+ )
210
+
211
+ # --- Event Listener ---
212
  submit_btn.click(
213
  fn=virtual_tryon,
214
  inputs=[human_image, garment_image, garment_type],
 
216
  )
217
 
218
  if __name__ == "__main__":
219
+ demo.launch()