taesiri commited on
Commit
e7ddab0
·
1 Parent(s): d03f6fb
Files changed (1) hide show
  1. app.py +63 -2
app.py CHANGED
@@ -4,10 +4,52 @@ from PIL import Image
4
  import io
5
  import time
6
  import os
 
7
 
8
 
9
  access_token = os.environ.get("HUGGINGFACE_TOKEN")
10
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
11
  # Load and prepare the dataset
12
  dataset = load_dataset(
13
  "taesiri/PhotoshopRequest-DailyDump",
@@ -16,6 +58,14 @@ dataset = load_dataset(
16
  token=access_token,
17
  )
18
 
 
 
 
 
 
 
 
 
19
  BUFFER_SIZE = 1
20
  sample_iterator = None
21
  sample_count = 0
@@ -33,6 +83,7 @@ reshuffle_dataset() # Initial shuffle
33
 
34
 
35
  def get_next_sample():
 
36
  global sample_count
37
 
38
  if sample_count >= BUFFER_SIZE:
@@ -60,7 +111,6 @@ with gr.Blocks() as demo:
60
  """
61
  This is a preview of the PhotoshopRequest dataset. Each sample represents a Photoshop editing request post.
62
  Click the 'Sample New Item' button to retrieve a random sample from the dataset.
63
- </hr>
64
  """
65
  )
66
 
@@ -72,9 +122,20 @@ with gr.Blocks() as demo:
72
 
73
  sample_button = gr.Button("Sample New Item")
74
 
 
 
 
 
 
 
 
 
 
 
 
75
  sample_button.click(
76
  get_next_sample, outputs=[post_info, source_image, edited_image]
77
- )
78
 
79
  if __name__ == "__main__":
80
  demo.launch()
 
4
  import io
5
  import time
6
  import os
7
+ from datetime import datetime, timedelta
8
 
9
 
10
  access_token = os.environ.get("HUGGINGFACE_TOKEN")
11
 
12
+ # Global variables
13
+ dataset = None
14
+ dataset_size = "Unknown"
15
+ last_refresh_time = None
16
+ REFRESH_INTERVAL = timedelta(hours=24)
17
+
18
+
19
+ def load_and_prepare_dataset():
20
+ global dataset, dataset_size, last_refresh_time
21
+
22
+ dataset = load_dataset(
23
+ "taesiri/PhotoshopRequest-DailyDump",
24
+ split="train",
25
+ streaming=True,
26
+ token=access_token,
27
+ )
28
+
29
+ # Get dataset info
30
+ dataset_info = dataset.info
31
+ dataset_size = (
32
+ dataset_info.splits["train"].num_examples
33
+ if dataset_info.splits.get("train")
34
+ else "Unknown"
35
+ )
36
+
37
+ last_refresh_time = datetime.now()
38
+
39
+
40
+ def check_and_refresh_dataset():
41
+ global last_refresh_time
42
+ current_time = datetime.now()
43
+ if (
44
+ last_refresh_time is None
45
+ or (current_time - last_refresh_time) >= REFRESH_INTERVAL
46
+ ):
47
+ load_and_prepare_dataset()
48
+
49
+
50
+ # Initial dataset load
51
+ load_and_prepare_dataset()
52
+
53
  # Load and prepare the dataset
54
  dataset = load_dataset(
55
  "taesiri/PhotoshopRequest-DailyDump",
 
58
  token=access_token,
59
  )
60
 
61
+ # Get dataset info
62
+ dataset_info = dataset.info
63
+ dataset_size = (
64
+ dataset_info.splits["train"].num_examples
65
+ if dataset_info.splits.get("train")
66
+ else "Unknown"
67
+ )
68
+
69
  BUFFER_SIZE = 1
70
  sample_iterator = None
71
  sample_count = 0
 
83
 
84
 
85
  def get_next_sample():
86
+ check_and_refresh_dataset()
87
  global sample_count
88
 
89
  if sample_count >= BUFFER_SIZE:
 
111
  """
112
  This is a preview of the PhotoshopRequest dataset. Each sample represents a Photoshop editing request post.
113
  Click the 'Sample New Item' button to retrieve a random sample from the dataset.
 
114
  """
115
  )
116
 
 
122
 
123
  sample_button = gr.Button("Sample New Item")
124
 
125
+ info_md = gr.Markdown()
126
+
127
+ def update_info():
128
+ return f"""
129
+ <div style="text-align: center;">
130
+ <hr>
131
+ Dataset Size: {dataset_size} items<br>
132
+ Last Refreshed: {last_refresh_time.strftime('%Y-%m-%d %H:%M:%S UTC') if last_refresh_time else 'Unknown'}
133
+ </div>
134
+ """
135
+
136
  sample_button.click(
137
  get_next_sample, outputs=[post_info, source_image, edited_image]
138
+ ).then(update_info, outputs=[info_md])
139
 
140
  if __name__ == "__main__":
141
  demo.launch()