Spaces:
Sleeping
Sleeping
sanbo
commited on
Commit
·
e201fa0
1
Parent(s):
cd320c7
update sth. at 2025-01-16 22:00:51
Browse files
app.py
CHANGED
@@ -1,17 +1,14 @@
|
|
1 |
-
from fastapi import FastAPI, HTTPException
|
2 |
from pydantic import BaseModel
|
3 |
from transformers import AutoTokenizer, AutoModel
|
4 |
import torch
|
5 |
from typing import List, Dict
|
6 |
import uvicorn
|
7 |
|
8 |
-
#
|
9 |
-
class TextRequest(BaseModel):
|
10 |
-
text: str
|
11 |
-
|
12 |
class EmbeddingResponse(BaseModel):
|
13 |
status: str
|
14 |
-
embeddings: List[
|
15 |
|
16 |
# 创建FastAPI应用
|
17 |
app = FastAPI(
|
@@ -25,30 +22,42 @@ model_name = "jinaai/jina-embeddings-v3"
|
|
25 |
tokenizer = AutoTokenizer.from_pretrained(model_name, trust_remote_code=True)
|
26 |
model = AutoModel.from_pretrained(model_name, trust_remote_code=True)
|
27 |
|
28 |
-
|
29 |
-
async def generate_embeddings(request: TextRequest):
|
30 |
try:
|
31 |
# 使用分词器处理输入文本
|
32 |
-
inputs = tokenizer(
|
33 |
|
34 |
-
|
35 |
with torch.no_grad():
|
36 |
embeddings = model(**inputs).last_hidden_state.mean(dim=1)
|
37 |
|
38 |
-
|
39 |
status="success",
|
40 |
embeddings=embeddings.numpy().tolist()
|
41 |
)
|
42 |
except Exception as e:
|
43 |
raise HTTPException(status_code=500, detail=str(e))
|
44 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
45 |
@app.get("/")
|
46 |
async def root():
|
47 |
return {
|
48 |
"status": "active",
|
49 |
"model": model_name,
|
50 |
-
"usage": "Send POST request to /
|
51 |
}
|
52 |
|
53 |
if __name__ == "__main__":
|
54 |
-
uvicorn.run(app, host="0.0.0.0", port=7860)
|
|
|
1 |
+
from fastapi import FastAPI, HTTPException, Request
|
2 |
from pydantic import BaseModel
|
3 |
from transformers import AutoTokenizer, AutoModel
|
4 |
import torch
|
5 |
from typing import List, Dict
|
6 |
import uvicorn
|
7 |
|
8 |
+
# 定义响应模型
|
|
|
|
|
|
|
9 |
class EmbeddingResponse(BaseModel):
|
10 |
status: str
|
11 |
+
embeddings: List[Listfloat]]
|
12 |
|
13 |
# 创建FastAPI应用
|
14 |
app = FastAPI(
|
|
|
22 |
tokenizer = AutoTokenizer.from_pretrained(model_name, trust_remote_code=True)
|
23 |
model = AutoModel.from_pretrained(model_name, trust_remote_code=True)
|
24 |
|
25 |
+
async def generate_embeddings(text: str):
|
|
|
26 |
try:
|
27 |
# 使用分词器处理输入文本
|
28 |
+
inputs = tokenizer(text, return_tensors="pt", truncation=True, max_length=512)
|
29 |
|
30 |
+
#生成嵌入
|
31 |
with torch.no_grad():
|
32 |
embeddings = model(**inputs).last_hidden_state.mean(dim=1)
|
33 |
|
34 |
+
return EmbeddingResponse(
|
35 |
status="success",
|
36 |
embeddings=embeddings.numpy().tolist()
|
37 |
)
|
38 |
except Exception as e:
|
39 |
raise HTTPException(status_code=500, detail=str(e))
|
40 |
|
41 |
+
@app.post("/api/v1/embeddings")
|
42 |
+
@app.post("/hf/v1/embeddings")
|
43 |
+
async def embedding(request: Request):
|
44 |
+
try:
|
45 |
+
data = await request.json()
|
46 |
+
text = data.get('input', '')
|
47 |
+
if not text:
|
48 |
+
raise HTTPException(status_code=400, detail="Input text is missing")
|
49 |
+
|
50 |
+
return await generate_embeddings(text)
|
51 |
+
except Exception as e:
|
52 |
+
raise HTTPException(status_code=500, detail=str(e))
|
53 |
+
|
54 |
@app.get("/")
|
55 |
async def root():
|
56 |
return {
|
57 |
"status": "active",
|
58 |
"model": model_name,
|
59 |
+
"usage": "Send POST request to /api/v1/embeddings"
|
60 |
}
|
61 |
|
62 |
if __name__ == "__main__":
|
63 |
+
uvicorn.run(app, host="0.0.0.0", port=7860)
|
app.py1
ADDED
@@ -0,0 +1,54 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from fastapi import FastAPI, HTTPException
|
2 |
+
from pydantic import BaseModel
|
3 |
+
from transformers import AutoTokenizer, AutoModel
|
4 |
+
import torch
|
5 |
+
from typing import List, Dict
|
6 |
+
import uvicorn
|
7 |
+
|
8 |
+
# 定义请求和响应模型
|
9 |
+
class TextRequest(BaseModel):
|
10 |
+
text: str
|
11 |
+
|
12 |
+
class EmbeddingResponse(BaseModel):
|
13 |
+
status: str
|
14 |
+
embeddings: List[List[float]]
|
15 |
+
|
16 |
+
# 创建FastAPI应用
|
17 |
+
app = FastAPI(
|
18 |
+
title="Jina Embeddings API",
|
19 |
+
description="Text embedding generation service using jina-embeddings-v3",
|
20 |
+
version="1.0.0"
|
21 |
+
)
|
22 |
+
|
23 |
+
# 加载模型和分词器
|
24 |
+
model_name = "jinaai/jina-embeddings-v3"
|
25 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name, trust_remote_code=True)
|
26 |
+
model = AutoModel.from_pretrained(model_name, trust_remote_code=True)
|
27 |
+
|
28 |
+
@app.post("/generate_embeddings", response_model=EmbeddingResponse)
|
29 |
+
async def generate_embeddings(request: TextRequest):
|
30 |
+
try:
|
31 |
+
# 使用分词器处理输入文本
|
32 |
+
inputs = tokenizer(request.text, return_tensors="pt", truncation=True, max_length=512)
|
33 |
+
|
34 |
+
# 生成嵌入
|
35 |
+
with torch.no_grad():
|
36 |
+
embeddings = model(**inputs).last_hidden_state.mean(dim=1)
|
37 |
+
|
38 |
+
return EmbeddingResponse(
|
39 |
+
status="success",
|
40 |
+
embeddings=embeddings.numpy().tolist()
|
41 |
+
)
|
42 |
+
except Exception as e:
|
43 |
+
raise HTTPException(status_code=500, detail=str(e))
|
44 |
+
|
45 |
+
@app.get("/")
|
46 |
+
async def root():
|
47 |
+
return {
|
48 |
+
"status": "active",
|
49 |
+
"model": model_name,
|
50 |
+
"usage": "Send POST request to /generate_embeddings"
|
51 |
+
}
|
52 |
+
|
53 |
+
if __name__ == "__main__":
|
54 |
+
uvicorn.run(app, host="0.0.0.0", port=7860)
|