sehsapneb commited on
Commit
41ace18
·
verified ·
1 Parent(s): 63c7bef

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +94 -0
app.py ADDED
@@ -0,0 +1,94 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import time
3
+ 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
+ from webdriver_manager.chrome import ChromeDriverManager
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 自动化浏览器,与 SAI-ChatBot 进行交互。
14
+ 此函数专门为在 Hugging Face Spaces 的环境中运行而优化。
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
+ initial_assistant_count = len(driver.find_elements(By.CSS_SELECTOR, ".message-item[type='assistant']"))
45
+
46
+ last_assistant_selector = "(.//div[@class='message-item' and @type='assistant'])[last()]"
47
+ wait.until(EC.numberOfElementsToBeMoreThan(
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:
60
+ try:
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
67
+ else:
68
+ previous_text = current_text
69
+ time.sleep(1.5)
70
+ except Exception:
71
+ time.sleep(0.5)
72
+
73
+ if not full_response:
74
+ full_response = previous_text if previous_text else "超时或未能捕获到完整回复。"
75
+
76
+ except Exception as e:
77
+ full_response = f"自动化过程中发生错误: {e}"
78
+ finally:
79
+ # --- 5. 任务结束,关闭浏览器 ---
80
+ driver.quit()
81
+
82
+ return full_response
83
+
84
+ # --- 使用 Gradio 创建 Web 界面 ---
85
+ iface = gr.Interface(
86
+ fn=chat_with_sai_automation,
87
+ inputs=gr.Textbox(lines=5, label="你的问题", placeholder="在这里输入你想问 SAI-ChatBot 的问题..."),
88
+ outputs=gr.Textbox(lines=15, label="AI 的回复"),
89
+ title="SAI-ChatBot 自动化工具",
90
+ description="在后台使用 Selenium 驱动一个真实的 Chrome 浏览器来与 SAI-ChatBot 交互。请耐心等待,首次运行可能需要一些时间来构建环境。"
91
+ )
92
+
93
+ if __name__ == "__main__":
94
+ iface.launch()