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("""
Upload â Rename â Download