From 1c98b1c739efa010802a418a931475736e4e9231 Mon Sep 17 00:00:00 2001 From: Yxjiang <2237303+yxjiang@users.noreply.github.com> Date: Wed, 6 Aug 2025 07:11:08 -0700 Subject: [PATCH 1/2] Cleanup setup env files --- examples/media-gen/README.md | 41 +++--------- examples/media-gen/setup.py | 108 +++++++++++++++++++++++++++++++ examples/media-gen/setup_env.bat | 40 ------------ examples/media-gen/setup_env.py | 46 ------------- examples/media-gen/setup_env.sh | 42 ------------ 5 files changed, 118 insertions(+), 159 deletions(-) create mode 100644 examples/media-gen/setup.py delete mode 100644 examples/media-gen/setup_env.bat delete mode 100644 examples/media-gen/setup_env.py delete mode 100755 examples/media-gen/setup_env.sh diff --git a/examples/media-gen/README.md b/examples/media-gen/README.md index ee627f1..d384668 100644 --- a/examples/media-gen/README.md +++ b/examples/media-gen/README.md @@ -18,47 +18,28 @@ This package provides: ### Quick Setup -**Linux/macOS:** +**Linux (recommended):** ```bash -./setup_env.sh -``` - -**Windows:** -```cmd -setup_env.bat +python setup.py ``` **Manual Setup:** ```bash python3 -m venv venv -source venv/bin/activate # On Windows: venv\Scripts\activate.bat +source venv/bin/activate pip install -r requirements.txt +cp env.example .env ``` ### Environment Configuration -**Option 1: Use the setup script (recommended):** -```bash -python setup_env.py -``` - -**Option 2: Manual setup:** -1. **Copy the environment template:** - ```bash - cp env.example .env - ``` - -2. **Edit `.env` file with your API keys:** - ```bash - # Edit .env file with your actual API keys - nano .env # or use your preferred editor - ``` +The setup script automatically creates a `.env` file from the template. You'll need to edit it with your actual API keys: -3. **Required API keys:** - - `OPENAI_API_KEY`: For DALL-E image generation - - `REPLICATE_API_TOKEN`: For various AI models +**Required API keys:** +- `OPENAI_API_KEY`: For DALL-E image generation +- `REPLICATE_API_TOKEN`: For various AI models - **Note:** The `.env` file is automatically ignored by git to keep your keys secure. +**Note:** The `.env` file is automatically ignored by git to keep your keys secure. ## File Structure @@ -73,11 +54,9 @@ media-gen/ │ └── test_dummy_media_gen.py # Comprehensive tests ├── env.example # Environment variables template -├── setup_env.py # Environment setup script +├── setup.py # Unified setup script (all platforms) ├── example_usage.py # Usage examples ├── requirements.txt # Dependencies -├── setup_env.sh # Linux/macOS setup script -├── setup_env.bat # Windows setup script ├── __init__.py # Main package exports └── README.md # This file ``` diff --git a/examples/media-gen/setup.py b/examples/media-gen/setup.py new file mode 100644 index 0000000..2d2199b --- /dev/null +++ b/examples/media-gen/setup.py @@ -0,0 +1,108 @@ +#!/usr/bin/env python3 +""" +Setup script for Media Generation Example (Linux). +Handles virtual environment creation and environment configuration. +""" +import sys +import shutil +import subprocess +from pathlib import Path + + +def run_command( + cmd: list[str], check: bool = True +) -> subprocess.CompletedProcess: + """Run a command and return the result.""" + print(f"Running: {' '.join(cmd)}") + return subprocess.run( + cmd, check=check, capture_output=True, text=True + ) + + +def check_python_version() -> None: + """Check if Python version is compatible.""" + if sys.version_info < (3, 10): + print("Error: Python 3.10+ is required") + sys.exit(1) + version = f"{sys.version_info.major}.{sys.version_info.minor}" + print(f"✓ Python {version} detected") + + +def create_virtual_environment() -> None: + """Create virtual environment.""" + venv_path = Path("venv") + + if venv_path.exists(): + print("✓ Virtual environment already exists") + response = input("Do you want to recreate it? (y/N): ").lower() + if response == 'y': + shutil.rmtree(venv_path) + else: + return + + print("Creating virtual environment...") + run_command([sys.executable, "-m", "venv", "venv"]) + print("✓ Virtual environment created") + + +def install_requirements() -> None: + """Install requirements in the virtual environment.""" + pip_path = "venv/bin/pip" + + print("Upgrading pip...") + run_command([pip_path, "install", "--upgrade", "pip"]) + + print("Installing requirements...") + run_command([pip_path, "install", "-r", "requirements.txt"]) + print("✓ Requirements installed") + + +def setup_environment_file() -> None: + """Set up the .env file from template.""" + env_file = Path(".env") + example_file = Path("env.example") + + if env_file.exists(): + print("✓ .env file already exists") + response = input("Do you want to overwrite it? (y/N): ").lower() + if response != 'y': + return + + if example_file.exists(): + shutil.copy(example_file, env_file) + print("✓ Created .env file from template") + else: + print("✗ env.example file not found") + return + + +def main() -> None: + """Main setup function.""" + print("Media Generation Example Setup") + print("=" * 40) + + # Check Python version + check_python_version() + + # Create virtual environment + create_virtual_environment() + + # Install requirements + install_requirements() + + # Setup environment file + setup_environment_file() + + print("\n✅ Setup complete!") + print("\nNext steps:") + print("1. Edit the .env file with your actual API keys") + print("2. Activate the virtual environment:") + print(" source venv/bin/activate") + print("3. Run 'python example_usage.py' to test the setup") + print("\nRequired API keys:") + print("- OPENAI_API_KEY: For DALL-E image generation") + print("- REPLICATE_API_TOKEN: For various AI models") + + +if __name__ == "__main__": + main() \ No newline at end of file diff --git a/examples/media-gen/setup_env.bat b/examples/media-gen/setup_env.bat deleted file mode 100644 index f0458ae..0000000 --- a/examples/media-gen/setup_env.bat +++ /dev/null @@ -1,40 +0,0 @@ -@echo off -REM Setup script for Media Generation Example (Windows) -REM This script creates a virtual environment and installs dependencies - -echo Setting up Media Generation Example environment... - -REM Check if Python 3 is available -python --version >nul 2>&1 -if errorlevel 1 ( - echo Error: Python 3 is required but not installed. - exit /b 1 -) - -REM Create virtual environment -echo Creating virtual environment... -python -m venv venv - -REM Activate virtual environment -echo Activating virtual environment... -call venv\Scripts\activate.bat - -REM Upgrade pip -echo Upgrading pip... -python -m pip install --upgrade pip - -REM Install requirements -echo Installing requirements... -pip install -r requirements.txt - -echo. -echo ✅ Environment setup complete! -echo. -echo To activate the environment in the future: -echo venv\Scripts\activate.bat -echo. -echo To run the example: -echo python example_usage.py -echo. -echo To run tests: -echo cd tests ^&^& python test_dummy_media_gen.py \ No newline at end of file diff --git a/examples/media-gen/setup_env.py b/examples/media-gen/setup_env.py deleted file mode 100644 index 2fdae48..0000000 --- a/examples/media-gen/setup_env.py +++ /dev/null @@ -1,46 +0,0 @@ -#!/usr/bin/env python3 -""" -Setup script for media generation tools environment. - -This script helps users set up their .env file with the required API keys. -""" -import shutil -from pathlib import Path - - -def setup_environment(): - """Set up the environment configuration.""" - print("Media Generation Tools Environment Setup") - print("=" * 40) - - # Check if .env already exists - env_file = Path(".env") - example_file = Path("env.example") - - if env_file.exists(): - print("✓ .env file already exists") - response = input("Do you want to overwrite it? (y/N): ").lower() - if response != 'y': - print("Setup cancelled.") - return - - # Copy example file to .env - if example_file.exists(): - shutil.copy(example_file, env_file) - print("✓ Created .env file from template") - else: - print("✗ env.example file not found") - return - - print("\nNext steps:") - print("1. Edit the .env file with your actual API keys") - print("2. Run 'python example_usage.py' to test the setup") - print("\nRequired API keys:") - print("- OPENAI_API_KEY: For DALL-E image generation") - print("- REPLICATE_API_TOKEN: For various AI models") - - print("\n✓ Environment setup completed!") - - -if __name__ == "__main__": - setup_environment() \ No newline at end of file diff --git a/examples/media-gen/setup_env.sh b/examples/media-gen/setup_env.sh deleted file mode 100755 index d04a227..0000000 --- a/examples/media-gen/setup_env.sh +++ /dev/null @@ -1,42 +0,0 @@ -#!/bin/bash - -# Setup script for Media Generation Example -# This script creates a virtual environment and installs dependencies - -set -e # Exit on any error - -echo "Setting up Media Generation Example environment..." - -# Check if Python 3 is available -if ! command -v python3 &> /dev/null; then - echo "Error: Python 3 is required but not installed." - exit 1 -fi - -# Create virtual environment -echo "Creating virtual environment..." -python3 -m venv venv - -# Activate virtual environment -echo "Activating virtual environment..." -source venv/bin/activate - -# Upgrade pip -echo "Upgrading pip..." -pip install --upgrade pip - -# Install requirements -echo "Installing requirements..." -pip install -r requirements.txt - -echo "" -echo "✅ Environment setup complete!" -echo "" -echo "To activate the environment in the future:" -echo " source venv/bin/activate" -echo "" -echo "To run the example:" -echo " python example_usage.py" -echo "" -echo "To run tests:" -echo " cd tests && python test_dummy_media_gen.py" \ No newline at end of file From 94b4a88bfbc50252282a7f0b605ecc3de1793d0c Mon Sep 17 00:00:00 2001 From: Yxjiang <2237303+yxjiang@users.noreply.github.com> Date: Wed, 6 Aug 2025 07:16:46 -0700 Subject: [PATCH 2/2] update by isort --- examples/media-gen/setup.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/examples/media-gen/setup.py b/examples/media-gen/setup.py index 2d2199b..3a68e8c 100644 --- a/examples/media-gen/setup.py +++ b/examples/media-gen/setup.py @@ -3,9 +3,10 @@ Setup script for Media Generation Example (Linux). Handles virtual environment creation and environment configuration. """ +import subprocess import sys + import shutil -import subprocess from pathlib import Path