Update app.py
Browse files
app.py
CHANGED
|
@@ -4,56 +4,55 @@ 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 |
-
|
| 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) -> str:
|
| 12 |
"""
|
| 13 |
-
使用 Selenium
|
| 14 |
-
|
| 15 |
"""
|
| 16 |
-
# --- 1. 设置和初始化浏览器 (Hugging Face 环境) ---
|
| 17 |
-
options = webdriver.ChromeOptions()
|
| 18 |
-
options.add_argument("--headless") # 在服务器上,必须使用无头模式
|
| 19 |
-
options.add_argument("--no-sandbox")
|
| 20 |
-
options.add_argument("--disable-dev-shm-usage")
|
| 21 |
-
options.add_argument("--disable-gpu")
|
| 22 |
-
|
| 23 |
-
# 自动安装和管理 ChromeDriver
|
| 24 |
-
try:
|
| 25 |
-
service = ChromeService(ChromeDriverManager().install())
|
| 26 |
-
driver = webdriver.Chrome(service=service, options=options)
|
| 27 |
-
except Exception as e:
|
| 28 |
-
return f"初始化浏览器失败: {e}\n请检查 packages.txt 和 requirements.txt 是否配置正确。"
|
| 29 |
-
|
| 30 |
full_response = ""
|
| 31 |
try:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 32 |
# --- 2. 访问网页并等待输入框加载 ---
|
|
|
|
| 33 |
driver.get("https://sai.coludai.cn/")
|
| 34 |
|
| 35 |
wait = WebDriverWait(driver, 20)
|
| 36 |
textarea_selector = 'textarea[placeholder="随时与未来对话,探索无限可能...."]'
|
|
|
|
| 37 |
textarea = wait.until(EC.presence_of_element_located((By.CSS_SELECTOR, textarea_selector)))
|
| 38 |
|
| 39 |
# --- 3. 输入问题并发送 ---
|
|
|
|
| 40 |
textarea.send_keys(prompt_text)
|
| 41 |
textarea.send_keys(Keys.RETURN)
|
| 42 |
|
| 43 |
# --- 4. 等待并捕获 AI 回复 ---
|
| 44 |
-
|
| 45 |
|
| 46 |
last_assistant_selector = "(.//div[@class='message-item' and @type='assistant'])[last()]"
|
| 47 |
-
wait.until(EC.
|
| 48 |
-
(By.CSS_SELECTOR, ".message-item[type='assistant']"),
|
| 49 |
-
initial_assistant_count
|
| 50 |
-
))
|
| 51 |
-
|
| 52 |
last_response_element = driver.find_element(By.XPATH, last_assistant_selector)
|
| 53 |
|
| 54 |
previous_text = ""
|
| 55 |
-
#
|
| 56 |
-
max_wait_time = 60 # 秒
|
| 57 |
start_time = time.time()
|
| 58 |
|
| 59 |
while time.time() - start_time < max_wait_time:
|
|
@@ -61,6 +60,9 @@ def chat_with_sai_automation(prompt_text: str) -> str:
|
|
| 61 |
markdown_body = last_response_element.find_element(By.CSS_SELECTOR, '.markdown-body')
|
| 62 |
current_text = markdown_body.text
|
| 63 |
|
|
|
|
|
|
|
|
|
|
| 64 |
if current_text == previous_text and current_text != "":
|
| 65 |
full_response = current_text
|
| 66 |
break
|
|
@@ -74,21 +76,22 @@ def chat_with_sai_automation(prompt_text: str) -> str:
|
|
| 74 |
full_response = previous_text if previous_text else "超时或未能捕获到完整回复。"
|
| 75 |
|
| 76 |
except Exception as e:
|
| 77 |
-
full_response = f"自动化过程中发生错误: {e}"
|
| 78 |
finally:
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
return full_response
|
| 83 |
|
| 84 |
# --- 使用 Gradio 创建 Web 界面 ---
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
)
|
|
|
|
| 92 |
|
| 93 |
if __name__ == "__main__":
|
| 94 |
iface.launch()
|
|
|
|
| 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:
|
| 18 |
+
# --- 1. 设置和初始化浏览器 ---
|
| 19 |
+
progress(0, desc="正在设置浏览器选项...")
|
| 20 |
+
options = webdriver.ChromeOptions()
|
| 21 |
+
options.add_argument("--headless")
|
| 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:
|
|
|
|
| 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
|
|
|
|
| 76 |
full_response = previous_text if previous_text else "超时或未能捕获到完整回复。"
|
| 77 |
|
| 78 |
except Exception as e:
|
| 79 |
+
full_response = f"自动化过程中发生错误: {e}\n\n请检查 Hugging Face Space 的日志获取详细信息。"
|
| 80 |
finally:
|
| 81 |
+
if 'driver' in locals() and driver:
|
| 82 |
+
driver.quit()
|
| 83 |
+
|
| 84 |
return full_response
|
| 85 |
|
| 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 的回复")
|
| 93 |
+
btn = gr.Button("提交")
|
| 94 |
+
btn.click(fn=chat_with_sai_automation, inputs=inp, outputs=out)
|
| 95 |
|
| 96 |
if __name__ == "__main__":
|
| 97 |
iface.launch()
|