Spaces:
Paused
Paused
| 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 | |
| ) |