autotrain-fixed / app.py
Wynn Watson
Complete authentication bypass - remove HuggingFace login requirement
4371c4b
#!/usr/bin/env python3
"""
AutoTrain solution with complete authentication bypass
"""
import os
import pwd
import getpass
import sys
# Fix environment variables FIRST
os.environ["OMP_NUM_THREADS"] = "1"
os.environ["MKL_NUM_THREADS"] = "1"
os.environ["NUMEXPR_NUM_THREADS"] = "1"
os.environ["OPENBLAS_NUM_THREADS"] = "1"
os.environ["HF_HOME"] = "/tmp/huggingface_cache"
# Disable authentication checks completely
os.environ["DISABLE_OAUTH"] = "1"
os.environ["IS_RUNNING_IN_SPACE"] = "false"
os.environ["AUTOTRAIN_USERNAME"] = "user"
os.environ["AUTOTRAIN_TOKEN"] = "fake_token"
print("��� Environment variables fixed!")
print(f"✅ OMP_NUM_THREADS = {os.environ.get('OMP_NUM_THREADS')}")
# Create cache and working directories
os.makedirs("/tmp/huggingface_cache", exist_ok=True)
os.makedirs("/tmp/autotrain", exist_ok=True)
# Set working directory for database
os.chdir("/tmp/autotrain")
# Monkey patch the getuser function to fix the permission error
def mock_getuser():
return "user"
getpass.getuser = mock_getuser
# Also patch pwd.getpwuid for the same issue
original_getpwuid = pwd.getpwuid
def mock_getpwuid(uid):
class MockPwdEntry:
def __init__(self):
self.pw_name = "user"
self.pw_uid = uid
self.pw_gid = uid
self.pw_dir = "/app"
self.pw_shell = "/bin/bash"
def __getitem__(self, index):
if index == 0:
return self.pw_name
elif index == 1:
return "x" # password placeholder
elif index == 2:
return self.pw_uid
elif index == 3:
return self.pw_gid
elif index == 4:
return "User" # gecos
elif index == 5:
return self.pw_dir
elif index == 6:
return self.pw_shell
else:
raise IndexError("list index out of range")
return MockPwdEntry()
pwd.getpwuid = mock_getpwuid
print("��� User permission patches applied!")
if __name__ == "__main__":
print("��� Starting AutoTrain...")
try:
import uvicorn
from fastapi import FastAPI, Request, Depends
from fastapi.responses import HTMLResponse, RedirectResponse
from fastapi.staticfiles import StaticFiles
from starlette.middleware.sessions import SessionMiddleware
# Import and patch autotrain modules BEFORE creating the app
import autotrain.app.ui_routes as ui_routes
# Completely bypass authentication
def mock_user_authentication(request: Request = None):
return ("user", [])
def mock_get_user_and_orgs(request: Request):
return ("user", [])
# Patch all authentication functions
ui_routes.user_authentication = mock_user_authentication
ui_routes.get_user_and_orgs = mock_get_user_and_orgs
# Override the load_index function completely
def bypass_load_index(request: Request):
# Set session data
request.session["user"] = "user"
request.session["orgs"] = []
# Return the main AutoTrain interface directly
return HTMLResponse(content="""
<!DOCTYPE html>
<html>
<head>
<title>AutoTrain Advanced</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<style>
body { font-family: Arial, sans-serif; margin: 40px; background: #f5f5f5; }
.container { max-width: 800px; margin: 0 auto; background: white; padding: 30px; border-radius: 10px; box-shadow: 0 2px 10px rgba(0,0,0,0.1); }
h1 { color: #333; text-align: center; margin-bottom: 30px; }
.status { padding: 15px; background: #e8f5e8; border: 1px solid #4caf50; border-radius: 5px; margin: 20px 0; }
.feature { background: #f8f9fa; padding: 20px; margin: 15px 0; border-radius: 8px; border-left: 4px solid #007bff; }
.feature h3 { margin-top: 0; color: #007bff; }
.btn { display: inline-block; padding: 12px 24px; background: #007bff; color: white; text-decoration: none; border-radius: 5px; margin: 10px 5px; }
.btn:hover { background: #0056b3; }
</style>
</head>
<body>
<div class="container">
<h1>��� AutoTrain Advanced</h1>
<div class="status">
<strong>✅ Status:</strong> AutoTrain is running successfully with authentication bypass enabled.
</div>
<div class="feature">
<h3>��� Train Models</h3>
<p>Train state-of-the-art machine learning models with your own data using AutoTrain's automated pipeline.</p>
<a href="/train" class="btn">Start Training</a>
</div>
<div class="feature">
<h3>��� Datasets</h3>
<p>Upload and manage your datasets for training. Supports various formats including CSV, JSON, and text files.</p>
<a href="/datasets" class="btn">Manage Datasets</a>
</div>
<div class="feature">
<h3>��� Projects</h3>
<p>View and manage your training projects, monitor progress, and download trained models.</p>
<a href="/projects" class="btn">View Projects</a>
</div>
<div class="feature">
<h3>⚙️ Settings</h3>
<p>Configure training parameters, model settings, and other preferences.</p>
<a href="/settings" class="btn">Settings</a>
</div>
<div style="text-align: center; margin-top: 30px; color: #666;">
<p>AutoTrain Advanced - Automated Machine Learning Platform</p>
<p>Running in bypass mode for Hugging Face Spaces</p>
</div>
</div>
</body>
</html>
""")
# Replace the load_index function
ui_routes.load_index = bypass_load_index
# Import the router after patching
from autotrain.app.ui_routes import ui_router
app = FastAPI(title="AutoTrain Advanced")
# Add session middleware
app.add_middleware(SessionMiddleware, secret_key="autotrain-secret-key")
# Mount static files
app.mount("/static", StaticFiles(directory="static"), name="static")
# Add a root redirect to bypass any login redirects
@app.get("/")
async def root(request: Request):
return bypass_load_index(request)
# Add the UI router
app.include_router(ui_router)
print("✅ AutoTrain app created successfully!")
uvicorn.run(app, host="0.0.0.0", port=7860, log_level="info")
except Exception as e:
print(f"❌ Error: {e}")
import traceback
traceback.print_exc()