Spaces:
Runtime error
Runtime error
feat: fastAPI
Browse files- Dockerfile +1 -1
- app.py +68 -1
- requirements.txt +4 -2
Dockerfile
CHANGED
@@ -1,7 +1,7 @@
|
|
1 |
# read the doc: https://huggingface.co/docs/hub/spaces-sdks-docker
|
2 |
# you will also find guides on how best to write your Dockerfile
|
3 |
|
4 |
-
FROM python:3.
|
5 |
|
6 |
RUN useradd -m -u 1000 user
|
7 |
USER user
|
|
|
1 |
# read the doc: https://huggingface.co/docs/hub/spaces-sdks-docker
|
2 |
# you will also find guides on how best to write your Dockerfile
|
3 |
|
4 |
+
FROM python:3.10
|
5 |
|
6 |
RUN useradd -m -u 1000 user
|
7 |
USER user
|
app.py
CHANGED
@@ -1,7 +1,74 @@
|
|
|
|
|
|
1 |
from fastapi import FastAPI
|
|
|
|
|
|
|
|
|
|
|
|
|
2 |
|
3 |
app = FastAPI()
|
4 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
5 |
@app.get("/")
|
6 |
def greet_json():
|
7 |
-
return {"Hello": "World!"}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
from typing import List
|
3 |
from fastapi import FastAPI
|
4 |
+
from pydantic import BaseModel
|
5 |
+
from llama_index.vector_stores.milvus import MilvusVectorStore
|
6 |
+
from llama_index.embeddings.huggingface import HuggingFaceEmbedding
|
7 |
+
# from llama_index.core.postprocessor import SentenceTransformerRerank
|
8 |
+
from llama_index.core import VectorStoreIndex
|
9 |
+
from llama_index.core import Settings
|
10 |
|
11 |
app = FastAPI()
|
12 |
|
13 |
+
# rerank = SentenceTransformerRerank(
|
14 |
+
# model="cross-encoder/ms-marco-MiniLM-L-2-v2", top_n=3
|
15 |
+
# )
|
16 |
+
Settings.embed_model = HuggingFaceEmbedding(model_name="BAAI/bge-small-en-v1.5",
|
17 |
+
cache_folder=".cache")
|
18 |
+
|
19 |
+
vector_store = MilvusVectorStore(
|
20 |
+
overwrite=False,
|
21 |
+
uri=os.getenv('MILVUS_CLOUD_URI'),
|
22 |
+
token=os.getenv('MILVUS_CLOUD_TOKEN'),
|
23 |
+
collection_name=os.getenv('COLLECTION_NAME'),
|
24 |
+
dim=384,
|
25 |
+
)
|
26 |
+
|
27 |
@app.get("/")
|
28 |
def greet_json():
|
29 |
+
return {"Hello": "World!"}
|
30 |
+
|
31 |
+
class SearchRequest(BaseModel):
|
32 |
+
query: str
|
33 |
+
limit: int = 10
|
34 |
+
|
35 |
+
class Metadata(BaseModel):
|
36 |
+
window: str
|
37 |
+
original_text: str
|
38 |
+
|
39 |
+
class MyNodeWithScore(BaseModel):
|
40 |
+
node: Metadata
|
41 |
+
relationships: List[Metadata]
|
42 |
+
score: float
|
43 |
+
|
44 |
+
class MyResult(BaseModel):
|
45 |
+
results: List[MyNodeWithScore]
|
46 |
+
|
47 |
+
|
48 |
+
@app.post("/search/")
|
49 |
+
def search(search_request: SearchRequest):
|
50 |
+
sentence_index = VectorStoreIndex.from_vector_store(vector_store=vector_store)
|
51 |
+
|
52 |
+
retriever = sentence_index.as_retriever(
|
53 |
+
include_text=True, # include source chunk with matching paths
|
54 |
+
similarity_top_k=search_request.limit,
|
55 |
+
# node_postprocessors=[rerank]
|
56 |
+
)
|
57 |
+
|
58 |
+
result_retriever_engine = retriever.retrieve(search_request.query)
|
59 |
+
|
60 |
+
|
61 |
+
|
62 |
+
node_with_score_list = MyResult(results=[MyNodeWithScore(
|
63 |
+
node=Metadata(window=result.metadata['window'],
|
64 |
+
original_text=result.metadata['original_text']),
|
65 |
+
relationships=[
|
66 |
+
Metadata(window=relationship.metadata.get('window', " "),
|
67 |
+
original_text=relationship.metadata.get('original_text', " ")
|
68 |
+
) for key, relationship in result.node.relationships.items()
|
69 |
+
],
|
70 |
+
score=result.get_score()) for result in result_retriever_engine])
|
71 |
+
|
72 |
+
# node_with_score_list = [json.loads(result.json()) for result in query_engine]
|
73 |
+
|
74 |
+
return node_with_score_list
|
requirements.txt
CHANGED
@@ -1,2 +1,4 @@
|
|
1 |
-
fastapi
|
2 |
-
uvicorn[standard]
|
|
|
|
|
|
1 |
+
fastapi==0.111.0
|
2 |
+
uvicorn[standard]==0.30.1
|
3 |
+
llama-index-vector-stores-milvus==0.1.20
|
4 |
+
llama-index-embeddings-huggingface==0.2.2
|