Tiko / app.py
Mxltic's picture
Update app.py
078695b verified
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(
"""
<div style="text-align: center;">
<h1>πŸš€ Tiko - TikTok Downloader</h1>
<h3>Premium TikTok Content Downloader</h3>
</div>
## ⚠️ 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
)