Update app.py
Browse files
app.py
CHANGED
|
@@ -1,5 +1,12 @@
|
|
| 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
|
|
@@ -7,47 +14,70 @@ 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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
"""
|
| 12 |
-
|
| 13 |
-
|
| 14 |
"""
|
| 15 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
try:
|
| 17 |
-
# --- 1. 设置和初始化浏览器 ---
|
| 18 |
-
progress(0, desc="正在设置浏览器选项...")
|
| 19 |
-
options = webdriver.ChromeOptions()
|
| 20 |
-
options.add_argument("--headless")
|
| 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)
|
|
@@ -60,36 +90,90 @@ def chat_with_sai_automation(prompt_text: str, progress=gr.Progress(track_tqdm=T
|
|
| 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
|
| 65 |
-
|
| 66 |
-
|
| 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 |
-
|
|
|
|
| 78 |
finally:
|
| 79 |
-
if
|
| 80 |
driver.quit()
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import time
|
| 2 |
+
import json
|
| 3 |
+
import uuid
|
| 4 |
+
from fastapi import FastAPI, Request, HTTPException, Depends
|
| 5 |
+
from fastapi.security import HTTPBearer, HTTPAuthorizationCredentials
|
| 6 |
+
from starlette.responses import StreamingResponse
|
| 7 |
+
from pydantic import BaseModel
|
| 8 |
+
from typing import List, Optional
|
| 9 |
+
|
| 10 |
from selenium import webdriver
|
| 11 |
from selenium.webdriver.common.by import By
|
| 12 |
from selenium.webdriver.common.keys import Keys
|
|
|
|
| 14 |
from selenium.webdriver.support.ui import WebDriverWait
|
| 15 |
from selenium.webdriver.support import expected_conditions as EC
|
| 16 |
|
| 17 |
+
# --- 1. FastAPI 应用和认证设置 ---
|
| 18 |
+
|
| 19 |
+
app = FastAPI(
|
| 20 |
+
title="SAI-ChatBot OpenAI-Compatible API",
|
| 21 |
+
description="使用 Selenium 自动化在后台与 SAI-ChatBot 交互,并以 OpenAI API 格式返回结果。",
|
| 22 |
+
version="1.0.0"
|
| 23 |
+
)
|
| 24 |
+
|
| 25 |
+
# 一个简单的认证方案:只需要提供任何 Bearer Token 即可
|
| 26 |
+
auth_scheme = HTTPBearer()
|
| 27 |
+
|
| 28 |
+
def api_key_auth(credentials: HTTPAuthorizationCredentials = Depends(auth_scheme)):
|
| 29 |
+
"""
|
| 30 |
+
我们不校验 key 的内容,只检查是否存在 Authorization header。
|
| 31 |
+
这满足了“key填什么都过”的要求。
|
| 32 |
+
"""
|
| 33 |
+
if not credentials:
|
| 34 |
+
raise HTTPException(
|
| 35 |
+
status_code=401,
|
| 36 |
+
detail="Not authenticated",
|
| 37 |
+
headers={"WWW-Authenticate": "Bearer"},
|
| 38 |
+
)
|
| 39 |
+
# 这里可以添加日志,记录传入的 token,但我们不验证它
|
| 40 |
+
# print(f"Received token: {credentials.scheme} {credentials.token}")
|
| 41 |
+
return credentials.token
|
| 42 |
+
|
| 43 |
+
# --- 2. OpenAI 格式的数据模型 (使用 Pydantic) ---
|
| 44 |
+
|
| 45 |
+
class ChatMessage(BaseModel):
|
| 46 |
+
role: str
|
| 47 |
+
content: str
|
| 48 |
+
|
| 49 |
+
class ChatCompletionRequest(BaseModel):
|
| 50 |
+
model: str # 我们会忽略这个字段,但为了兼容性保留它
|
| 51 |
+
messages: List[ChatMessage]
|
| 52 |
+
stream: Optional[bool] = False
|
| 53 |
+
|
| 54 |
+
# --- 3. Selenium 自动化核心函数 ---
|
| 55 |
+
|
| 56 |
+
def get_sai_response(prompt_text: str):
|
| 57 |
"""
|
| 58 |
+
这是一个生成器函数,它会逐步返回(yield)AI的回复。
|
| 59 |
+
这使得我们可以实现流式API。
|
| 60 |
"""
|
| 61 |
+
options = webdriver.ChromeOptions()
|
| 62 |
+
options.add_argument("--headless")
|
| 63 |
+
options.add_argument("--no-sandbox")
|
| 64 |
+
options.add_argument("--disable-dev-shm-usage")
|
| 65 |
+
options.add_argument("--disable-gpu")
|
| 66 |
+
options.binary_location = "/usr/bin/chromium"
|
| 67 |
+
|
| 68 |
+
service = ChromeService(executable_path='/usr/bin/chromedriver')
|
| 69 |
+
driver = None
|
| 70 |
try:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 71 |
driver = webdriver.Chrome(service=service, options=options)
|
|
|
|
|
|
|
|
|
|
| 72 |
driver.get("https://sai.coludai.cn/")
|
| 73 |
|
| 74 |
wait = WebDriverWait(driver, 20)
|
| 75 |
textarea_selector = 'textarea[placeholder="随时与未来对话,探索无限可能...."]'
|
|
|
|
| 76 |
textarea = wait.until(EC.presence_of_element_located((By.CSS_SELECTOR, textarea_selector)))
|
| 77 |
|
|
|
|
|
|
|
| 78 |
textarea.send_keys(prompt_text)
|
| 79 |
textarea.send_keys(Keys.RETURN)
|
| 80 |
|
|
|
|
|
|
|
|
|
|
| 81 |
last_assistant_selector = "(.//div[@class='message-item' and @type='assistant'])[last()]"
|
| 82 |
wait.until(EC.presence_of_element_located((By.XPATH, last_assistant_selector)))
|
| 83 |
last_response_element = driver.find_element(By.XPATH, last_assistant_selector)
|
|
|
|
| 90 |
try:
|
| 91 |
markdown_body = last_response_element.find_element(By.CSS_SELECTOR, '.markdown-body')
|
| 92 |
current_text = markdown_body.text
|
| 93 |
+
|
| 94 |
+
if current_text != previous_text:
|
| 95 |
+
new_text_chunk = current_text[len(previous_text):]
|
| 96 |
+
yield new_text_chunk
|
|
|
|
| 97 |
previous_text = current_text
|
| 98 |
+
|
| 99 |
+
# 检查是否已经有一段时间没有新内容了,可以认为结束
|
| 100 |
+
if current_text == previous_text and current_text != "":
|
| 101 |
+
# 再稍微等待一下,以防万一
|
| 102 |
time.sleep(1.5)
|
| 103 |
+
final_text_check = markdown_body.text
|
| 104 |
+
if final_text_check == previous_text:
|
| 105 |
+
break # 确认结束,退出循环
|
| 106 |
+
|
| 107 |
+
time.sleep(0.5)
|
| 108 |
+
|
| 109 |
except Exception:
|
| 110 |
time.sleep(0.5)
|
|
|
|
|
|
|
|
|
|
| 111 |
|
| 112 |
except Exception as e:
|
| 113 |
+
error_message = f"自动化过程中发生错误: {e}"
|
| 114 |
+
yield error_message
|
| 115 |
finally:
|
| 116 |
+
if driver:
|
| 117 |
driver.quit()
|
| 118 |
+
|
| 119 |
+
# --- 4. API 端点定义 ---
|
| 120 |
+
|
| 121 |
+
@app.post("/v1/chat/completions")
|
| 122 |
+
async def chat_completions(request: ChatCompletionRequest, token: str = Depends(api_key_auth)):
|
| 123 |
+
# 从消息列表中提取最后一个用户的提问
|
| 124 |
+
last_user_message = next((msg.content for msg in reversed(request.messages) if msg.role == 'user'), None)
|
| 125 |
+
|
| 126 |
+
if not last_user_message:
|
| 127 |
+
raise HTTPException(status_code=400, detail="No user message found")
|
| 128 |
+
|
| 129 |
+
response_id = f"chatcmpl-{uuid.uuid4()}"
|
| 130 |
+
created_timestamp = int(time.time())
|
| 131 |
+
|
| 132 |
+
# --- 流式响应 (Streaming Response) ---
|
| 133 |
+
if request.stream:
|
| 134 |
+
async def stream_generator():
|
| 135 |
+
for chunk in get_sai_response(last_user_message):
|
| 136 |
+
if not chunk:
|
| 137 |
+
continue
|
| 138 |
+
# 构造符合 OpenAI SSE 格式的 JSON
|
| 139 |
+
response_chunk = {
|
| 140 |
+
"id": response_id,
|
| 141 |
+
"object": "chat.completion.chunk",
|
| 142 |
+
"created": created_timestamp,
|
| 143 |
+
"model": "sai-chatbot-l6",
|
| 144 |
+
"choices": [{
|
| 145 |
+
"index": 0,
|
| 146 |
+
"delta": {"content": chunk},
|
| 147 |
+
"finish_reason": None
|
| 148 |
+
}]
|
| 149 |
+
}
|
| 150 |
+
yield f"data: {json.dumps(response_chunk)}\n\n"
|
| 151 |
+
# 发送结束标志
|
| 152 |
+
yield f"data: [DONE]\n\n"
|
| 153 |
+
|
| 154 |
+
return StreamingResponse(stream_generator(), media_type="text/event-stream")
|
| 155 |
+
|
| 156 |
+
# --- 非流式响应 (Non-Streaming Response) ---
|
| 157 |
+
else:
|
| 158 |
+
full_content_chunks = [chunk for chunk in get_sai_response(last_user_message)]
|
| 159 |
+
full_content = "".join(full_content_chunks)
|
| 160 |
+
|
| 161 |
+
return {
|
| 162 |
+
"id": response_id,
|
| 163 |
+
"object": "chat.completion",
|
| 164 |
+
"created": created_timestamp,
|
| 165 |
+
"model": "sai-chatbot-l6",
|
| 166 |
+
"choices": [{
|
| 167 |
+
"index": 0,
|
| 168 |
+
"message": {
|
| 169 |
+
"role": "assistant",
|
| 170 |
+
"content": full_content,
|
| 171 |
+
},
|
| 172 |
+
"finish_reason": "stop"
|
| 173 |
+
}],
|
| 174 |
+
"usage": {
|
| 175 |
+
"prompt_tokens": len(last_user_message), # 伪造的 token 计数
|
| 176 |
+
"completion_tokens": len(full_content),
|
| 177 |
+
"total_tokens": len(last_user_message) + len(full_content)
|
| 178 |
+
}
|
| 179 |
+
}
|