Spaces:
Sleeping
Sleeping
Upload 2 files
Browse filesFiles changed (2)
- Dockerfile+12-5
- app.py+13-7
12334567891011126 unmodified lines192021222324252627282930313224333435362 unmodified lines394041334243443745464740484950FROM python:3.10-slim
# Install system-level dependencies for browsers# Install system dependencies for Playwright ChromiumRUN apt-get update && apt-get install -y \ wget \ gnupg \ ca-certificates \ procps \ # Required system libraries for Chromium libnss3 \ libatk1.0-0 \ libatk-bridge2.0-0 \6 unmodified lines libgbm1 \ libasound2 \ libpangocairo-1.0-0 \ # Adding missing X11 fixes and other common missing dependencies libxfixes3 \ libx11-6 \ libxcb1 \ libxcursor1 \ libxi6 \ libxtst6 \ libxshmfence1 \ --no-install-recommends \ && rm -rf /var/lib/apt/lists/*
# Setup the mandatory Hugging Face user (UID 1000)# Mandatory Hugging Face user setupRUN useradd -m -u 1000 userUSER userENV HOME=/home/user \2 unmodified lines
WORKDIR $HOME/app
# Only copy requirements first to cache layersCOPY --chown=user requirements.txt .RUN pip install --no-cache-dir -r requirements.txt
# Install Playwright browser to the default user home directory# Install Playwright and ChromiumRUN playwright install chromium
# Copy your application codeCOPY --chown=user app.py .
EXPOSE 786012 unmodified lines13141516161718191 unmodified line21222324252624252627282930313233344 unmodified lines394041374243444516 unmodified lines6263646061656667686970717212 unmodified linesdef add_log(message): timestamp = datetime.datetime.now().strftime('%H:%M:%S') log_entry = f"[{timestamp}] {message}" print(log_entry) # Prints to HF Build/Container logs print(log_entry) logs.append(log_entry) if len(logs) > 50: logs.pop(0)
1 unmodified line add_log(f"Starting keep-alive visit to: {URL}") try: with sync_playwright() as p: # Relying on default path for browser executable browser = p.chromium.launch(headless=True, args=['--no-sandbox', '--disable-dev-shm-usage']) context = browser.new_context(user_agent="Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36") # Re-enabling explicit user-agent and simplified launch browser = p.chromium.launch( headless=True, args=['--no-sandbox', '--disable-dev-shm-usage', '--disable-gpu'] ) context = browser.new_context( user_agent="Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36" ) page = context.new_page() page.goto(URL, wait_until='networkidle', timeout=60000)4 unmodified lines scroll_y = i * 800 page.evaluate(f"window.scrollTo(0, {scroll_y});") add_log(f"Scrolled to {scroll_y}px...") time.sleep(random.uniform(1.0, 2.5)) time.sleep(random.uniform(1.5, 3.0))
page.evaluate("window.scrollTo(0, 0);") add_log("Visit completed. Closing browser.")16 unmodified linesdef get_logs(): return "\n".join(reversed(logs))
# Gradio Interfacewith gr.Blocks(theme=gr.themes.Soft()) as demo:# Gradio Interface fix for v6 warningwith gr.Blocks() as demo: gr.Markdown("# 🚀 Cloud Stay-Alive Bot") status_box = gr.Textbox(label="Latest Logs (Top First)", value=get_logs, every=5, lines=15)
if __name__ == "__main__": # Move theme to launch to fix the Gradio 6 warning demo.launch(server_name="0.0.0.0", server_port=7860)Dockerfile+12-5
app.py+13-7