Spaces:
Running
Running
import gradio as gr | |
import os | |
UPLOAD_DIR = "uploads" | |
os.makedirs(UPLOAD_DIR, exist_ok=True) | |
def save_file(file): | |
if file is None: | |
return "Please upload a file." | |
# file.name is the uploaded filename | |
# file is a NamedString -> use file to read content directly | |
file_path = os.path.join(UPLOAD_DIR, file.name) | |
with open(file_path, "wb") as f: | |
f.write(file) # just write the bytes | |
return f"✅ File `{file.name}` uploaded successfully!" | |
with gr.Blocks() as demo: | |
gr.Markdown("## 🖼️ Anonymous Image Upload\nDrop an image file below. No login required.") | |
with gr.Row(): | |
uploader = gr.File(label="Upload Image", file_types=[".png", ".jpg", ".jpeg"]) | |
output = gr.Textbox(label="Status") | |
uploader.change(fn=save_file, inputs=uploader, outputs=output) | |
demo.launch() | |