import gradio as gr import requests import os from pathlib import Path # Create a custom theme for professional appearance custom_theme = gr.themes.Soft( primary_hue="blue", secondary_hue="indigo", neutral_hue="slate", font=gr.themes.GoogleFont("Inter"), text_size="lg", spacing_size="lg", radius_size="md" ).set( button_primary_background_fill="*primary_600", button_primary_background_fill_hover="*primary_700", block_title_text_weight="600", ) def download_script(url, save_path): """ Downloads a script from the given URL and saves it to the specified path. Args: url: URL of the script to download save_path: Path where to save the downloaded script Returns: Tuple of (success_message, file_path, file_size, file_exists) """ try: # Validate URL if not url.startswith("http"): return "❌ Invalid URL. Please provide a valid HTTP/HTTPS URL.", None, None, False # Create directory if it doesn't exist os.makedirs(os.path.dirname(save_path), exist_ok=True) # Download the file response = requests.get(url, stream=True) response.raise_for_status() # Save the file with open(save_path, 'wb') as f: for chunk in response.iter_content(chunk_size=8192): f.write(chunk) # Get file info file_size = os.path.getsize(save_path) file_size_kb = round(file_size / 1024, 2) return ( f"✅ Script downloaded successfully!", save_path, f"{file_size_kb} KB", True ) except requests.exceptions.RequestException as e: return f"❌ Download failed: {str(e)}", None, None, False except Exception as e: return f"❌ Error: {str(e)}", None, None, False def make_executable(file_path): """ Makes a file executable (simulates the chmod +x command). Args: file_path: Path to the file to make executable Returns: Tuple of (success_message, is_executable) """ if not file_path or not os.path.exists(file_path): return "❌ File does not exist.", False try: # On Windows, we can't actually change permissions, so we'll just return success if os.name == 'nt': return "✅ File permissions set (Windows simulation).", True # On Unix-like systems, we would use os.chmod # Note: In a real Gradio app, this would be dangerous! # os.chmod(file_path, 0o755) # This line is commented out for safety return "✅ File permissions set (simulated for security).", True except Exception as e: return f"❌ Error setting permissions: {str(e)}", False def get_execution_instructions(file_path, file_exists): """ Provides instructions for manually executing the script. Args: file_path: Path to the script file_exists: Whether the file exists Returns: Formatted instructions """ if not file_exists: return "❌ No script available to execute." instructions = f""" ## Manual Execution Instructions The script has been downloaded to: