mirror of
https://github.com/neuml/txtai.git
synced 2026-10-12 02:19:52 +00:00
15 KiB
15 KiB
In [ ]:
%%capture
!pip install git+https://github.com/neuml/txtai#egg=txtai[api]In [ ]:
%%writefile config.yml
cloud:
provider: huggingface-hub
container: neuml/txtai-intro
embeddings:Writing config.yml
In [ ]:
import uuid
str(uuid.uuid5(uuid.NAMESPACE_DNS, "TokenTest"))'edd590d3-bfab-5425-8a85-79b01e3127ee'
In [ ]:
!CONFIG=config.yml TOKEN=`echo -n 'edd590d3-bfab-5425-8a85-79b01e3127ee' | sha256sum | head -c 64` uvicorn "txtai.api:app" &> api.log &
!sleep 60In [ ]:
!curl -X GET -I 'http://localhost:8000/search?query=feel+good+story&limit=1'HTTP/1.1 401 Unauthorized [1mdate[0m: Thu, 04 Jan 2024 15:08:38 GMT [1mserver[0m: uvicorn [1mcontent-length[0m: 40 [1mcontent-type[0m: application/json
In [ ]:
!curl -X GET -I 'http://localhost:8000/search?query=feel+good+story&limit=1' -H 'Authorization: Bearer junk'HTTP/1.1 401 Unauthorized [1mdate[0m: Thu, 04 Jan 2024 15:08:38 GMT [1mserver[0m: uvicorn [1mcontent-length[0m: 40 [1mcontent-type[0m: application/json
In [ ]:
!curl -X GET 'http://localhost:8000/search?query=feel+good+story&limit=1' -H 'Authorization: Bearer edd590d3-bfab-5425-8a85-79b01e3127ee'[{"id":"4","text":"Maine man wins $1M from $25 lottery ticket","score":0.08329025655984879}]In [ ]:
%%writefile authentication.py
import secrets
from fastapi import Depends, HTTPException, status
from fastapi.security import HTTPBasic, HTTPBasicCredentials
security = HTTPBasic()
class Authentication:
def __call__(self, credentials: HTTPBasicCredentials = Depends(security)):
user = credentials.username.encode("utf8")
validuser = secrets.compare_digest(user, b"txtai")
password = credentials.password.encode("utf8")
validpassword = secrets.compare_digest(password, b"theembeddingsdb")
if not (validuser and validpassword):
raise HTTPException(
status_code=status.HTTP_401_UNAUTHORIZED,
detail="Incorrect user or password",
headers={"WWW-Authenticate": "Basic"},
)
return credentials.usernameWriting authentication.py
In [ ]:
!killall -9 uvicorn
!CONFIG=config.yml DEPENDENCIES=authentication.Authentication uvicorn "txtai.api:app" &> api.log &
!sleep 30In [ ]:
!curl -X GET -I 'http://localhost:8000/search?query=feel+good+story&limit=1'HTTP/1.1 401 Unauthorized [1mdate[0m: Thu, 04 Jan 2024 15:09:10 GMT [1mserver[0m: uvicorn [1mwww-authenticate[0m: Basic [1mcontent-length[0m: 30 [1mcontent-type[0m: application/json
In [ ]:
!curl -X GET -I 'http://localhost:8000/search?query=feel+good+story&limit=1' -H "Authorization: Basic junk"HTTP/1.1 401 Unauthorized [1mdate[0m: Thu, 04 Jan 2024 15:09:10 GMT [1mserver[0m: uvicorn [1mwww-authenticate[0m: Basic [1mcontent-length[0m: 47 [1mcontent-type[0m: application/json
In [ ]:
!echo -n txtai:theembeddingsdb | base64dHh0YWk6dGhlZW1iZWRkaW5nc2Ri
In [ ]:
!curl -X GET 'http://localhost:8000/search?query=feel+good+story&limit=1' -H "Authorization: Basic dHh0YWk6dGhlZW1iZWRkaW5nc2Ri"[{"id":"4","text":"Maine man wins $1M from $25 lottery ticket","score":0.08329025655984879}]