rahul7star commited on
Commit
9fed8d8
·
verified ·
1 Parent(s): a5a83f4

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +76 -53
app.py CHANGED
@@ -34,28 +34,29 @@ def quota_guard(fn):
34
  return fn(*args)
35
  return wrapper
36
 
37
-
38
  # ======================================================
39
  # CONFIG
40
  # ======================================================
41
- REPO_ID = "rahul7star/WAN22-Mar-2026" # PRO repo
42
  FREE_REPO = "rahul7star/ohamlab"
43
  FREE_IMAGE_DIR = "showcase/image"
 
 
 
 
44
 
45
- HF_TOKEN = os.getenv("HF_TOKEN") # HuggingFace token for private repo
46
  APP_PASSWORD = os.getenv("APP_PASSWORD")
47
  if not APP_PASSWORD:
48
  raise RuntimeError("APP_PASSWORD env var not set")
49
 
50
  api = HfApi()
51
 
52
-
53
  # ======================================================
54
  # FREE IMAGE PREVIEW
55
  # ======================================================
56
  def load_free_images():
57
  tmp = tempfile.mkdtemp()
58
- files = api.list_repo_files(FREE_REPO)
59
 
60
  images = [
61
  f for f in files
@@ -72,74 +73,76 @@ def load_free_images():
72
  local = []
73
  for img in picked:
74
  try:
75
- local.append(hf_hub_download(FREE_REPO, img, local_dir=tmp))
76
- except Exception:
77
- pass
 
78
 
79
  return local
80
 
81
  FREE_IMAGES = load_free_images()
82
 
83
-
84
  # ======================================================
85
  # PRO HELPERS
86
  # ======================================================
87
- def list_date_folders():
88
- """Return last 20 dates from repo"""
 
 
89
  files = api.list_repo_files(REPO_ID, token=HF_TOKEN)
90
  dates = sorted({f.split("/")[0] for f in files if "/" in f}, reverse=True)
91
- return dates[:20]
92
 
 
 
 
93
 
94
- def load_images_for_date(date_folder):
95
- """Download images locally and return local paths"""
96
- tmp = tempfile.mkdtemp()
97
  files = api.list_repo_files(REPO_ID, token=HF_TOKEN)
98
- images = []
99
 
100
  for f in files:
101
  if f.startswith(date_folder + "/") and f.lower().endswith((".png", ".jpg", ".jpeg", ".webp")):
102
- # always save as input_image.png to avoid weird names
103
- local_path = os.path.join(tmp, *f.split("/"))
104
- os.makedirs(os.path.dirname(local_path), exist_ok=True)
105
  try:
106
- hf_hub_download(REPO_ID, f, local_dir=os.path.dirname(local_path), token=HF_TOKEN, local_file_name=os.path.basename(local_path))
107
- images.append(local_path)
108
- print(f"[DOWNLOAD] {f} -> {local_path}")
 
 
 
 
 
 
 
 
109
  except Exception as e:
110
- print(f"[ERROR] Download failed {f}: {e}")
111
-
112
- print(f"[INFO] Total images for {date_folder}: {len(images)}")
113
- return images
114
 
 
 
 
115
 
116
  # ======================================================
117
  # PAGINATION
118
  # ======================================================
119
- def render_grid(date_folder, page, page_size):
120
- data = load_images_for_date(date_folder)
121
- total_pages = max(1, math.ceil(len(data) / page_size))
122
-
123
- page = max(0, min(int(page), total_pages - 1))
124
  start, end = page * page_size, (page + 1) * page_size
125
- page_images = data[start:end]
126
-
127
- print(f"[RENDER] Page {page+1}/{total_pages}, images: {page_images}")
128
-
129
- return page_images, page
130
-
131
-
132
- def next_page(date, page, size):
133
- return render_grid(date, page + 1, size)
134
 
 
 
135
 
136
- def prev_page(date, page, size):
137
- return render_grid(date, page - 1, size)
138
-
139
-
140
- def reset_page(date, size):
141
- return render_grid(date, 0, size)
142
 
 
 
143
 
144
  # ======================================================
145
  # AUTH
@@ -151,15 +154,14 @@ def check_password(pwd):
151
  gr.update(visible=True),
152
  gr.update(visible=False),
153
  gr.update(visible=True),
154
- [],
155
- 0,
156
  "❌ Wrong password",
157
  ""
158
  )
159
 
160
  dates = list_date_folders()
161
  latest = dates[0] if dates else ""
162
- images, page = render_grid(latest, 0, 6)
163
 
164
  return (
165
  gr.update(visible=False),
@@ -171,13 +173,25 @@ def check_password(pwd):
171
  latest
172
  )
173
 
174
-
175
  # ======================================================
176
  # CSS
177
  # ======================================================
178
  css = """
 
 
 
 
 
 
 
 
 
 
 
 
 
 
179
  footer, .gradio-footer { display:none!important; }
180
-
181
  #ohamlab-footer {
182
  position:fixed;
183
  bottom:0;
@@ -190,7 +204,6 @@ footer, .gradio-footer { display:none!important; }
190
  }
191
  """
192
 
193
-
194
  # ======================================================
195
  # UI
196
  # ======================================================
@@ -214,13 +227,23 @@ with gr.Blocks() as demo:
214
  current_date = gr.State("")
215
  page_state = gr.State(0)
216
  page_size = gr.State(6)
 
217
 
 
218
  gallery = gr.Gallery(label="Images", elem_id="gallery", columns=3, height="auto")
219
 
 
 
 
220
  with gr.Row():
221
  prev_btn = gr.Button("⬅ Previous")
222
  next_btn = gr.Button("Next ➡")
223
 
 
 
 
 
 
224
  prev_btn.click(prev_page, [current_date, page_state, page_size], [gallery, page_state])
225
  next_btn.click(next_page, [current_date, page_state, page_size], [gallery, page_state])
226
 
@@ -232,4 +255,4 @@ with gr.Blocks() as demo:
232
 
233
  gr.HTML("<div id='ohamlab-footer'>© OhamLab Copyright</div>")
234
 
235
- demo.launch(share=False)
 
34
  return fn(*args)
35
  return wrapper
36
 
 
37
  # ======================================================
38
  # CONFIG
39
  # ======================================================
40
+ REPO_ID = "rahul7star/WAN22-Mar-2026"
41
  FREE_REPO = "rahul7star/ohamlab"
42
  FREE_IMAGE_DIR = "showcase/image"
43
+ HF_TOKEN = os.getenv("HF_TOKEN") # Must have read access to private repo
44
+
45
+ if not HF_TOKEN:
46
+ raise RuntimeError("HF_TOKEN env var not set")
47
 
 
48
  APP_PASSWORD = os.getenv("APP_PASSWORD")
49
  if not APP_PASSWORD:
50
  raise RuntimeError("APP_PASSWORD env var not set")
51
 
52
  api = HfApi()
53
 
 
54
  # ======================================================
55
  # FREE IMAGE PREVIEW
56
  # ======================================================
57
  def load_free_images():
58
  tmp = tempfile.mkdtemp()
59
+ files = api.list_repo_files(FREE_REPO, token=HF_TOKEN)
60
 
61
  images = [
62
  f for f in files
 
73
  local = []
74
  for img in picked:
75
  try:
76
+ local_path = hf_hub_download(FREE_REPO, img, local_dir=tmp, token=HF_TOKEN)
77
+ local.append(local_path)
78
+ except Exception as e:
79
+ print(f"[FREE IMG ERROR] {img}: {e}")
80
 
81
  return local
82
 
83
  FREE_IMAGES = load_free_images()
84
 
 
85
  # ======================================================
86
  # PRO HELPERS
87
  # ======================================================
88
+ tmp_dir = tempfile.mkdtemp()
89
+ images_cache = {} # date_folder -> list of local image paths
90
+
91
+ def list_date_folders(last_n_days=20):
92
  files = api.list_repo_files(REPO_ID, token=HF_TOKEN)
93
  dates = sorted({f.split("/")[0] for f in files if "/" in f}, reverse=True)
94
+ return dates[:last_n_days]
95
 
96
+ def download_images_for_date(date_folder):
97
+ if date_folder in images_cache:
98
+ return images_cache[date_folder]
99
 
 
 
 
100
  files = api.list_repo_files(REPO_ID, token=HF_TOKEN)
101
+ local_paths = []
102
 
103
  for f in files:
104
  if f.startswith(date_folder + "/") and f.lower().endswith((".png", ".jpg", ".jpeg", ".webp")):
105
+ sub_path = os.path.join(tmp_dir, f)
106
+ os.makedirs(os.path.dirname(sub_path), exist_ok=True)
 
107
  try:
108
+ local_path = hf_hub_download(
109
+ REPO_ID,
110
+ f,
111
+ local_dir=os.path.dirname(sub_path),
112
+ token=HF_TOKEN
113
+ )
114
+ # Rename to a consistent name
115
+ final_path = os.path.join(os.path.dirname(local_path), "input_image.png")
116
+ os.rename(local_path, final_path)
117
+ local_paths.append(final_path)
118
+ print(f"[DOWNLOAD] {f} -> {final_path}")
119
  except Exception as e:
120
+ print(f"[DOWNLOAD ERROR] {f}: {e}")
 
 
 
121
 
122
+ images_cache[date_folder] = local_paths
123
+ print(f"[INFO] Total images for {date_folder}: {len(local_paths)}")
124
+ return local_paths
125
 
126
  # ======================================================
127
  # PAGINATION
128
  # ======================================================
129
+ def render_page(date_folder, page, page_size):
130
+ images = download_images_for_date(date_folder)
131
+ total_pages = max(1, math.ceil(len(images) / page_size))
132
+ page = max(0, min(page, total_pages - 1))
 
133
  start, end = page * page_size, (page + 1) * page_size
134
+ subset = images[start:end]
135
+ print(f"[RENDER] Page {page+1}/{total_pages}, images: {subset}")
136
+ return subset, page
 
 
 
 
 
 
137
 
138
+ def next_page(date_folder, page, page_size):
139
+ return render_page(date_folder, page + 1, page_size)
140
 
141
+ def prev_page(date_folder, page, page_size):
142
+ return render_page(date_folder, page - 1, page_size)
 
 
 
 
143
 
144
+ def reset_page(date_folder, page_size):
145
+ return render_page(date_folder, 0, page_size)
146
 
147
  # ======================================================
148
  # AUTH
 
154
  gr.update(visible=True),
155
  gr.update(visible=False),
156
  gr.update(visible=True),
157
+ [], 0,
 
158
  "❌ Wrong password",
159
  ""
160
  )
161
 
162
  dates = list_date_folders()
163
  latest = dates[0] if dates else ""
164
+ images, page = render_page(latest, 0, 6)
165
 
166
  return (
167
  gr.update(visible=False),
 
173
  latest
174
  )
175
 
 
176
  # ======================================================
177
  # CSS
178
  # ======================================================
179
  css = """
180
+ .grid {
181
+ display:grid;
182
+ grid-template-columns:repeat(auto-fill,minmax(220px,1fr));
183
+ gap:16px;
184
+ }
185
+ .card {
186
+ background:#0b1220;
187
+ padding:10px;
188
+ border-radius:14px;
189
+ }
190
+ .card img {
191
+ width:100%;
192
+ border-radius:10px;
193
+ }
194
  footer, .gradio-footer { display:none!important; }
 
195
  #ohamlab-footer {
196
  position:fixed;
197
  bottom:0;
 
204
  }
205
  """
206
 
 
207
  # ======================================================
208
  # UI
209
  # ======================================================
 
227
  current_date = gr.State("")
228
  page_state = gr.State(0)
229
  page_size = gr.State(6)
230
+ images_state = gr.State([])
231
 
232
+ # Gallery to render images
233
  gallery = gr.Gallery(label="Images", elem_id="gallery", columns=3, height="auto")
234
 
235
+ page_info = gr.Markdown()
236
+
237
+ # Navigation buttons
238
  with gr.Row():
239
  prev_btn = gr.Button("⬅ Previous")
240
  next_btn = gr.Button("Next ➡")
241
 
242
+ def update_gallery(images, page, page_size):
243
+ subset, page = render_page(current_date.value, page, page_size)
244
+ page_info.update(f"Page {page+1} / {math.ceil(len(images_state.value)/page_size)}")
245
+ return subset, page
246
+
247
  prev_btn.click(prev_page, [current_date, page_state, page_size], [gallery, page_state])
248
  next_btn.click(next_page, [current_date, page_state, page_size], [gallery, page_state])
249
 
 
255
 
256
  gr.HTML("<div id='ohamlab-footer'>© OhamLab Copyright</div>")
257
 
258
+ demo.launch()