Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -14,7 +14,12 @@ You can also use 🐣e5-mistral🛌🏻 by cloning this space. 🧬🔬🔍 Simp
|
|
| 14 |
Join us : 🌟TeamTonic🌟 is always making cool demos! Join our active builder's🛠️community on 👻Discord: [](https://discord.gg/GWpVpekp) On 🤗Huggingface: [TeamTonic](https://huggingface.co/TeamTonic) & [MultiTransformer](https://huggingface.co/MultiTransformer) On 🌐Github: [Polytonic](https://github.com/tonic-ai) & contribute to 🌟 [Poly](https://github.com/tonic-ai/poly)
|
| 15 |
"""
|
| 16 |
|
| 17 |
-
os.environ['PYTORCH_CUDA_ALLOC_CONF'] = 'max_split_size_mb:50'
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
|
| 19 |
def last_token_pool(last_hidden_states: Tensor, attention_mask: Tensor) -> Tensor:
|
| 20 |
left_padding = (attention_mask[:, -1].sum() == attention_mask.shape[0])
|
|
@@ -31,20 +36,15 @@ def get_detailed_instruct(task_description: str, query: str) -> str:
|
|
| 31 |
|
| 32 |
@spaces.GPU
|
| 33 |
def compute_embeddings(*input_texts):
|
| 34 |
-
torch.backends.cuda.matmul.allow_tf32 = True
|
| 35 |
-
torch.backends.cudnn.allow_tf32 = True
|
| 36 |
-
torch.backends.cudnn.benchmark = True
|
| 37 |
tokenizer = AutoTokenizer.from_pretrained('intfloat/e5-mistral-7b-instruct')
|
| 38 |
-
model = AutoModel.from_pretrained('intfloat/e5-mistral-7b-instruct')
|
| 39 |
-
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
| 40 |
-
model.to(device)
|
| 41 |
max_length = 4096
|
| 42 |
task = 'Given a web search query, retrieve relevant passages that answer the query'
|
| 43 |
-
|
| 44 |
processed_texts = [get_detailed_instruct(task, text) for text in input_texts]
|
| 45 |
batch_dict = tokenizer(processed_texts, max_length=max_length - 1, return_attention_mask=False, padding=False, truncation=True)
|
| 46 |
batch_dict['input_ids'] = [input_ids + [tokenizer.eos_token_id] for input_ids in batch_dict['input_ids']]
|
| 47 |
batch_dict = tokenizer.pad(batch_dict, padding=True, return_attention_mask=True, return_tensors='pt')
|
|
|
|
| 48 |
outputs = model(**batch_dict)
|
| 49 |
embeddings = last_token_pool(outputs.last_hidden_state, batch_dict['attention_mask'])
|
| 50 |
embeddings = F.normalize(embeddings, p=2, dim=1)
|
|
|
|
| 14 |
Join us : 🌟TeamTonic🌟 is always making cool demos! Join our active builder's🛠️community on 👻Discord: [](https://discord.gg/GWpVpekp) On 🤗Huggingface: [TeamTonic](https://huggingface.co/TeamTonic) & [MultiTransformer](https://huggingface.co/MultiTransformer) On 🌐Github: [Polytonic](https://github.com/tonic-ai) & contribute to 🌟 [Poly](https://github.com/tonic-ai/poly)
|
| 15 |
"""
|
| 16 |
|
| 17 |
+
# os.environ['PYTORCH_CUDA_ALLOC_CONF'] = 'max_split_size_mb:50'
|
| 18 |
+
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
| 19 |
+
|
| 20 |
+
# torch.backends.cuda.matmul.allow_tf32 = True
|
| 21 |
+
# torch.backends.cudnn.allow_tf32 = True
|
| 22 |
+
# torch.backends.cudnn.benchmark = True
|
| 23 |
|
| 24 |
def last_token_pool(last_hidden_states: Tensor, attention_mask: Tensor) -> Tensor:
|
| 25 |
left_padding = (attention_mask[:, -1].sum() == attention_mask.shape[0])
|
|
|
|
| 36 |
|
| 37 |
@spaces.GPU
|
| 38 |
def compute_embeddings(*input_texts):
|
|
|
|
|
|
|
|
|
|
| 39 |
tokenizer = AutoTokenizer.from_pretrained('intfloat/e5-mistral-7b-instruct')
|
| 40 |
+
model = AutoModel.from_pretrained('intfloat/e5-mistral-7b-instruct', torch_dtype="auto", device_map=device))
|
|
|
|
|
|
|
| 41 |
max_length = 4096
|
| 42 |
task = 'Given a web search query, retrieve relevant passages that answer the query'
|
|
|
|
| 43 |
processed_texts = [get_detailed_instruct(task, text) for text in input_texts]
|
| 44 |
batch_dict = tokenizer(processed_texts, max_length=max_length - 1, return_attention_mask=False, padding=False, truncation=True)
|
| 45 |
batch_dict['input_ids'] = [input_ids + [tokenizer.eos_token_id] for input_ids in batch_dict['input_ids']]
|
| 46 |
batch_dict = tokenizer.pad(batch_dict, padding=True, return_attention_mask=True, return_tensors='pt')
|
| 47 |
+
batch_dict = {k: v.to(device) for k, v in batch_dict.items()}
|
| 48 |
outputs = model(**batch_dict)
|
| 49 |
embeddings = last_token_pool(outputs.last_hidden_state, batch_dict['attention_mask'])
|
| 50 |
embeddings = F.normalize(embeddings, p=2, dim=1)
|