Update app.py
Browse files
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 |
-
|
| 14 |
-
|
| 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 |
-
#
|
|
|
|
| 26 |
|
| 27 |
-
progress(0.
|
| 28 |
-
#
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
|
|
|
| 32 |
|
| 33 |
# --- 2. 访问网页并等待输入框加载 ---
|
| 34 |
-
progress(0.
|
| 35 |
driver.get("https://sai.coludai.cn/")
|
| 36 |
|
| 37 |
wait = WebDriverWait(driver, 20)
|
| 38 |
textarea_selector = 'textarea[placeholder="随时与未来对话,探索无限可能...."]'
|
| 39 |
-
progress(0.
|
| 40 |
textarea = wait.until(EC.presence_of_element_located((By.CSS_SELECTOR, textarea_selector)))
|
| 41 |
|
| 42 |
# --- 3. 输入问题并发送 ---
|
| 43 |
-
progress(0.
|
| 44 |
textarea.send_keys(prompt_text)
|
| 45 |
textarea.send_keys(Keys.RETURN)
|
| 46 |
|
| 47 |
# --- 4. 等待并捕获 AI 回复 ---
|
| 48 |
-
progress(0.
|
| 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 的回复")
|