Spaces:
Sleeping
Sleeping
fastelectronicvegetable
commited on
Commit
·
d29e2b3
1
Parent(s):
69242cf
cors middleware
Browse files
main.py
CHANGED
|
@@ -5,6 +5,7 @@ from pydantic.generics import GenericModel
|
|
| 5 |
from sentence_transformers import SentenceTransformer
|
| 6 |
from fastapi import FastAPI
|
| 7 |
import os, asyncio, numpy, ujson
|
|
|
|
| 8 |
|
| 9 |
MODEL = SentenceTransformer("all-mpnet-base-v2")
|
| 10 |
|
|
@@ -31,10 +32,6 @@ async def encode(sentences: List[str]) -> List[numpy.ndarray]:
|
|
| 31 |
result = await loop.run_in_executor(None, _encode, sentences)
|
| 32 |
return result
|
| 33 |
|
| 34 |
-
class SemanticSearchReq(BaseModel):
|
| 35 |
-
query: str
|
| 36 |
-
candidates: List[str]
|
| 37 |
-
|
| 38 |
class EmbedReq(BaseModel):
|
| 39 |
sentences: List[str]
|
| 40 |
|
|
@@ -44,4 +41,11 @@ app = FastAPI()
|
|
| 44 |
async def embed(embed: EmbedReq):
|
| 45 |
result = await encode(embed.sentences)
|
| 46 |
# Convert it to an ordinary list of floats
|
| 47 |
-
return ujson.dumps([r.tolist() for r in result])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
from sentence_transformers import SentenceTransformer
|
| 6 |
from fastapi import FastAPI
|
| 7 |
import os, asyncio, numpy, ujson
|
| 8 |
+
from fastapi.middleware.cors import CORSMiddleware
|
| 9 |
|
| 10 |
MODEL = SentenceTransformer("all-mpnet-base-v2")
|
| 11 |
|
|
|
|
| 32 |
result = await loop.run_in_executor(None, _encode, sentences)
|
| 33 |
return result
|
| 34 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 35 |
class EmbedReq(BaseModel):
|
| 36 |
sentences: List[str]
|
| 37 |
|
|
|
|
| 41 |
async def embed(embed: EmbedReq):
|
| 42 |
result = await encode(embed.sentences)
|
| 43 |
# Convert it to an ordinary list of floats
|
| 44 |
+
return ujson.dumps([r.tolist() for r in result])
|
| 45 |
+
|
| 46 |
+
app.add_middleware(
|
| 47 |
+
CORSMiddleware,
|
| 48 |
+
allow_origins=["*"],
|
| 49 |
+
allow_methods=["*"],
|
| 50 |
+
allow_headers=["*"],
|
| 51 |
+
)
|