sanbo commited on
Commit
3028bfb
·
1 Parent(s): e201fa0

update sth. at 2025-01-16 22:04:20

Browse files
Files changed (2) hide show
  1. README.md +20 -0
  2. app.py +18 -22
README.md CHANGED
@@ -11,3 +11,23 @@ short_description: jina-embeddings-v3
11
  ---
12
 
13
  Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
11
  ---
12
 
13
  Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
14
+
15
+ ## Usage
16
+
17
+ You can generate embeddings by sending a POST request to one of the following endpoints:
18
+
19
+ - `/generate_embeddings`
20
+ - `/api/v1/embeddings`
21
+ - `/hf/v1/embeddings`
22
+ - `/api/v1/chat/completions`
23
+ - `/hf/v1/chat/completions`
24
+
25
+ Example request using `curl`:
26
+
27
+ ```sh
28
+ curl -X POST https://sanbo1200-jina-embeddings-v3.hf.space/api/v1/embeddings \
29
+ -H "Content-Type: application/json" \
30
+ -d '{
31
+ "input": "Your text string goes here",
32
+ "model": "jinaai/jina-embeddings-v3"
33
+ }'
app.py CHANGED
@@ -5,10 +5,14 @@ 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,41 +26,33 @@ model_name = "jinaai/jina-embeddings-v3"
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__":
 
5
  from typing import List, Dict
6
  import uvicorn
7
 
8
+ # 定义请求和响应模型
9
+ class EmbeddingRequest(BaseModel):
10
+ input: str
11
+ model: str = "jinaai/jina-embeddings-v3"
12
+
13
  class EmbeddingResponse(BaseModel):
14
  status: str
15
+ embeddings: List[List[float]]
16
 
17
  # 创建FastAPI应用
18
  app = FastAPI(
 
26
  tokenizer = AutoTokenizer.from_pretrained(model_name, trust_remote_code=True)
27
  model = AutoModel.from_pretrained(model_name, trust_remote_code=True)
28
 
29
+ @app.post("/generate_embeddings", response_model=EmbeddingResponse)
30
+ @app.post("/api/v1/embeddings", response_model=EmbeddingResponse)
31
+ @app.post("/hf/v1/embeddings", response_model=EmbeddingResponse)
32
+ @app.post("/api/v1/chat/completions", response_model=EmbeddingResponse)
33
+ @app.post("/hf/v1/chat/completions", response_model=EmbeddingResponse)
34
+ async def generate_embeddings(request: EmbeddingRequest):
35
  try:
36
  # 使用分词器处理输入文本
37
+ inputs = tokenizer(request.input, return_tensors="pt", truncation=True, max_length=512)
38
+
39
+ # 生成嵌入
40
  with torch.no_grad():
41
  embeddings = model(**inputs).last_hidden_state.mean(dim=1)
42
+
43
+ return EmbeddingResponse(
44
  status="success",
45
  embeddings=embeddings.numpy().tolist()
46
  )
47
  except Exception as e:
48
  raise HTTPException(status_code=500, detail=str(e))
49
 
 
 
 
 
 
 
 
 
 
 
 
 
 
50
  @app.get("/")
51
  async def root():
52
  return {
53
  "status": "active",
54
  "model": model_name,
55
+ "usage": "Send POST request to /generate_embeddings or /api/v1/embeddings or /hf/v1/embeddings"
56
  }
57
 
58
  if __name__ == "__main__":