Ryanus commited on
Commit
9691ddd
·
verified ·
1 Parent(s): 0c0e871

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +16 -2
app.py CHANGED
@@ -1,6 +1,8 @@
1
  import gradio as gr
2
  import time
3
  import random
 
 
4
  from selenium import webdriver
5
  from selenium.webdriver.firefox.service import Service
6
  from selenium.webdriver.firefox.options import Options
@@ -21,11 +23,23 @@ def generate_traffic(url, views, stay_duration_min, stay_duration_max):
21
 
22
  # 配置 Firefox 无头模式
23
  options = Options()
24
- options.add_argument("--headless") # 必须使用无头模式
25
  options.add_argument("--disable-gpu")
26
  options.add_argument("--no-sandbox")
27
  options.add_argument("--disable-dev-shm-usage")
28
 
 
 
 
 
 
 
 
 
 
 
 
 
29
  # 使用 webdriver-manager 自动安装和管理 GeckoDriver
30
  try:
31
  service = Service(GeckoDriverManager().install())
@@ -52,7 +66,7 @@ with gr.Blocks(title="网站流量生成器") as demo:
52
 
53
  with gr.Row():
54
  with gr.Column():
55
- url_input = gr.Textbox(label="目标网址 (URL)", placeholder="https://example.com")
56
  views_input = gr.Number(label="访问次数", value=10, minimum=1, maximum=100, precision=0)
57
  stay_min_input = gr.Number(label="最小停留时间 (秒)", value=5, minimum=1, precision=0)
58
  stay_max_input = gr.Number(label="最大停留时间 (秒)", value=15, minimum=1, precision=0)
 
1
  import gradio as gr
2
  import time
3
  import random
4
+ import os
5
+ import shutil
6
  from selenium import webdriver
7
  from selenium.webdriver.firefox.service import Service
8
  from selenium.webdriver.firefox.options import Options
 
23
 
24
  # 配置 Firefox 无头模式
25
  options = Options()
26
+ options.add_argument("--headless")
27
  options.add_argument("--disable-gpu")
28
  options.add_argument("--no-sandbox")
29
  options.add_argument("--disable-dev-shm-usage")
30
 
31
+ # 🔧 关键修复:为 Hugging Face Spaces 指定 Firefox 二进制路径
32
+ # Space 中 Firefox 通常安装在 /usr/bin/firefox
33
+ firefox_binary_path = "/usr/bin/firefox"
34
+ if not os.path.exists(firefox_binary_path):
35
+ # 作为备选方案,尝试通过 which 命令查找
36
+ firefox_binary_path = shutil.which("firefox")
37
+
38
+ if not firefox_binary_path:
39
+ return "错误:在 Space 中未找到 Firefox 浏览器。请确保已在 apt.txt 中添加 'firefox'。"
40
+
41
+ options.binary_location = firefox_binary_path
42
+
43
  # 使用 webdriver-manager 自动安装和管理 GeckoDriver
44
  try:
45
  service = Service(GeckoDriverManager().install())
 
66
 
67
  with gr.Row():
68
  with gr.Column():
69
+ url_input = gr.Textbox(label="目标网址", placeholder="https://example.com")
70
  views_input = gr.Number(label="访问次数", value=10, minimum=1, maximum=100, precision=0)
71
  stay_min_input = gr.Number(label="最小停留时间 (秒)", value=5, minimum=1, precision=0)
72
  stay_max_input = gr.Number(label="最大停留时间 (秒)", value=15, minimum=1, precision=0)