Wauplin HF staff commited on
Commit
b61bd8e
1 Parent(s): 8f527d3

Update app_parquet.py

Browse files
Files changed (1) hide show
  1. app_parquet.py +11 -16
app_parquet.py CHANGED
@@ -187,16 +187,16 @@ PARQUET_DATASET_DIR.mkdir(parents=True, exist_ok=True)
187
  scheduler = ParquetScheduler(repo_id="example-space-to-dataset-parquet")
188
 
189
  # client = Client("stabilityai/stable-diffusion") # Space is paused
190
- client = Client("runwayml/stable-diffusion-v1-5")
 
191
 
192
 
193
  def generate(prompt: str) -> tuple[str, list[str]]:
194
  """Generate images on 'submit' button."""
195
  # Generate from https://huggingface.co/spaces/stabilityai/stable-diffusion
196
  # out_dir = client.predict(prompt, "", 9, fn_index=1) # Space 'stabilityai/stable-diffusion' is paused
197
- out_dir = client.predict(prompt, fn_index=1)
198
- with (Path(out_dir) / "captions.json").open() as f:
199
- paths = list(json.load(f).keys())
200
 
201
  # Save config used to generate data
202
  with tempfile.NamedTemporaryFile(
@@ -206,7 +206,7 @@ def generate(prompt: str) -> tuple[str, list[str]]:
206
  {"prompt": prompt, "negative_prompt": "", "guidance_scale": 9}, config_file
207
  )
208
 
209
- return config_file.name, paths
210
 
211
 
212
  def get_selected_index(evt: gr.SelectData) -> int:
@@ -215,7 +215,7 @@ def get_selected_index(evt: gr.SelectData) -> int:
215
 
216
 
217
  def save_preference(
218
- config_path: str, gallery: list[dict[str, Any]], selected_index: int
219
  ) -> None:
220
  """Save preference, i.e. move images to a new folder and send paths+config to scheduler."""
221
  save_dir = PARQUET_DATASET_DIR / f"{uuid.uuid4()}"
@@ -226,14 +226,14 @@ def save_preference(
226
  data = json.load(f)
227
 
228
  # Add selected item + timestamp
229
- data["selected_index"] = selected_index
230
  data["timestamp"] = datetime.datetime.utcnow().isoformat()
231
 
232
  # Copy and add images
233
- for index, path in enumerate(x["name"] for x in gallery):
234
  name = f"{index:03d}"
235
- dst_path = save_dir / f"{name}{Path(path).suffix}"
236
- shutil.move(path, dst_path)
237
  data[f"image_{name}"] = dst_path
238
 
239
  # Send to scheduler
@@ -261,14 +261,9 @@ def get_demo():
261
  )
262
 
263
  # Save preference on click
264
- gallery.select(
265
- fn=get_selected_index,
266
- outputs=selected_index,
267
- queue=False,
268
- )
269
  save_preference_button.click(
270
  fn=save_preference,
271
- inputs=[config_path, gallery, selected_index],
272
  queue=False,
273
  ).then(
274
  fn=clear,
 
187
  scheduler = ParquetScheduler(repo_id="example-space-to-dataset-parquet")
188
 
189
  # client = Client("stabilityai/stable-diffusion") # Space is paused
190
+ # client = Client("runwayml/stable-diffusion-v1-5") # Space has been deleted
191
+ client = Client("black-forest-labs/FLUX.1-schnell")
192
 
193
 
194
  def generate(prompt: str) -> tuple[str, list[str]]:
195
  """Generate images on 'submit' button."""
196
  # Generate from https://huggingface.co/spaces/stabilityai/stable-diffusion
197
  # out_dir = client.predict(prompt, "", 9, fn_index=1) # Space 'stabilityai/stable-diffusion' is paused
198
+ # out_dir = client.predict(prompt, fn_index=1) # Space "runwayml/stable-diffusion-v1-5" has been deleted
199
+ image_path, _ = client.predict(prompt, api_name="/infer")
 
200
 
201
  # Save config used to generate data
202
  with tempfile.NamedTemporaryFile(
 
206
  {"prompt": prompt, "negative_prompt": "", "guidance_scale": 9}, config_file
207
  )
208
 
209
+ return config_file.name, image_path
210
 
211
 
212
  def get_selected_index(evt: gr.SelectData) -> int:
 
215
 
216
 
217
  def save_preference(
218
+ config_path: str, image_path: str
219
  ) -> None:
220
  """Save preference, i.e. move images to a new folder and send paths+config to scheduler."""
221
  save_dir = PARQUET_DATASET_DIR / f"{uuid.uuid4()}"
 
226
  data = json.load(f)
227
 
228
  # Add selected item + timestamp
229
+ data["selected_index"] = 0
230
  data["timestamp"] = datetime.datetime.utcnow().isoformat()
231
 
232
  # Copy and add images
233
+ for index in range(4): # fake 4 images
234
  name = f"{index:03d}"
235
+ dst_path = save_dir / f"{name}{Path(image_path).suffix}"
236
+ shutil.copyfile(image_path, dst_path)
237
  data[f"image_{name}"] = dst_path
238
 
239
  # Send to scheduler
 
261
  )
262
 
263
  # Save preference on click
 
 
 
 
 
264
  save_preference_button.click(
265
  fn=save_preference,
266
+ inputs=[config_path, gallery],
267
  queue=False,
268
  ).then(
269
  fn=clear,