Spaces:
Sleeping
Sleeping
first commit
Browse files- .env +1 -0
- Dockerfile +11 -0
- helpers.py +17 -0
- main.py +56 -0
- requirements.txt +121 -0
.env
ADDED
|
@@ -0,0 +1 @@
|
|
|
|
|
|
|
| 1 |
+
UPSTASH_REDIS_PWD = "d1cd7cd7021446928fb03f8aa3ab15ac"
|
Dockerfile
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
FROM python:3.10
|
| 2 |
+
|
| 3 |
+
WORKDIR /code
|
| 4 |
+
|
| 5 |
+
COPY ./requirements.txt /code/requirements.txt
|
| 6 |
+
|
| 7 |
+
RUN pip install --no-cache-dir --upgrade -r /code/requirements.txt
|
| 8 |
+
|
| 9 |
+
COPY . .
|
| 10 |
+
|
| 11 |
+
CMD ["uvicorn", "app.main:app", "--host", "0.0.0.0", "--port", "7860"]
|
helpers.py
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from fuzzywuzzy import fuzz
|
| 2 |
+
|
| 3 |
+
class isCache:
|
| 4 |
+
def __init__(self, status, closest_match) -> None:
|
| 5 |
+
self.status = status
|
| 6 |
+
self.closest_match = closest_match
|
| 7 |
+
|
| 8 |
+
def in_cache (message: str, keysList: list):
|
| 9 |
+
highest_score = 0
|
| 10 |
+
for s in keysList:
|
| 11 |
+
score = fuzz.partial_token_sort_ratio(message, s)
|
| 12 |
+
if score > highest_score:
|
| 13 |
+
highest_score = score
|
| 14 |
+
if highest_score > 90:
|
| 15 |
+
return isCache(True, s)
|
| 16 |
+
return isCache(False, None)
|
| 17 |
+
|
main.py
ADDED
|
@@ -0,0 +1,56 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from dotenv import load_dotenv
|
| 2 |
+
import os
|
| 3 |
+
|
| 4 |
+
from transformers import AutoTokenizer, BlenderbotSmallForConditionalGeneration
|
| 5 |
+
from helpers import in_cache
|
| 6 |
+
|
| 7 |
+
from fastapi import FastAPI
|
| 8 |
+
from pydantic import BaseModel
|
| 9 |
+
import redis
|
| 10 |
+
|
| 11 |
+
from starlette.middleware.cors import CORSMiddleware
|
| 12 |
+
|
| 13 |
+
|
| 14 |
+
load_dotenv()
|
| 15 |
+
|
| 16 |
+
model = BlenderbotSmallForConditionalGeneration.from_pretrained("facebook/blenderbot_small-90M")
|
| 17 |
+
tokenizer = AutoTokenizer.from_pretrained("facebook/blenderbot_small-90M")
|
| 18 |
+
class UserMSGRequest(BaseModel):
|
| 19 |
+
message: str
|
| 20 |
+
|
| 21 |
+
r = redis.Redis(
|
| 22 |
+
host='eu2-suitable-cod-32440.upstash.io',
|
| 23 |
+
port=32440,
|
| 24 |
+
password=os.getenv('UPSTASH_REDIS_PWD'),
|
| 25 |
+
ssl=True
|
| 26 |
+
)
|
| 27 |
+
|
| 28 |
+
app = FastAPI()
|
| 29 |
+
|
| 30 |
+
origins = ["*"]
|
| 31 |
+
|
| 32 |
+
app.add_middleware(
|
| 33 |
+
CORSMiddleware,
|
| 34 |
+
allow_origins=origins,
|
| 35 |
+
allow_credentials=True,
|
| 36 |
+
allow_methods=["*"],
|
| 37 |
+
allow_headers=["*"],
|
| 38 |
+
)
|
| 39 |
+
|
| 40 |
+
@app.post("/generate-message")
|
| 41 |
+
async def root(utterance: UserMSGRequest):
|
| 42 |
+
key_list = r.keys()
|
| 43 |
+
is_in_cache = in_cache(utterance.message, key_list)
|
| 44 |
+
if is_in_cache.status:
|
| 45 |
+
print("From Cache!")
|
| 46 |
+
return r.get(is_in_cache.closest_match)
|
| 47 |
+
inputs = tokenizer(utterance.message, return_tensors = "pt")
|
| 48 |
+
results = model.generate(**inputs)
|
| 49 |
+
response = tokenizer.batch_decode(results, skip_special_tokens=True)[0]
|
| 50 |
+
r.set(utterance.message, response, 6000)
|
| 51 |
+
return response
|
| 52 |
+
|
| 53 |
+
if __name__ == '__main__':
|
| 54 |
+
import uvicorn
|
| 55 |
+
|
| 56 |
+
uvicorn.run(app, host="0.0.0.0", port=8080)
|
requirements.txt
ADDED
|
@@ -0,0 +1,121 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
aioredis==2.0.1
|
| 2 |
+
annotated-types==0.6.0
|
| 3 |
+
ansiwrap==0.8.4
|
| 4 |
+
anyio==3.7.1
|
| 5 |
+
async-timeout==4.0.3
|
| 6 |
+
attrs==23.1.0
|
| 7 |
+
beautifulsoup4==4.12.2
|
| 8 |
+
blinker==1.7.0
|
| 9 |
+
certifi==2023.11.17
|
| 10 |
+
charset-normalizer==3.3.2
|
| 11 |
+
click==8.1.7
|
| 12 |
+
cryptography==3.4.8
|
| 13 |
+
dnspython==2.4.2
|
| 14 |
+
email-validator==2.1.0.post1
|
| 15 |
+
entrypoints==0.4
|
| 16 |
+
exceptiongroup==1.2.0
|
| 17 |
+
fastapi==0.105.0
|
| 18 |
+
fastjsonschema==2.18.0
|
| 19 |
+
filelock==3.13.1
|
| 20 |
+
find-libpython==0.3.0
|
| 21 |
+
Flask==3.0.0
|
| 22 |
+
fsspec==2023.12.2
|
| 23 |
+
fuzzywuzzy==0.18.0
|
| 24 |
+
h11==0.14.0
|
| 25 |
+
httpcore==1.0.2
|
| 26 |
+
httplib2==0.20.2
|
| 27 |
+
httptools==0.6.1
|
| 28 |
+
httpx==0.25.2
|
| 29 |
+
huggingface-hub==0.19.4
|
| 30 |
+
idna==3.6
|
| 31 |
+
importlib-metadata==4.6.4
|
| 32 |
+
itsdangerous==2.1.2
|
| 33 |
+
jeepney==0.7.1
|
| 34 |
+
jellyfish==0.11.2
|
| 35 |
+
Jinja2==3.1.2
|
| 36 |
+
jsonschema==4.19.0
|
| 37 |
+
jsonschema-specifications==2023.7.1
|
| 38 |
+
jupyter_client==8.3.1
|
| 39 |
+
jupyter_core==5.3.1
|
| 40 |
+
keyring==23.5.0
|
| 41 |
+
launchpadlib==1.10.16
|
| 42 |
+
lazr.restfulclient==0.14.4
|
| 43 |
+
lazr.uri==1.0.6
|
| 44 |
+
Levenshtein==0.23.0
|
| 45 |
+
MarkupSafe==2.1.3
|
| 46 |
+
more-itertools==8.10.0
|
| 47 |
+
mpmath==1.3.0
|
| 48 |
+
nbclient==0.8.0
|
| 49 |
+
nbformat==5.9.2
|
| 50 |
+
netifaces==0.11.0
|
| 51 |
+
networkx==3.2.1
|
| 52 |
+
numpy==1.26.2
|
| 53 |
+
nvidia-cublas-cu12==12.1.3.1
|
| 54 |
+
nvidia-cuda-cupti-cu12==12.1.105
|
| 55 |
+
nvidia-cuda-nvrtc-cu12==12.1.105
|
| 56 |
+
nvidia-cuda-runtime-cu12==12.1.105
|
| 57 |
+
nvidia-cudnn-cu12==8.9.2.26
|
| 58 |
+
nvidia-cufft-cu12==11.0.2.54
|
| 59 |
+
nvidia-curand-cu12==10.3.2.106
|
| 60 |
+
nvidia-cusolver-cu12==11.4.5.107
|
| 61 |
+
nvidia-cusparse-cu12==12.1.0.106
|
| 62 |
+
nvidia-nccl-cu12==2.18.1
|
| 63 |
+
nvidia-nvjitlink-cu12==12.3.101
|
| 64 |
+
nvidia-nvtx-cu12==12.1.105
|
| 65 |
+
oauthlib==3.2.0
|
| 66 |
+
openai==1.3.9
|
| 67 |
+
orjson==3.9.10
|
| 68 |
+
packaging==23.2
|
| 69 |
+
papermill==2.4.0
|
| 70 |
+
pexpect==4.8.0
|
| 71 |
+
Pillow==10.1.0
|
| 72 |
+
platformdirs==3.10.0
|
| 73 |
+
ptyprocess==0.7.0
|
| 74 |
+
pydantic==2.5.2
|
| 75 |
+
pydantic-extra-types==2.2.0
|
| 76 |
+
pydantic-settings==2.1.0
|
| 77 |
+
pydantic_core==2.14.5
|
| 78 |
+
Pygments==2.11.2
|
| 79 |
+
PyJWT==2.3.0
|
| 80 |
+
pyparsing==2.4.7
|
| 81 |
+
python-dateutil==2.8.2
|
| 82 |
+
python-dotenv==1.0.0
|
| 83 |
+
python-Levenshtein==0.23.0
|
| 84 |
+
python-multipart==0.0.6
|
| 85 |
+
python-telegram-bot==20.7
|
| 86 |
+
PyYAML==6.0.1
|
| 87 |
+
pyzmq==25.1.1
|
| 88 |
+
rapidfuzz==3.5.2
|
| 89 |
+
redis==5.0.1
|
| 90 |
+
referencing==0.30.2
|
| 91 |
+
regex==2023.10.3
|
| 92 |
+
requests==2.31.0
|
| 93 |
+
rpds-py==0.10.2
|
| 94 |
+
safetensors==0.4.1
|
| 95 |
+
SecretStorage==3.3.1
|
| 96 |
+
six==1.16.0
|
| 97 |
+
sniffio==1.3.0
|
| 98 |
+
soupsieve==2.4.1
|
| 99 |
+
starlette==0.27.0
|
| 100 |
+
tenacity==8.2.3
|
| 101 |
+
termcolor==1.1.0
|
| 102 |
+
textwrap3==0.9.2
|
| 103 |
+
tokenizers==0.15.0
|
| 104 |
+
torch==2.1.2
|
| 105 |
+
torchaudio==2.1.2
|
| 106 |
+
torchvision==0.16.2
|
| 107 |
+
tornado==6.3.3
|
| 108 |
+
tqdm==4.66.1
|
| 109 |
+
traitlets==5.9.0
|
| 110 |
+
transformers==4.36.1
|
| 111 |
+
triton==2.1.0
|
| 112 |
+
typing_extensions==4.9.0
|
| 113 |
+
ujson==5.9.0
|
| 114 |
+
urllib3==2.1.0
|
| 115 |
+
uvicorn==0.24.0.post1
|
| 116 |
+
uvloop==0.19.0
|
| 117 |
+
wadllib==1.3.6
|
| 118 |
+
watchfiles==0.21.0
|
| 119 |
+
websockets==12.0
|
| 120 |
+
Werkzeug==3.0.1
|
| 121 |
+
zipp==1.0.0
|