sehsapneb commited on
Commit
5a4e6c4
·
verified ·
1 Parent(s): 4634821

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +17 -19
app.py CHANGED
@@ -4,14 +4,13 @@ from selenium import webdriver
4
  from selenium.webdriver.common.by import By
5
  from selenium.webdriver.common.keys import Keys
6
  from selenium.webdriver.chrome.service import Service as ChromeService
7
- # 不再需要 webdriver-manager 了
8
  from selenium.webdriver.support.ui import WebDriverWait
9
  from selenium.webdriver.support import expected_conditions as EC
10
 
11
  def chat_with_sai_automation(prompt_text: str, progress=gr.Progress(track_tqdm=True)) -> str:
12
  """
13
- 使用 Selenium 4.6+ 内置的 SeleniumManager,自动下载并管理匹配的
14
- Chrome for Testing ChromeDriver。
15
  """
16
  full_response = ""
17
  try:
@@ -22,47 +21,46 @@ def chat_with_sai_automation(prompt_text: str, progress=gr.Progress(track_tqdm=T
22
  options.add_argument("--no-sandbox")
23
  options.add_argument("--disable-dev-shm-usage")
24
  options.add_argument("--disable-gpu")
25
- # 我们不再需要指定 binary_location,让 SeleniumManager 自动处理
 
26
 
27
- progress(0.1, desc="SeleniumManager 正在自动下载匹配的浏览器和驱动...")
28
- # Selenium 4.6.0 及以上版本,不再需要 Service 对象或 webdriver-manager
29
- # 直接调用 webdriver.Chrome(options=options) 即可
30
- # SeleniumManager 会在后台自动完成所有下载和匹配工作
31
- driver = webdriver.Chrome(options=options)
 
32
 
33
  # --- 2. 访问网页并等待输入框加载 ---
34
- progress(0.3, desc=f"正在访问 sai.coludai.cn ...")
35
  driver.get("https://sai.coludai.cn/")
36
 
37
  wait = WebDriverWait(driver, 20)
38
  textarea_selector = 'textarea[placeholder="随时与未来对话,探索无限可能...."]'
39
- progress(0.4, desc="等待页面输入框加载...")
40
  textarea = wait.until(EC.presence_of_element_located((By.CSS_SELECTOR, textarea_selector)))
41
 
42
  # --- 3. 输入问题并发送 ---
43
- progress(0.5, desc=f"输入问题并发送...")
44
  textarea.send_keys(prompt_text)
45
  textarea.send_keys(Keys.RETURN)
46
 
47
  # --- 4. 等待并捕获 AI 回复 ---
48
- progress(0.6, desc="等待 AI 回复...")
49
 
50
  last_assistant_selector = "(.//div[@class='message-item' and @type='assistant'])[last()]"
51
  wait.until(EC.presence_of_element_located((By.XPATH, last_assistant_selector)))
52
  last_response_element = driver.find_element(By.XPATH, last_assistant_selector)
53
 
54
  previous_text = ""
55
- max_wait_time = 120 # 增加超时时间
56
  start_time = time.time()
57
 
58
  while time.time() - start_time < max_wait_time:
59
  try:
60
  markdown_body = last_response_element.find_element(By.CSS_SELECTOR, '.markdown-body')
61
  current_text = markdown_body.text
62
-
63
- # 更新进度条
64
- progress(0.6 + 0.4 * (len(current_text) / 500), desc=f"正在接收... (长度: {len(current_text)})")
65
-
66
  if current_text == previous_text and current_text != "":
67
  full_response = current_text
68
  break
@@ -86,7 +84,7 @@ def chat_with_sai_automation(prompt_text: str, progress=gr.Progress(track_tqdm=T
86
  # --- 使用 Gradio 创建 Web 界面 ---
87
  with gr.Blocks() as iface:
88
  gr.Markdown("# SAI-ChatBot 自动化工具")
89
- gr.Markdown("在后台使用 Selenium 自动与 SAI-ChatBot 交互。请耐心等待,首次运行可能需要一些时间来下载浏览器和驱动。")
90
  with gr.Row():
91
  inp = gr.Textbox(lines=5, label="你的问题", placeholder="在这里输入你想问 SAI-ChatBot 的问题...")
92
  out = gr.Textbox(lines=15, label="AI 的回复")
 
4
  from selenium.webdriver.common.by import By
5
  from selenium.webdriver.common.keys import Keys
6
  from selenium.webdriver.chrome.service import Service as ChromeService
 
7
  from selenium.webdriver.support.ui import WebDriverWait
8
  from selenium.webdriver.support import expected_conditions as EC
9
 
10
  def chat_with_sai_automation(prompt_text: str, progress=gr.Progress(track_tqdm=True)) -> str:
11
  """
12
+ 最终版:使用系统中通过 apt-get 安装的 chromium 和 chromedriver。
13
+ 这是在 Hugging Face Spaces 上最稳定、最可靠的方案。
14
  """
15
  full_response = ""
16
  try:
 
21
  options.add_argument("--no-sandbox")
22
  options.add_argument("--disable-dev-shm-usage")
23
  options.add_argument("--disable-gpu")
24
+ # 当通过 apt 安装时,chromium 通常在这里
25
+ options.binary_location = "/usr/bin/chromium"
26
 
27
+ progress(0.2, desc="正在指定系统驱动...")
28
+ # 直接指定由 apt 安装的 chromedriver 的路径
29
+ service = ChromeService(executable_path='/usr/bin/chromedriver')
30
+
31
+ progress(0.3, desc="正在启动浏览器...")
32
+ driver = webdriver.Chrome(service=service, options=options)
33
 
34
  # --- 2. 访问网页并等待输入框加载 ---
35
+ progress(0.4, desc="正在访问 sai.coludai.cn ...")
36
  driver.get("https://sai.coludai.cn/")
37
 
38
  wait = WebDriverWait(driver, 20)
39
  textarea_selector = 'textarea[placeholder="随时与未来对话,探索无限可能...."]'
40
+ progress(0.5, desc="等待页面输入框加载...")
41
  textarea = wait.until(EC.presence_of_element_located((By.CSS_SELECTOR, textarea_selector)))
42
 
43
  # --- 3. 输入问题并发送 ---
44
+ progress(0.6, desc="输入问题并发送...")
45
  textarea.send_keys(prompt_text)
46
  textarea.send_keys(Keys.RETURN)
47
 
48
  # --- 4. 等待并捕获 AI 回复 ---
49
+ progress(0.7, desc="等待 AI 回复...")
50
 
51
  last_assistant_selector = "(.//div[@class='message-item' and @type='assistant'])[last()]"
52
  wait.until(EC.presence_of_element_located((By.XPATH, last_assistant_selector)))
53
  last_response_element = driver.find_element(By.XPATH, last_assistant_selector)
54
 
55
  previous_text = ""
56
+ max_wait_time = 120
57
  start_time = time.time()
58
 
59
  while time.time() - start_time < max_wait_time:
60
  try:
61
  markdown_body = last_response_element.find_element(By.CSS_SELECTOR, '.markdown-body')
62
  current_text = markdown_body.text
63
+ progress(0.7 + 0.3 * (min(len(current_text), 500) / 500), desc=f"正在接收... (长度: {len(current_text)})")
 
 
 
64
  if current_text == previous_text and current_text != "":
65
  full_response = current_text
66
  break
 
84
  # --- 使用 Gradio 创建 Web 界面 ---
85
  with gr.Blocks() as iface:
86
  gr.Markdown("# SAI-ChatBot 自动化工具")
87
+ gr.Markdown("在后台使用 Selenium 自动与 SAI-ChatBot 交互。请耐心等待,首次运行可能需要一些时间来构建环境。")
88
  with gr.Row():
89
  inp = gr.Textbox(lines=5, label="你的问题", placeholder="在这里输入你想问 SAI-ChatBot 的问题...")
90
  out = gr.Textbox(lines=15, label="AI 的回复")