Spaces:
Sleeping
Sleeping
Upload 2 files
Browse filesFiles changed (2)
- Dockerfile+12-5
- app.py+13-7
12345678910116 unmodified lines181920212223242526272 unmodified lines3031323334353637383940414243FROM python:3.10-slim# Install system-level dependencies for browsersRUN apt-get update && apt-get install -y \wget \gnupg \ca-certificates \procps \libnss3 \libatk1.0-0 \libatk-bridge2.0-0 \6 unmodified lineslibgbm1 \libasound2 \libpangocairo-1.0-0 \--no-install-recommends \&& rm -rf /var/lib/apt/lists/*# Setup the mandatory Hugging Face user (UID 1000)RUN useradd -m -u 1000 userUSER userENV HOME=/home/user \2 unmodified linesWORKDIR $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 directoryRUN playwright install chromium# Copy your application codeCOPY --chown=user app.py .EXPOSE 78601234567891011126 unmodified lines1920212223242526272829303132333435362 unmodified lines394041424344454647484950FROM python:3.10-slim# Install system dependencies for Playwright ChromiumRUN apt-get update && apt-get install -y \wget \gnupg \ca-certificates \procps \# Required system libraries for Chromiumlibnss3 \libatk1.0-0 \libatk-bridge2.0-0 \6 unmodified lineslibgbm1 \libasound2 \libpangocairo-1.0-0 \# Adding missing X11 fixes and other common missing dependencieslibxfixes3 \libx11-6 \libxcb1 \libxcursor1 \libxi6 \libxtst6 \libxshmfence1 \--no-install-recommends \&& rm -rf /var/lib/apt/lists/*# Mandatory Hugging Face user setupRUN useradd -m -u 1000 userUSER userENV HOME=/home/user \2 unmodified linesWORKDIR $HOME/appCOPY --chown=user requirements.txt .RUN pip install --no-cache-dir -r requirements.txt# Install Playwright and ChromiumRUN playwright install chromiumCOPY --chown=user app.py .EXPOSE 7860
12 unmodified lines131415161718191 unmodified line2122232425262728294 unmodified lines3435363738394016 unmodified lines5758596061626364656612 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 logslogs.append(log_entry)if len(logs) > 50: logs.pop(0)1 unmodified lineadd_log(f"Starting keep-alive visit to: {URL}")try:with sync_playwright() as p:# Relying on default path for browser executablebrowser = 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")page = context.new_page()page.goto(URL, wait_until='networkidle', timeout=60000)4 unmodified linesscroll_y = i * 800page.evaluate(f"window.scrollTo(0, {scroll_y});")add_log(f"Scrolled to {scroll_y}px...")time.sleep(random.uniform(1.0, 2.5))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: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__":demo.launch(server_name="0.0.0.0", server_port=7860)12 unmodified lines131415161718191 unmodified line21222324252627282930313233344 unmodified lines3940414243444516 unmodified lines626364656667686970717212 unmodified linesdef add_log(message):timestamp = datetime.datetime.now().strftime('%H:%M:%S')log_entry = f"[{timestamp}] {message}"print(log_entry)logs.append(log_entry)if len(logs) > 50: logs.pop(0)1 unmodified lineadd_log(f"Starting keep-alive visit to: {URL}")try:with sync_playwright() as p:# Re-enabling explicit user-agent and simplified launchbrowser = 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 linesscroll_y = i * 800page.evaluate(f"window.scrollTo(0, {scroll_y});")add_log(f"Scrolled to {scroll_y}px...")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 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 warningdemo.launch(server_name="0.0.0.0", server_port=7860)
Dockerfile+12-5
app.py+13-7