Spaces:
Sleeping
Sleeping
v1 with gradio + custom api endpoint
Browse files- Dockerfile +1 -1
- app.py +43 -5
- requirements.txt +2 -1
Dockerfile
CHANGED
@@ -7,7 +7,7 @@ ARG DEBIAN_FRONTEND=noninteractive
|
|
7 |
|
8 |
RUN apt-get update
|
9 |
|
10 |
-
RUN python -m pip install spaces
|
11 |
|
12 |
WORKDIR /app
|
13 |
COPY requirements.txt .
|
|
|
7 |
|
8 |
RUN apt-get update
|
9 |
|
10 |
+
RUN python -m pip install spaces pydantic
|
11 |
|
12 |
WORKDIR /app
|
13 |
COPY requirements.txt .
|
app.py
CHANGED
@@ -1,20 +1,58 @@
|
|
1 |
import gradio as gr
|
2 |
-
from fastapi import FastAPI
|
3 |
import uvicorn
|
|
|
|
|
|
|
|
|
|
|
4 |
|
5 |
app = FastAPI()
|
6 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
7 |
|
8 |
with gr.Blocks(fill_height=True) as demo:
|
9 |
-
|
|
|
|
|
|
|
|
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
-
@app.
|
16 |
-
def
|
17 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
18 |
|
19 |
|
20 |
|
|
|
1 |
import gradio as gr
|
2 |
+
from fastapi import FastAPI, Request
|
3 |
import uvicorn
|
4 |
+
import spaces
|
5 |
+
from sentence_transformers import SentenceTransformer
|
6 |
+
from sentence_transformers.util import cos_sim
|
7 |
+
from sentence_transformers.quantization import quantize_embeddings
|
8 |
+
|
9 |
|
10 |
app = FastAPI()
|
11 |
|
12 |
+
print("Loading embedding model");
|
13 |
+
Embedder = SentenceTransformer("mixedbread-ai/mxbai-embed-large-v1")
|
14 |
+
|
15 |
+
@spaces.GPU
|
16 |
+
def embed(text):
|
17 |
+
|
18 |
+
query_embedding = Embedder.encode(text)
|
19 |
+
return query_embedding.tolist();
|
20 |
+
|
21 |
+
|
22 |
+
|
23 |
|
24 |
with gr.Blocks(fill_height=True) as demo:
|
25 |
+
text = gr.Textbox();
|
26 |
+
embeddings = gr.Textbox()
|
27 |
+
|
28 |
+
text.submit(embed, [text], [embeddings]);
|
29 |
+
|
30 |
|
31 |
|
32 |
|
33 |
|
34 |
|
35 |
+
@app.post("/v1/embeddings")
|
36 |
+
async def openai_embeddings(request: Request):
|
37 |
+
body = await request.json();
|
38 |
+
print(body);
|
39 |
+
|
40 |
+
model = body['model']
|
41 |
+
text = body['input'];
|
42 |
+
embeddings = embed(text)
|
43 |
+
return {
|
44 |
+
'object': "list"
|
45 |
+
,'data': [{
|
46 |
+
'object': "embeddings"
|
47 |
+
,'embedding': embeddings
|
48 |
+
,'index':0
|
49 |
+
}]
|
50 |
+
,'model':model
|
51 |
+
,'usage':{
|
52 |
+
'prompt_tokens': 0
|
53 |
+
,'total_tokens': 0
|
54 |
+
}
|
55 |
+
}
|
56 |
|
57 |
|
58 |
|
requirements.txt
CHANGED
@@ -1,2 +1,3 @@
|
|
1 |
fastapi
|
2 |
-
uvicorn
|
|
|
|
1 |
fastapi
|
2 |
+
uvicorn
|
3 |
+
sentence_transformers
|