import os import shutil import subprocess import sys import importlib.util import gradio as gr # Configuration - USE DIFFERENT PATH REPO_DIR = "/tmp/private_repo" # Changed to /tmp which is writable GITHUB_REPO = os.getenv("GITHUB_REPO", "").strip() GITHUB_TOKEN = os.getenv("GITHUB_TOKEN", "").strip() def setup_github_clone(): """Setup GitHub clone with token authentication if available""" if GITHUB_TOKEN and GITHUB_REPO: # Replace URL with token authentication if "github.com" in GITHUB_REPO: auth_url = GITHUB_REPO.replace( "https://github.com/", f"https://{GITHUB_TOKEN}@github.com/" ) return auth_url return GITHUB_REPO def load_private_code(): """Load private code from GitHub repository""" try: print("๐Ÿš€ Starting private code loading process...") if not GITHUB_REPO: print("โŒ GITHUB_REPO environment variable not set") return False # Clean up existing repo if os.path.exists(REPO_DIR): print("๐Ÿงน Cleaning up existing repository...") shutil.rmtree(REPO_DIR) # Clone repository print(f"๐Ÿ“ฅ Cloning repository from: {GITHUB_REPO}") clone_url = setup_github_clone() clone_cmd = ["git", "clone", "--depth", "1", clone_url, REPO_DIR] result = subprocess.run( clone_cmd, capture_output=True, text=True, timeout=300 ) if result.returncode != 0: print(f"โŒ Git clone failed: {result.stderr}") return False print("โœ… Repository cloned successfully!") # Install requirements requirements_path = os.path.join(REPO_DIR, "requirements.txt") if os.path.exists(requirements_path): print("๐Ÿ“ฆ Installing dependencies from requirements.txt...") install_result = subprocess.run( [sys.executable, "-m", "pip", "install", "-r", requirements_path], capture_output=True, text=True ) if install_result.returncode == 0: print("โœ… Dependencies installed successfully!") else: print(f"โš ๏ธ Dependency installation had issues: {install_result.stderr}") # Add to Python path sys.path.insert(0, REPO_DIR) print("๐ŸŽ‰ Private code loaded successfully!") return True except subprocess.TimeoutExpired: print("โŒ Git clone timeout - repository might be too large or network issue") return False except Exception as e: print(f"โŒ Error loading private code: {str(e)}") return False def create_fallback_ui(): """Create a fallback UI when private repo fails to load""" with gr.Blocks( title="Tiko - Setup Required", theme=gr.themes.Soft(primary_hue="purple") ) as demo: gr.Markdown( """

๐Ÿš€ Tiko - TikTok Downloader

Premium TikTok Content Downloader

## โš ๏ธ Setup In Progress We're currently setting up your Tiko instance. This might be because: - ๐Ÿ”„ First-time setup is running - ๐Ÿ“ฅ Downloading latest updates - โš™๏ธ Installing dependencies ### ๐ŸŽฏ What's Happening? Tiko is cloning your private repository from GitHub and setting up the environment. **Repository:** `https://github.com/miftahganzz/Tiko` ### โณ Next Steps: 1. Wait a few moments 2. Refresh this page 3. The app should load automatically If you continue to see this message, check: - GitHub repository URL is correct - Repository is accessible - Enough storage space available **Need help?** Contact support. """ ) # Add refresh button refresh_btn = gr.Button("๐Ÿ”„ Refresh Page", size="lg") refresh_btn.click( fn=lambda: None, inputs=[], outputs=[] ) # Debug info with gr.Accordion("๐Ÿ” Technical Details", open=False): gr.Markdown(f""" **Environment Info:** - GITHUB_REPO: `{GITHUB_REPO or 'Not set'}` - GITHUB_TOKEN: `{'Set' if GITHUB_TOKEN else 'Not set'}` - Repo Directory: `{REPO_DIR}` - Python Path: `{sys.executable}` """) return demo def load_private_app(): """Load the main app from private repository""" try: private_app_path = os.path.join(REPO_DIR, "app.py") if not os.path.exists(private_app_path): raise FileNotFoundError(f"app.py not found in private repo at {private_app_path}") print(f"๐Ÿ“‚ Loading app from: {private_app_path}") # Dynamically import the private module spec = importlib.util.spec_from_file_location("tiko_private", private_app_path) private_module = importlib.util.module_from_spec(spec) spec.loader.exec_module(private_module) print("โœ… Private module imported successfully!") # Get the main app function if hasattr(private_module, "main_app"): print("๐ŸŽฏ Found main_app() function, launching...") return private_module.main_app() elif hasattr(private_module, "demo"): print("๐ŸŽฏ Found demo variable, launching...") return private_module.demo elif hasattr(private_module, "create_tiko_ui"): print("๐ŸŽฏ Found create_tiko_ui() function, launching...") return private_module.create_tiko_ui() else: # Try to find any function that returns a Gradio interface for attr_name in dir(private_module): attr = getattr(private_module, attr_name) if callable(attr) and not attr_name.startswith('_'): try: result = attr() if isinstance(result, (gr.Blocks, gr.Interface)): print(f"๐ŸŽฏ Found function {attr_name}, launching...") return result except: continue raise AttributeError("No valid Gradio app found") except Exception as e: print(f"โŒ Failed to load private app: {str(e)}") raise # Main execution flow print("=" * 50) print("๐Ÿš€ Tiko - TikTok Downloader Initializing...") print("=" * 50) print(f"๐Ÿ“ Repository: {GITHUB_REPO}") print(f"๐Ÿ“‚ Target Directory: {REPO_DIR}") # Try to load private code if load_private_code(): try: print("๐ŸŽ‰ Successfully loaded private code, launching Tiko...") demo = load_private_app() print("โœ… Tiko is ready to go!") except Exception as e: print(f"โŒ Failed to launch private app: {e}") print("๐Ÿ”„ Falling back to setup UI...") demo = create_fallback_ui() else: print("โŒ Could not load private repository") print("๐Ÿ”„ Launching setup UI...") demo = create_fallback_ui() # Launch the application if __name__ == "__main__": print("๐ŸŒ Starting Gradio server...") demo.launch( server_name="0.0.0.0", server_port=7860, share=True )