Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
@@ -1,4 +1,4 @@
|
|
1 |
-
import spaces
|
2 |
import torch
|
3 |
import torch.nn.functional as F
|
4 |
from torch import Tensor
|
@@ -9,7 +9,7 @@ import os
|
|
9 |
title = """
|
10 |
# 👋🏻Welcome to 🙋🏻♂️Tonic's 🐣e5-mistral🛌🏻Embeddings """
|
11 |
description = """
|
12 |
-
You can use this Space to test out the current model [intfloat/e5-mistral-7b-instruct](https://huggingface.co/intfloat/e5-mistral-7b-instruct).
|
13 |
You can also use 🐣e5-mistral🛌🏻 by cloning this space. 🧬🔬🔍 Simply click here: <a style="display:inline-block" href="https://huggingface.co/spaces/Tonic/e5?duplicate=true"><img src="https://img.shields.io/badge/-Duplicate%20Space-blue?labelColor=white&style=flat&logo=data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAAAXNSR0IArs4c6QAAAP5JREFUOE+lk7FqAkEURY+ltunEgFXS2sZGIbXfEPdLlnxJyDdYB62sbbUKpLbVNhyYFzbrrA74YJlh9r079973psed0cvUD4A+4HoCjsA85X0Dfn/RBLBgBDxnQPfAEJgBY+A9gALA4tcbamSzS4xq4FOQAJgCDwV2CPKV8tZAJcAjMMkUe1vX+U+SMhfAJEHasQIWmXNN3abzDwHUrgcRGmYcgKe0bxrblHEB4E/pndMazNpSZGcsZdBlYJcEL9Afo75molJyM2FxmPgmgPqlWNLGfwZGG6UiyEvLzHYDmoPkDDiNm9JR9uboiONcBXrpY1qmgs21x1QwyZcpvxt9NS09PlsPAAAAAElFTkSuQmCC&logoWidth=14" alt="Duplicate Space"></a></h3>
|
14 |
Join us : 🌟TeamTonic🌟 is always making cool demos! Join our active builder's🛠️community 👻 [](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) 🤗Big thanks to Yuvi Sharma and all the folks at huggingface for the community grant 🤗
|
15 |
"""
|
@@ -44,52 +44,111 @@ def last_token_pool(last_hidden_states: Tensor, attention_mask: Tensor) -> Tenso
|
|
44 |
sequence_lengths = attention_mask.sum(dim=1) - 1
|
45 |
batch_size = last_hidden_states.shape[0]
|
46 |
return last_hidden_states[torch.arange(batch_size, device=last_hidden_states.device), sequence_lengths]
|
|
|
|
|
47 |
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
53 |
|
54 |
-
batch_dict = tokenizer(processed_texts, max_length=max_length - 1, return_attention_mask=False, padding=False, truncation=True)
|
55 |
-
batch_dict['input_ids'] = [input_ids + [tokenizer.eos_token_id] for input_ids in batch_dict['input_ids']]
|
56 |
-
batch_dict = tokenizer.pad(batch_dict, padding=True, return_attention_mask=True, return_tensors='pt')
|
57 |
-
batch_dict = {k: v.to(device) for k, v in batch_dict.items()}
|
58 |
-
outputs = model(**batch_dict)
|
59 |
-
embeddings = last_token_pool(outputs.last_hidden_state, batch_dict['attention_mask'])
|
60 |
-
embeddings = F.normalize(embeddings, p=2, dim=1)
|
61 |
-
embeddings_list = embeddings.detach().cpu().numpy().tolist()
|
62 |
-
return embeddings_list
|
63 |
|
64 |
def app_interface():
|
|
|
65 |
with gr.Blocks() as demo:
|
66 |
gr.Markdown(title)
|
67 |
gr.Markdown(description)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
68 |
|
69 |
-
task_dropdown = gr.Dropdown(list(tasks.keys()), label="Select a Task", value=list(tasks.keys())[0])
|
70 |
-
|
71 |
-
input_text_box = gr.Textbox(label="📖Input Text")
|
72 |
-
system_prompt_box = gr.Textbox(label="🤖System Prompt (Optional)")
|
73 |
-
|
74 |
-
compute_button = gr.Button("Try🐣🛌🏻e5")
|
75 |
-
|
76 |
-
output_display = gr.Textbox(label="🐣e5-mistral🛌🏻")
|
77 |
-
|
78 |
with gr.Row():
|
79 |
with gr.Column():
|
80 |
-
system_prompt_box
|
81 |
input_text_box
|
82 |
with gr.Column():
|
83 |
compute_button
|
84 |
output_display
|
85 |
|
86 |
-
compute_button.click(
|
87 |
-
fn=compute_embeddings,
|
88 |
-
inputs=[task_dropdown, input_text_box],
|
89 |
-
outputs=output_display
|
90 |
-
)
|
91 |
-
|
92 |
-
|
93 |
return demo
|
94 |
|
95 |
# Run the Gradio app
|
|
|
1 |
+
# import spaces
|
2 |
import torch
|
3 |
import torch.nn.functional as F
|
4 |
from torch import Tensor
|
|
|
9 |
title = """
|
10 |
# 👋🏻Welcome to 🙋🏻♂️Tonic's 🐣e5-mistral🛌🏻Embeddings """
|
11 |
description = """
|
12 |
+
You can use this ZeroGPU Space to test out the current model [intfloat/e5-mistral-7b-instruct](https://huggingface.co/intfloat/e5-mistral-7b-instruct). 🐣e5-mistral🛌🏻 has a larger context🪟window, a different prompting/return🛠️mechanism and generally better results than other embedding models. use it via API to create embeddings or try out the sentence similarity to see how various optimization parameters affect performance.
|
13 |
You can also use 🐣e5-mistral🛌🏻 by cloning this space. 🧬🔬🔍 Simply click here: <a style="display:inline-block" href="https://huggingface.co/spaces/Tonic/e5?duplicate=true"><img src="https://img.shields.io/badge/-Duplicate%20Space-blue?labelColor=white&style=flat&logo=data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAAAXNSR0IArs4c6QAAAP5JREFUOE+lk7FqAkEURY+ltunEgFXS2sZGIbXfEPdLlnxJyDdYB62sbbUKpLbVNhyYFzbrrA74YJlh9r079973psed0cvUD4A+4HoCjsA85X0Dfn/RBLBgBDxnQPfAEJgBY+A9gALA4tcbamSzS4xq4FOQAJgCDwV2CPKV8tZAJcAjMMkUe1vX+U+SMhfAJEHasQIWmXNN3abzDwHUrgcRGmYcgKe0bxrblHEB4E/pndMazNpSZGcsZdBlYJcEL9Afo75molJyM2FxmPgmgPqlWNLGfwZGG6UiyEvLzHYDmoPkDDiNm9JR9uboiONcBXrpY1qmgs21x1QwyZcpvxt9NS09PlsPAAAAAElFTkSuQmCC&logoWidth=14" alt="Duplicate Space"></a></h3>
|
14 |
Join us : 🌟TeamTonic🌟 is always making cool demos! Join our active builder's🛠️community 👻 [](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) 🤗Big thanks to Yuvi Sharma and all the folks at huggingface for the community grant 🤗
|
15 |
"""
|
|
|
44 |
sequence_lengths = attention_mask.sum(dim=1) - 1
|
45 |
batch_size = last_hidden_states.shape[0]
|
46 |
return last_hidden_states[torch.arange(batch_size, device=last_hidden_states.device), sequence_lengths]
|
47 |
+
def clear_cuda_cache():
|
48 |
+
torch.cuda.empty_cache()
|
49 |
|
50 |
+
def free_memory(*args):
|
51 |
+
for arg in args:
|
52 |
+
del arg
|
53 |
+
|
54 |
+
class EmbeddingModel:
|
55 |
+
def __init__(self):
|
56 |
+
self.tokenizer = AutoTokenizer.from_pretrained('intfloat/e5-mistral-7b-instruct')
|
57 |
+
self.model = AutoModel.from_pretrained('intfloat/e5-mistral-7b-instruct', torch_dtype=torch.float16, device_map=device)
|
58 |
+
|
59 |
+
def _compute_cosine_similarity(self, emb1, emb2):
|
60 |
+
tensor1 = torch.tensor(emb1).to(device).half()
|
61 |
+
tensor2 = torch.tensor(emb2).to(device).half()
|
62 |
+
similarity = F.cosine_similarity(tensor1, tensor2).item()
|
63 |
+
free_memory(tensor1, tensor2)
|
64 |
+
return similarity
|
65 |
+
|
66 |
+
def compute_embeddings(self, selected_task, input_text):
|
67 |
+
try:
|
68 |
+
task_description = tasks[selected_task]
|
69 |
+
except KeyError:
|
70 |
+
print(f"Selected task not found: {selected_task}")
|
71 |
+
return f"Error: Task '{selected_task}' not found. Please select a valid task."
|
72 |
+
max_length = 2042
|
73 |
+
processed_texts = [f'Instruct: {task_description}\nQuery: {input_text}']
|
74 |
+
|
75 |
+
batch_dict = self.tokenizer(processed_texts, max_length=max_length - 1, return_attention_mask=False, padding=False, truncation=True)
|
76 |
+
batch_dict['input_ids'] = [input_ids + [self.tokenizer.eos_token_id] for input_ids in batch_dict['input_ids']]
|
77 |
+
batch_dict = self.tokenizer.pad(batch_dict, padding=True, return_attention_mask=True, return_tensors='pt')
|
78 |
+
batch_dict = {k: v.to(device) for k, v in batch_dict.items()}
|
79 |
+
outputs = self.model(**batch_dict)
|
80 |
+
embeddings = last_token_pool(outputs.last_hidden_state, batch_dict['attention_mask'])
|
81 |
+
embeddings = F.normalize(embeddings, p=2, dim=1)
|
82 |
+
embeddings_list = embeddings.detach().cpu().numpy().tolist()
|
83 |
+
return embeddings_list
|
84 |
+
|
85 |
+
def compute_similarity(self, selected_task, sentence1, sentence2, extra_sentence1, extra_sentence2):
|
86 |
+
try:
|
87 |
+
task_description = tasks[selected_task]
|
88 |
+
except KeyError:
|
89 |
+
print(f"Selected task not found: {selected_task}")
|
90 |
+
return f"Error: Task '{selected_task}' not found. Please select a valid task."
|
91 |
+
# Compute embeddings for each sentence
|
92 |
+
embeddings1 = self.compute_embeddings(self.selected_task, sentence1)
|
93 |
+
embeddings2 = self.compute_embeddings(self.selected_task, sentence2)
|
94 |
+
embeddings3 = self.compute_embeddings(self.selected_task, extra_sentence1)
|
95 |
+
embeddings4 = self.compute_embeddings(self.selected_task, extra_sentence2)
|
96 |
+
|
97 |
+
# Convert embeddings to tensors
|
98 |
+
embeddings_tensor1 = torch.tensor(embeddings1).to(device).half()
|
99 |
+
embeddings_tensor2 = torch.tensor(embeddings2).to(device).half()
|
100 |
+
embeddings_tensor3 = torch.tensor(embeddings3).to(device).half()
|
101 |
+
embeddings_tensor4 = torch.tensor(embeddings4).to(device).half()
|
102 |
+
|
103 |
+
# Compute cosine similarity
|
104 |
+
similarity1 = self._compute_cosine_similarity(embeddings1, embeddings2)
|
105 |
+
similarity2 = self._compute_cosine_similarity(embeddings1, embeddings3)
|
106 |
+
similarity3 = self._compute_cosine_similarity(embeddings1, embeddings4)
|
107 |
+
|
108 |
+
# Free memory
|
109 |
+
free_memory(embeddings1, embeddings2, embeddings3, embeddings4)
|
110 |
+
|
111 |
+
return similarity1, similarity2, similarity3
|
112 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
113 |
|
114 |
def app_interface():
|
115 |
+
embedding_model = EmbeddingModel()
|
116 |
with gr.Blocks() as demo:
|
117 |
gr.Markdown(title)
|
118 |
gr.Markdown(description)
|
119 |
+
with gr.Row():
|
120 |
+
task_dropdown = gr.Dropdown(list(tasks.keys()), label="Select a Task", value=list(tasks.keys())[0])
|
121 |
+
|
122 |
+
with gr.Tab("Embedding Generation"):
|
123 |
+
input_text_box = gr.Textbox(label="📖Input Text")
|
124 |
+
compute_button = gr.Button("Try🐣🛌🏻e5")
|
125 |
+
output_display = gr.Textbox(label="🐣e5-mistral🛌🏻 Embeddings")
|
126 |
+
compute_button.click(
|
127 |
+
fn=embedding_model.compute_embeddings,
|
128 |
+
inputs=[task_dropdown, input_text_box],
|
129 |
+
outputs=output_display
|
130 |
+
)
|
131 |
+
|
132 |
+
with gr.Tab("Sentence Similarity"):
|
133 |
+
sentence1_box = gr.Textbox(label="'Focus Sentence' - The 'Subject'")
|
134 |
+
sentence2_box = gr.Textbox(label="'Input Sentence' - 1")
|
135 |
+
extra_sentence1_box = gr.Textbox(label="'Input Sentence' - 2")
|
136 |
+
extra_sentence2_box = gr.Textbox(label="'Input Sentence' - 3")
|
137 |
+
similarity_button = gr.Button("Compute Similarity")
|
138 |
+
similarity_output = gr.Label(label="🐣e5-mistral🛌🏻 Similarity Scores")
|
139 |
+
similarity_button.click(
|
140 |
+
fn=embedding_model.compute_similarity,
|
141 |
+
inputs=[task_dropdown, sentence1_box, sentence2_box],
|
142 |
+
outputs=similarity_output
|
143 |
+
)
|
144 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
145 |
with gr.Row():
|
146 |
with gr.Column():
|
|
|
147 |
input_text_box
|
148 |
with gr.Column():
|
149 |
compute_button
|
150 |
output_display
|
151 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
152 |
return demo
|
153 |
|
154 |
# Run the Gradio app
|