#!/usr/bin/env python3 """ Working AutoTrain solution - bypasses user permission issues Fixed version to handle _TemplateResponse error """ 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 os.environ["DISABLE_OAUTH"] = "1" os.environ["IS_RUNNING_IN_SPACE"] = "false" 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 from fastapi.responses import HTMLResponse from starlette.middleware.sessions import SessionMiddleware from starlette.templating import Jinja2Templates import autotrain.app.ui_routes as ui_routes # Create a mock user authentication that returns the right format def mock_user_authentication(request: Request = None): # Return a tuple or list that has len() method return ("user", []) # (username, orgs) # Patch the authentication function before importing ui_router ui_routes.user_authentication = mock_user_authentication # Also patch the load_index function to handle the authentication properly original_load_index = ui_routes.load_index def patched_load_index(request: Request): try: # Set a mock user in the request session request.session["user"] = "user" request.session["orgs"] = [] # Call the original function return original_load_index(request) except Exception as e: print(f"ERROR in load_index: {e}") # Return a simple HTML response if there's an error return HTMLResponse(content=""" AutoTrain Advanced

AutoTrain Advanced

Welcome to AutoTrain! The application is starting up...

If you see this message, the server is running but there may be authentication issues.

""") ui_routes.load_index = patched_load_index 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") 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()