Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -54,14 +54,11 @@ def get_embedding(text: str) -> List[float]:
|
|
| 54 |
|
| 55 |
return embeddings[0].tolist()
|
| 56 |
|
| 57 |
-
@app.post("/v1/embeddings", response_model=EmbeddingResponse)
|
| 58 |
@spaces.GPU()
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
input_texts = request.input
|
| 64 |
-
|
| 65 |
embeddings = []
|
| 66 |
total_tokens = 0
|
| 67 |
|
|
@@ -77,22 +74,30 @@ async def create_embeddings(request: EmbeddingRequest):
|
|
| 77 |
"index": len(embeddings)
|
| 78 |
})
|
| 79 |
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
|
|
|
| 84 |
"prompt_tokens": total_tokens,
|
| 85 |
"total_tokens": total_tokens
|
| 86 |
}
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 90 |
|
| 91 |
@spaces.GPU()
|
| 92 |
def gradio_embedding(text: str) -> Dict:
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
|
|
|
|
|
|
|
|
|
| 96 |
|
| 97 |
# 创建 Gradio 界面
|
| 98 |
demo = gr.Interface(
|
|
|
|
| 54 |
|
| 55 |
return embeddings[0].tolist()
|
| 56 |
|
|
|
|
| 57 |
@spaces.GPU()
|
| 58 |
+
def process_embeddings(request_dict: dict) -> dict:
|
| 59 |
+
"""非异步函数处理嵌入向量"""
|
| 60 |
+
input_texts = [request_dict["input"]] if isinstance(request_dict["input"], str) else request_dict["input"]
|
| 61 |
+
|
|
|
|
|
|
|
| 62 |
embeddings = []
|
| 63 |
total_tokens = 0
|
| 64 |
|
|
|
|
| 74 |
"index": len(embeddings)
|
| 75 |
})
|
| 76 |
|
| 77 |
+
return {
|
| 78 |
+
"object": "list",
|
| 79 |
+
"data": embeddings,
|
| 80 |
+
"model": request_dict.get("model", model_name),
|
| 81 |
+
"usage": {
|
| 82 |
"prompt_tokens": total_tokens,
|
| 83 |
"total_tokens": total_tokens
|
| 84 |
}
|
| 85 |
+
}
|
| 86 |
+
|
| 87 |
+
@app.post("/v1/embeddings", response_model=EmbeddingResponse)
|
| 88 |
+
async def create_embeddings(request: EmbeddingRequest):
|
| 89 |
+
"""异步API端点"""
|
| 90 |
+
result = process_embeddings(request.dict())
|
| 91 |
+
return result
|
| 92 |
|
| 93 |
@spaces.GPU()
|
| 94 |
def gradio_embedding(text: str) -> Dict:
|
| 95 |
+
"""Gradio接口函数"""
|
| 96 |
+
request_dict = {
|
| 97 |
+
"input": text,
|
| 98 |
+
"model": model_name
|
| 99 |
+
}
|
| 100 |
+
return process_embeddings(request_dict)
|
| 101 |
|
| 102 |
# 创建 Gradio 界面
|
| 103 |
demo = gr.Interface(
|