ruilongli commited on
Commit
9592f2d
·
verified ·
1 Parent(s): 18a61d5

complete pipeline

Browse files
Files changed (1) hide show
  1. app.py +68 -17
app.py CHANGED
@@ -1,26 +1,77 @@
1
- import gradio as gr
2
- import os, shutil
3
 
4
- UPLOAD_DIR = "uploads" # permanent storage
5
- os.makedirs(UPLOAD_DIR, exist_ok=True)
 
 
 
 
6
 
7
- def save_file(temp_path: str): # temp_path is *already* a str
8
- if not temp_path:
 
 
 
 
 
9
  return "❌ No file received."
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
10
 
11
- filename = os.path.basename(temp_path)
12
- dest_path = os.path.join(UPLOAD_DIR, filename)
13
- shutil.copy(temp_path, dest_path) # copy from tmp → uploads/
14
- return f"✅ `{filename}` saved!"
 
 
 
 
 
 
 
 
 
 
 
15
 
 
16
  with gr.Blocks() as demo:
17
- gr.Markdown("## 🖼️ Anonymous image upload (no login required)")
18
- uploader = gr.File(
19
- label="Upload image",
20
- file_types=["image"], # or [".png", ".jpg", ".jpeg"]
21
- type="filepath" # <‑‑ CRUCIAL
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
22
  )
23
- status = gr.Textbox(label="Status")
24
- uploader.upload(save_file, uploader, status) # use .upload for clarity
25
 
26
  demo.queue().launch()
 
1
+ import gradio as gr, os, shutil, glob, datetime, getpass
2
+ from huggingface_hub import upload_file
3
 
4
+ # ---------- CONFIG -------------------------------------------------
5
+ DATASET_REPO = "your‑name/my_dataset" # change me
6
+ PENDING_DIR = "pending"
7
+ APPROVED_DIR = "approved"
8
+ ADMIN_KEY_ENV = os.getenv("ADMIN_KEY") # optional
9
+ HF_TOKEN = os.getenv("HF_TOKEN")
10
 
11
+ for d in (PENDING_DIR, APPROVED_DIR):
12
+ os.makedirs(d, exist_ok=True)
13
+ # -------------------------------------------------------------------
14
+
15
+ # -------- UPLOAD HANDLER (public) ----------------------------------
16
+ def handle_upload(tmp_path: str):
17
+ if not tmp_path:
18
  return "❌ No file received."
19
+ fname = os.path.basename(tmp_path)
20
+ dest = os.path.join(PENDING_DIR, fname)
21
+ shutil.copy(tmp_path, dest)
22
+ return f"✅ `{fname}` uploaded for review!"
23
+
24
+ # -------- LIST PENDING FILES --------------------------------------
25
+ def list_pending():
26
+ return sorted(glob.glob(f"{PENDING_DIR}/*"))
27
+
28
+ # -------- ADMIN: APPROVE & PUSH -----------------------------------
29
+ def approve_files(password, selected_files):
30
+ if ADMIN_KEY_ENV and password != ADMIN_KEY_ENV:
31
+ return "🔒 Wrong admin key!", None
32
+
33
+ if not selected_files:
34
+ return "⚠️ Nothing selected.", None
35
 
36
+ log = []
37
+ for file_path in selected_files:
38
+ filename = os.path.basename(file_path)
39
+ # 1) push to dataset repo
40
+ upload_file(
41
+ path_or_fileobj=file_path,
42
+ path_in_repo=f"images/{filename}",
43
+ repo_id=DATASET_REPO,
44
+ repo_type="dataset",
45
+ token=HF_TOKEN,
46
+ )
47
+ # 2) move to approved/
48
+ shutil.move(file_path, os.path.join(APPROVED_DIR, filename))
49
+ log.append(f"✅ {filename}")
50
+ return "\n".join(log), list_pending()
51
 
52
+ # -------------------- UI ------------------------------------------
53
  with gr.Blocks() as demo:
54
+ gr.Markdown("## 🖼️ Anonymous Image Upload")
55
+ with gr.Row():
56
+ up_comp = gr.File(type="filepath", file_types=["image"])
57
+ up_msg = gr.Textbox(label="Status")
58
+ up_comp.upload(handle_upload, up_comp, up_msg)
59
+
60
+ # ----- review panel -----
61
+ gr.Markdown("## 🔍 Admin review panel")
62
+ admin_pass = gr.Textbox(label="Admin key", type="password")
63
+ gallery = gr.Gallery(label="Pending images").style(grid=4)
64
+ approve_btn = gr.Button("Approve & publish")
65
+ review_log = gr.Textbox(label="Log / Result")
66
+
67
+ # initialise gallery
68
+ gallery.value = list_pending()
69
+
70
+ # approve action
71
+ approve_btn.click(
72
+ fn=approve_files,
73
+ inputs=[admin_pass, gallery],
74
+ outputs=[review_log, gallery],
75
  )
 
 
76
 
77
  demo.queue().launch()