datamk commited on
Commit
9e28b68
·
verified ·
1 Parent(s): 1511a2a

Upload 3 files

Browse files
Files changed (3) hide show
  1. Dockerfile +40 -0
  2. app.py +93 -0
  3. requirements.txt +3 -0
Dockerfile ADDED
@@ -0,0 +1,40 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ FROM python:3.10-slim
2
+
3
+ # Install system dependencies needed for Chrome and Selenium
4
+ RUN apt-get update && apt-get install -y \
5
+ wget \
6
+ gnupg \
7
+ unzip \
8
+ curl \
9
+ --no-install-recommends \
10
+ && rm -rf /var/lib/apt/lists/*
11
+
12
+ # Install Google Chrome Stable
13
+ RUN wget -q -O - https://dl-ssl.google.com/linux/linux_signing_key.pub | apt-key add - \
14
+ && sh -c 'echo "deb [arch=amd64] http://dl.google.com/linux/chrome/deb/ stable main" >> /etc/apt/sources.list.d/google-chrome.list' \
15
+ && apt-get update \
16
+ && apt-get install -y google-chrome-stable \
17
+ --no-install-recommends \
18
+ && rm -rf /var/lib/apt/lists/*
19
+
20
+ # Create user securely to run in HF Spaces instead of root
21
+ RUN useradd -m -u 1000 user
22
+ USER user
23
+ ENV HOME=/home/user \
24
+ PATH=/home/user/.local/bin:$PATH \
25
+ PYTHONUNBUFFERED=1
26
+
27
+ WORKDIR $HOME/app
28
+
29
+ # Install Python requirements
30
+ COPY --chown=user requirements.txt .
31
+ RUN pip install --no-cache-dir -r requirements.txt
32
+
33
+ # Copy our backend script into the container
34
+ COPY --chown=user app.py .
35
+
36
+ # HF Spaces requires the container to expose port 7860
37
+ EXPOSE 7860
38
+
39
+ # Start Gradio which will host our Background keep-alive thread
40
+ CMD ["python", "app.py"]
app.py ADDED
@@ -0,0 +1,93 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import threading
3
+ import time
4
+ import schedule
5
+ from selenium import webdriver
6
+ from selenium.webdriver.chrome.options import Options
7
+ import random
8
+ import datetime
9
+
10
+ URL = "https://streamer1-gvbgwaea2w2qmwgjey7ksg.streamlit.app/"
11
+ logs = ["Service started. Setting up Chrome driver..."]
12
+
13
+ def add_log(message):
14
+ timestamp = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')
15
+ logs.append(f"[{timestamp}] {message}")
16
+ # Keep the last 30 logs in memory to show on the UI
17
+ if len(logs) > 30:
18
+ logs.pop(0)
19
+
20
+ def visit_and_scroll():
21
+ add_log(f"Started keeping alive {URL}...")
22
+
23
+ options = Options()
24
+ options.add_argument("--headless=new")
25
+ # These flags are critically important for running Chrome inside a Docker container
26
+ options.add_argument("--no-sandbox")
27
+ options.add_argument("--disable-dev-shm-usage")
28
+ options.add_argument("--disable-gpu")
29
+ options.add_argument("--window-size=1920,1080")
30
+
31
+ driver = None
32
+ try:
33
+ driver = webdriver.Chrome(options=options)
34
+ driver.get(URL)
35
+ time.sleep(8)
36
+
37
+ total_height = driver.execute_script("return document.body.scrollHeight")
38
+ add_log("Scrolling down naturally...")
39
+ current_scroll = 0
40
+ while current_scroll < total_height:
41
+ scroll_step = random.randint(100, 500)
42
+ current_scroll += scroll_step
43
+ driver.execute_script(f"window.scrollTo(0, {current_scroll});")
44
+ time.sleep(random.uniform(0.3, 1.2))
45
+ new_height = driver.execute_script("return document.body.scrollHeight")
46
+ if new_height > total_height:
47
+ total_height = new_height
48
+
49
+ add_log("Scrolling up naturally...")
50
+ while current_scroll > 0:
51
+ scroll_step = random.randint(100, 500)
52
+ current_scroll -= scroll_step
53
+ if current_scroll < 0:
54
+ current_scroll = 0
55
+ driver.execute_script(f"window.scrollTo(0, {current_scroll});")
56
+ time.sleep(random.uniform(0.3, 1.2))
57
+
58
+ add_log("Done navigating! Closing background browser...")
59
+
60
+ except Exception as e:
61
+ add_log(f"Error occurred: {e}")
62
+ finally:
63
+ if driver:
64
+ time.sleep(2)
65
+ driver.quit()
66
+
67
+ def run_schedule():
68
+ # Initial run upon startup
69
+ visit_and_scroll()
70
+
71
+ # Schedule to run every 1 hour
72
+ schedule.every(1).hours.do(visit_and_scroll)
73
+ while True:
74
+ schedule.run_pending()
75
+ time.sleep(60)
76
+
77
+ # Start the keep-alive task in a background threading daemon
78
+ thread = threading.Thread(target=run_schedule, daemon=True)
79
+ thread.start()
80
+
81
+ def get_logs():
82
+ return "\n".join(logs)
83
+
84
+ # Create a minimal Gradio UI specifically to satisfy Hugging Face Spaces port mapping
85
+ with gr.Blocks() as demo:
86
+ gr.Markdown("# 🤖 Keep-Alive Bot Status Panel")
87
+ gr.Markdown(f"Currently maintaining a background connection to **{URL}** every 1 hour.")
88
+ output = gr.Textbox(label="Live System Logs", lines=15, value=get_logs(), interactive=False)
89
+ refresh_btn = gr.Button("Refresh Logs")
90
+ refresh_btn.click(fn=get_logs, outputs=output)
91
+
92
+ # Expose the port commonly used for HF Spaces deployments
93
+ demo.launch(server_name="0.0.0.0", server_port=7860)
requirements.txt ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ selenium>=4.10.0
2
+ schedule>=1.2.0
3
+ gradio>=4.0.0