Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -10,6 +10,7 @@ from fastapi.responses import MarkdownResponse # 确保导入这行
|
|
| 10 |
from pydantic import BaseModel
|
| 11 |
from transformers import AutoTokenizer, AutoModelForSequenceClassification
|
| 12 |
import torch
|
|
|
|
| 13 |
|
| 14 |
# 【双重保障】设置 Hugging Face 缓存目录(与 Dockerfile 一致)
|
| 15 |
os.environ["TRANSFORMERS_CACHE"] = "/tmp/huggingface_cache"
|
|
@@ -194,18 +195,110 @@ async def health_check():
|
|
| 194 |
|
| 195 |
|
| 196 |
# ------------------- 新增:根路径(/)首页路由 -------------------
|
| 197 |
-
@app.get("/", response_class=
|
| 198 |
async def home_page():
|
| 199 |
-
"""
|
|
|
|
| 200 |
return f"""
|
| 201 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 202 |
|
| 203 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 204 |
|
| 205 |
-
|
| 206 |
-
|
| 207 |
-
|
| 208 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 209 |
|
| 210 |
|
| 211 |
|
|
|
|
| 10 |
from pydantic import BaseModel
|
| 11 |
from transformers import AutoTokenizer, AutoModelForSequenceClassification
|
| 12 |
import torch
|
| 13 |
+
from fastapi.responses import HTMLResponse # 替换为 HTMLResponse
|
| 14 |
|
| 15 |
# 【双重保障】设置 Hugging Face 缓存目录(与 Dockerfile 一致)
|
| 16 |
os.environ["TRANSFORMERS_CACHE"] = "/tmp/huggingface_cache"
|
|
|
|
| 195 |
|
| 196 |
|
| 197 |
# ------------------- 新增:根路径(/)首页路由 -------------------
|
| 198 |
+
@app.get("/", response_class=HTMLResponse, description="API 首页(含调用指南)")
|
| 199 |
async def home_page():
|
| 200 |
+
"""根路径首页:用 HTML 渲染,避免 MarkdownResponse 依赖问题"""
|
| 201 |
+
current_time = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
|
| 202 |
return f"""
|
| 203 |
+
<!DOCTYPE html>
|
| 204 |
+
<html lang="zh-CN">
|
| 205 |
+
<head>
|
| 206 |
+
<meta charset="UTF-8">
|
| 207 |
+
<title>Cross-Encoder 重排序 API(兼容 GPT 格式)</title>
|
| 208 |
+
<style>
|
| 209 |
+
body {{ font-family: Arial, sans-serif; max-width: 1200px; margin: 0 auto; padding: 20px; }}
|
| 210 |
+
h1 {{ color: #2c3e50; border-bottom: 2px solid #3498db; padding-bottom: 10px; }}
|
| 211 |
+
h2 {{ color: #34495e; margin-top: 30px; }}
|
| 212 |
+
pre {{ background: #f8f9fa; padding: 15px; border-radius: 5px; border: 1px solid #e9ecef; overflow-x: auto; }}
|
| 213 |
+
table {{ border-collapse: collapse; width: 100%; margin: 20px 0; }}
|
| 214 |
+
th, td {{ border: 1px solid #e9ecef; padding: 12px; text-align: left; }}
|
| 215 |
+
th {{ background-color: #f1f5f9; }}
|
| 216 |
+
.note {{ color: #6c757d; font-size: 0.9em; }}
|
| 217 |
+
</style>
|
| 218 |
+
</head>
|
| 219 |
+
<body>
|
| 220 |
+
<h1>Cross-Encoder 重排序 API(兼容 GPT 格式)</h1>
|
| 221 |
+
<p>基于 <code>cross-encoder/ms-marco-MiniLM-L-6-v2</code> 模型,提供文本相关性排序服务,支持 GPT 标准 API 调用格式。</p>
|
| 222 |
+
|
| 223 |
+
<h2>核心功能</h2>
|
| 224 |
+
<ul>
|
| 225 |
+
<li>输入「查询语句 + 候选文档列表」,返回按相关性降序排列的结果(含分数、排名)</li>
|
| 226 |
+
<li>兼容 OpenAI 风格 API 格式,可直接用 OpenAI 库调用</li>
|
| 227 |
+
<li>支持 API Key 认证,保障接口安全</li>
|
| 228 |
+
</ul>
|
| 229 |
+
|
| 230 |
+
<h2>接口地址</h2>
|
| 231 |
+
<h3>重排序接口(兼容 GPT)</h3>
|
| 232 |
+
<ul>
|
| 233 |
+
<li><strong>URL</strong>: <code>{app.root_path}/v1/chat/completions</code></li>
|
| 234 |
+
<li><strong>方法</strong>: <code>POST</code></li>
|
| 235 |
+
<li><strong>认证</strong>: 需在 Header 中添加 <code>Authorization: Bearer <你的 API Key></code>(API Key 在 Hugging Face Spaces 环境变量中配置)</li>
|
| 236 |
+
</ul>
|
| 237 |
+
|
| 238 |
+
<h2>调用示例(Python)</h2>
|
| 239 |
+
<pre><code>from openai import OpenAI
|
| 240 |
+
|
| 241 |
+
# 配置客户端(指向当前 Space 地址)
|
| 242 |
+
client = OpenAI(
|
| 243 |
+
api_key="your-api-key-here", # 替换为你的 API Key
|
| 244 |
+
base_url="https://<your-username>-<your-space-name>.hf.space/v1" # 替换为你的 Space URL
|
| 245 |
+
)
|
| 246 |
|
| 247 |
+
# 发送重排序请求
|
| 248 |
+
response = client.chat.completions.create(
|
| 249 |
+
model="cross-encoder/ms-marco-MiniLM-L-6-v2", # 固定模型名
|
| 250 |
+
messages=[
|
| 251 |
+
{{
|
| 252 |
+
"role": "user",
|
| 253 |
+
"content": "query: 什么是机器学习?; documents: 机器学习是AI的分支; Python是编程语言; 深度学习是机器学习的子集;"
|
| 254 |
+
}}
|
| 255 |
+
],
|
| 256 |
+
top_k=2 # 可选,返回 Top 2 高相关文档
|
| 257 |
+
)
|
| 258 |
|
| 259 |
+
# 打印结果
|
| 260 |
+
print(response.choices[0].message.content)</code></pre>
|
| 261 |
+
|
| 262 |
+
<h2>请求参数说明</h2>
|
| 263 |
+
<table>
|
| 264 |
+
<tr>
|
| 265 |
+
<th>参数</th>
|
| 266 |
+
<th>类型</th>
|
| 267 |
+
<th>说明</th>
|
| 268 |
+
</tr>
|
| 269 |
+
<tr>
|
| 270 |
+
<td><code>model</code></td>
|
| 271 |
+
<td>string</td>
|
| 272 |
+
<td>固定为 <code>cross-encoder/ms-marco-MiniLM-L-6-v2</code>,不可修改</td>
|
| 273 |
+
</tr>
|
| 274 |
+
<tr>
|
| 275 |
+
<td><code>messages</code></td>
|
| 276 |
+
<td>list</td>
|
| 277 |
+
<td>消息列表,最后一条必须是 <code>role: user</code> 的消息</td>
|
| 278 |
+
</tr>
|
| 279 |
+
<tr>
|
| 280 |
+
<td><code>messages[].content</code></td>
|
| 281 |
+
<td>string</td>
|
| 282 |
+
<td>格式:<code>query: [你的查询]; documents: [文档1]; [文档2]; ...</code>(文档用分号分隔)</td>
|
| 283 |
+
</tr>
|
| 284 |
+
<tr>
|
| 285 |
+
<td><code>top_k</code></td>
|
| 286 |
+
<td>int</td>
|
| 287 |
+
<td>可选,默认返回 Top 3 文档,范围 1~20</td>
|
| 288 |
+
</tr>
|
| 289 |
+
</table>
|
| 290 |
+
|
| 291 |
+
<h2>健康检查</h2>
|
| 292 |
+
<ul>
|
| 293 |
+
<li><strong>URL</strong>: <code>{app.root_path}/health</code></li>
|
| 294 |
+
<li><strong>方法</strong>: <code>GET</code></li>
|
| 295 |
+
<li><strong>说明</strong>: 无需认证,用于检查服务是否正常运行</li>
|
| 296 |
+
</ul>
|
| 297 |
+
|
| 298 |
+
<p class="note">页面生成时间: {current_time}</p>
|
| 299 |
+
</body>
|
| 300 |
+
</html>
|
| 301 |
+
"""
|
| 302 |
|
| 303 |
|
| 304 |
|