ruilongli commited on
Commit
18a61d5
·
verified ·
1 Parent(s): 059e3c4

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +17 -15
app.py CHANGED
@@ -1,24 +1,26 @@
1
  import gradio as gr
2
- import os
3
- import shutil
4
 
5
- UPLOAD_DIR = "uploads"
6
  os.makedirs(UPLOAD_DIR, exist_ok=True)
7
 
8
-
9
- def save_file(file):
10
- if file is None:
11
  return "❌ No file received."
12
 
13
- dest_path = os.path.join(UPLOAD_DIR, file.name)
14
- shutil.copy(file.path, dest_path) # file.path is the temp file on disk
15
- return f"✅ File `{file.name}` uploaded successfully!"
 
16
 
17
  with gr.Blocks() as demo:
18
- gr.Markdown("## 🖼️ Anonymous Image Upload\nDrop an image file below. No login required.")
19
- with gr.Row():
20
- uploader = gr.File(label="Upload Image", file_types=[".png", ".jpg", ".jpeg"])
21
- output = gr.Textbox(label="Status")
22
- uploader.change(fn=save_file, inputs=uploader, outputs=output)
 
 
 
23
 
24
- demo.launch()
 
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()