blanchon commited on
Commit
5c9c31b
·
1 Parent(s): 165adcf

Add flag save

Browse files
Files changed (1) hide show
  1. app.py +44 -31
app.py CHANGED
@@ -1,7 +1,6 @@
1
  import csv
2
  import json
3
  import math
4
- import os
5
  import secrets
6
  from pathlib import Path
7
  from typing import cast
@@ -13,6 +12,7 @@ import torch
13
  from diffusers import FluxFillPipeline
14
  from gradio.components.gallery import GalleryMediaType
15
  from gradio.components.image_editor import EditorValue
 
16
  from PIL import Image, ImageFilter, ImageOps
17
 
18
  DEVICE = "cuda"
@@ -25,38 +25,44 @@ SYSTEM_PROMPT = r"""This two-panel split-frame image showcases a furniture in as
25
 
26
  MASK_CONTEXT_PADDING = 16 * 8
27
 
28
- if os.environ.get("IN_SPACES", None):
29
- FLAG_PATH = Path("/data/") / "flagged_data_points"
 
 
 
 
 
30
  else:
31
- FLAG_PATH = Path(__file__).parent / "flagged_data_points"
 
 
 
 
32
 
33
  EXAMPLES: dict[str, list[str, str, str, list[str]]] = {}
34
 
35
- if not FLAG_PATH.exists():
36
- FLAG_PATH.mkdir(parents=True)
37
- else:
38
- flag_files = FLAG_PATH.glob("dataset*.csv")
39
- for flag_file in flag_files:
40
- with flag_file.open("r") as file:
41
- reader = csv.reader(file)
42
- next(reader)
43
- for row in reader:
44
- furniture_image, room_image, results_values, time = row
45
- room_image = json.loads(room_image)
46
- room_image_background = room_image["background"]
47
- room_image_layers = room_image["layers"]
48
- room_image_composite = room_image["composite"]
49
- results_values = json.loads(results_values)
50
- results_values = [result["image"] for result in results_values]
51
- EXAMPLES[time] = [
52
- furniture_image,
53
- {
54
- "background": room_image_background,
55
- "layers": room_image_layers,
56
- "composite": room_image_composite,
57
- },
58
- # results_values,
59
- ]
60
 
61
  if not torch.cuda.is_available():
62
 
@@ -221,6 +227,13 @@ def flag(
221
  callback.flag(
222
  flag_data=[furniture_image_input, room_image_input, results],
223
  )
 
 
 
 
 
 
 
224
 
225
 
226
  @spaces.GPU(duration=150)
@@ -552,9 +565,9 @@ with gr.Blocks(css=css) as demo:
552
  [
553
  furniture_image_input,
554
  room_image_input,
555
- # results,
556
  ],
557
- "flagged_data_points",
558
  )
559
 
560
  with gr.Column(elem_id="col-showcase"):
 
1
  import csv
2
  import json
3
  import math
 
4
  import secrets
5
  from pathlib import Path
6
  from typing import cast
 
12
  from diffusers import FluxFillPipeline
13
  from gradio.components.gallery import GalleryMediaType
14
  from gradio.components.image_editor import EditorValue
15
+ from huggingface_hub import HfApi
16
  from PIL import Image, ImageFilter, ImageOps
17
 
18
  DEVICE = "cuda"
 
25
 
26
  MASK_CONTEXT_PADDING = 16 * 8
27
 
28
+ api = HfApi()
29
+
30
+ # Download the blanchon/FurnitureFlags init Path(__file__).parent / examples_dataset
31
+ FLAG_PATH = Path(__file__).parent / "examples_dataset"
32
+ if not torch.cuda.is_available():
33
+ FLAG_PATH = Path(__file__).parent / "examples_dataset"
34
+ FLAG_PATH.mkdir(parents=True, exist_ok=True)
35
  else:
36
+ api.snapshot_download(
37
+ repo_id="blanchon/FurnitureFlags",
38
+ local_dir=FLAG_PATH,
39
+ repo_type="dataset",
40
+ )
41
 
42
  EXAMPLES: dict[str, list[str, str, str, list[str]]] = {}
43
 
44
+ flag_files = FLAG_PATH.glob("dataset*.csv")
45
+ for flag_file in flag_files:
46
+ with flag_file.open("r") as file:
47
+ reader = csv.reader(file)
48
+ next(reader)
49
+ for row in reader:
50
+ furniture_image, room_image, results_values, time = row
51
+ room_image = json.loads(room_image)
52
+ room_image_background = room_image["background"]
53
+ room_image_layers = room_image["layers"]
54
+ room_image_composite = room_image["composite"]
55
+ results_values = json.loads(results_values)
56
+ results_values = [result["image"] for result in results_values]
57
+ EXAMPLES[time] = [
58
+ furniture_image,
59
+ {
60
+ "background": room_image_background,
61
+ "layers": room_image_layers,
62
+ "composite": room_image_composite,
63
+ },
64
+ # results_values,
65
+ ]
 
 
 
66
 
67
  if not torch.cuda.is_available():
68
 
 
227
  callback.flag(
228
  flag_data=[furniture_image_input, room_image_input, results],
229
  )
230
+ # Upload the flagged data points to the hub
231
+ api.upload_folder(
232
+ repo_id="blanchon/FurnitureFlags",
233
+ repo_type="dataset",
234
+ folder_path=FLAG_PATH,
235
+ ignore_patterns=[".cache"],
236
+ )
237
 
238
 
239
  @spaces.GPU(duration=150)
 
565
  [
566
  furniture_image_input,
567
  room_image_input,
568
+ results,
569
  ],
570
+ "examples_dataset",
571
  )
572
 
573
  with gr.Column(elem_id="col-showcase"):