Spaces:
Paused
Paused
File size: 4,775 Bytes
3ec1324 b4d8ca0 3ec1324 b4d8ca0 3ec1324 b4d8ca0 3ec1324 b4d8ca0 3ec1324 b4d8ca0 3ec1324 b4d8ca0 3ec1324 b4d8ca0 3ec1324 b4d8ca0 3ec1324 b4d8ca0 3ec1324 b4d8ca0 3ec1324 b4d8ca0 3ec1324 b4d8ca0 3ec1324 b4d8ca0 3ec1324 b4d8ca0 3ec1324 b4d8ca0 3ec1324 b4d8ca0 3ec1324 b4d8ca0 3ec1324 b4d8ca0 3ec1324 b4d8ca0 3ec1324 b4d8ca0 3ec1324 b4d8ca0 3ec1324 b4d8ca0 3ec1324 b4d8ca0 3ec1324 b4d8ca0 3ec1324 b4d8ca0 3ec1324 b4d8ca0 3ec1324 b4d8ca0 3ec1324 b4d8ca0 3ec1324 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 | import gradio as gr
import os
import tempfile
import shutil
import time
# Create uploads directory
UPLOADS_FOLDER = "uploads"
os.makedirs(UPLOADS_FOLDER, exist_ok=True)
def process_file(file_obj, custom_name):
"""
Simple file processor - returns file for download
No zip, no passwords, just clean file handling
"""
if not file_obj:
return None, "❌ Please upload a file first"
try:
# Handle Gradio 6+ file object
if isinstance(file_obj, dict):
file_path = file_obj["path"]
original_name = file_obj["name"]
else:
file_path = file_obj.name
original_name = os.path.basename(file_path)
# Create temp directory for processed file
temp_dir = tempfile.mkdtemp()
# Determine filename
if custom_name and custom_name.strip():
# Use custom name but preserve extension
ext = os.path.splitext(original_name)[1]
base_name = custom_name.strip()
if not base_name.endswith(ext):
final_name = base_name + ext
else:
final_name = base_name
else:
final_name = original_name
# Copy file to temp location
final_path = os.path.join(temp_dir, final_name)
shutil.copy2(file_path, final_path)
return final_path, f"✅ Ready: {final_name}"
except Exception as e:
return None, f"❌ Error: {str(e)}"
def get_file_info(file_obj):
"""Display basic file information"""
if not file_obj:
return "📁 No file selected"
try:
if isinstance(file_obj, dict):
file_path = file_obj["path"]
file_name = file_obj["name"]
else:
file_path = file_obj.name
file_name = os.path.basename(file_path)
size = os.path.getsize(file_path)
modified = time.ctime(os.path.getmtime(file_path))
return f"""
📄 **{file_name}**
• Size: {size/1024:.1f} KB
• Type: {os.path.splitext(file_name)[1]}
• Modified: {modified}
"""
except:
return "📁 File selected"
# Simple CSS
css = """
.container { max-width: 600px; margin: auto; }
.title { text-align: center; margin-bottom: 20px; }
"""
# Build minimal interface
with gr.Blocks(title="Simple File Upload", theme=gr.themes.Soft(), css=css) as app:
gr.Markdown("""
<div class="title">
<h1>📁 Simple File Upload</h1>
<p>Upload → Rename → Download</p>
</div>
""")
with gr.Row():
with gr.Column():
# File input
file_input = gr.File(
label="📤 Upload File",
file_count="single",
file_types=None # Accept all files
)
# File info display
file_info = gr.Markdown("📁 No file selected")
# Custom filename
custom_name = gr.Textbox(
label="✏️ Custom Filename (optional)",
placeholder="my-file",
info="Name without extension - original extension preserved"
)
# Process button
process_btn = gr.Button("⚡ Prepare for Download", variant="primary", size="lg")
# Status
status = gr.Textbox(label="Status", interactive=False)
# Download output
download = gr.File(label="📥 Download File", interactive=False)
# Update file info on upload
file_input.change(
fn=get_file_info,
inputs=[file_input],
outputs=file_info
)
# Process file on button click
process_btn.click(
fn=process_file,
inputs=[file_input, custom_name],
outputs=[download, status]
)
# Quick tips
with gr.Accordion("ℹ️ Tips", open=False):
gr.Markdown("""
**How to use:**
1. Click **Upload File** to select a file
2. (Optional) Enter a custom filename
3. Click **Prepare for Download**
4. Download your file
**Features:**
- ✅ Single file upload/download
- ✅ Custom filename support
- ✅ Original extension preserved
- ✅ Clean, simple interface
- ✅ No zip, no passwords, no complexity
**Perfect for:**
- Preparing files for Suno
- Quick file renaming
- Simple file transfers
""")
# Launch
if __name__ == "__main__":
app.launch(
show_error=True,
share=False,
server_name="0.0.0.0" # For Hugging Face Spaces
) |