Update app.py
Browse files
app.py
CHANGED
|
@@ -10,6 +10,10 @@ from datetime import timedelta
|
|
| 10 |
from datetime import timezone
|
| 11 |
import io
|
| 12 |
import requests
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
|
| 14 |
app = Flask(__name__)
|
| 15 |
|
|
@@ -25,6 +29,15 @@ qdrant_url = os.environ.get("qdrant_url")
|
|
| 25 |
client = QdrantClient(url=qdrant_url, port=443, api_key=qdrant_api_key, prefer_grpc=False)
|
| 26 |
|
| 27 |
# device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 28 |
|
| 29 |
def e5embed(query):
|
| 30 |
batch_dict = tokenizer(query, max_length=512, padding=True, truncation=True, return_tensors='pt')
|
|
|
|
| 10 |
from datetime import timezone
|
| 11 |
import io
|
| 12 |
import requests
|
| 13 |
+
import torch.nn.functional as F
|
| 14 |
+
import torch
|
| 15 |
+
from torch import Tensor
|
| 16 |
+
from transformers import AutoTokenizer, AutoModel
|
| 17 |
|
| 18 |
app = Flask(__name__)
|
| 19 |
|
|
|
|
| 29 |
client = QdrantClient(url=qdrant_url, port=443, api_key=qdrant_api_key, prefer_grpc=False)
|
| 30 |
|
| 31 |
# device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
| 32 |
+
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
| 33 |
+
|
| 34 |
+
def average_pool(last_hidden_states: Tensor,
|
| 35 |
+
attention_mask: Tensor) -> Tensor:
|
| 36 |
+
last_hidden = last_hidden_states.masked_fill(~attention_mask[..., None].bool(), 0.0)
|
| 37 |
+
return last_hidden.sum(dim=1) / attention_mask.sum(dim=1)[..., None]
|
| 38 |
+
|
| 39 |
+
tokenizer = AutoTokenizer.from_pretrained('intfloat/e5-base-v2')
|
| 40 |
+
model = AutoModel.from_pretrained('intfloat/e5-base-v2').to(device)
|
| 41 |
|
| 42 |
def e5embed(query):
|
| 43 |
batch_dict = tokenizer(query, max_length=512, padding=True, truncation=True, return_tensors='pt')
|