Sergidev commited on
Commit
2371338
·
verified ·
1 Parent(s): 4ede10d

Mistral v2

Browse files
Files changed (1) hide show
  1. app.py +6 -19
app.py CHANGED
@@ -3,33 +3,21 @@ import spaces
3
  import torch
4
  from transformers import AutoTokenizer, AutoModel
5
  import plotly.graph_objects as go
6
- from huggingface_hub import HfApi
7
- from huggingface_hub import hf_hub_download
8
- import os
9
- import sys
10
 
11
- HF_TOKEN = os.getenv("HF_TOKEN")
12
-
13
- import gradio as gr
14
- import spaces
15
- import torch
16
- from transformers import AutoTokenizer, AutoModel
17
- import plotly.graph_objects as go
18
- from huggingface_hub import HfApi
19
- from huggingface_hub import hf_hub_download
20
- import os
21
- import sys
22
-
23
- # Update the model name to Mistral 7B
24
  model_name = "mistralai/Mistral-7B-v0.1"
25
  tokenizer = AutoTokenizer.from_pretrained(model_name)
26
  model = None
27
 
 
 
 
 
28
  @spaces.GPU
29
  def get_embedding(text):
30
  global model
31
  if model is None:
32
  model = AutoModel.from_pretrained(model_name).cuda()
 
33
 
34
  inputs = tokenizer(text, return_tensors="pt", padding=True, truncation=True, max_length=512).to('cuda')
35
  with torch.no_grad():
@@ -37,7 +25,6 @@ def get_embedding(text):
37
  return outputs.last_hidden_state.mean(dim=1).squeeze().cpu().numpy()
38
 
39
  def reduce_to_3d(embedding):
40
- # Instead of PCA, we'll just take the first 3 dimensions
41
  return embedding[:3]
42
 
43
  @spaces.GPU
@@ -65,7 +52,7 @@ iface = gr.Interface(
65
  ],
66
  outputs=gr.Plot(),
67
  title="3D Embedding Comparison",
68
- description="Compare the embeddings of two strings visualized in 3D space."
69
  )
70
 
71
  iface.launch()
 
3
  import torch
4
  from transformers import AutoTokenizer, AutoModel
5
  import plotly.graph_objects as go
 
 
 
 
6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7
  model_name = "mistralai/Mistral-7B-v0.1"
8
  tokenizer = AutoTokenizer.from_pretrained(model_name)
9
  model = None
10
 
11
+ # Set pad token to eos token if not defined
12
+ if tokenizer.pad_token is None:
13
+ tokenizer.pad_token = tokenizer.eos_token
14
+
15
  @spaces.GPU
16
  def get_embedding(text):
17
  global model
18
  if model is None:
19
  model = AutoModel.from_pretrained(model_name).cuda()
20
+ model.resize_token_embeddings(len(tokenizer))
21
 
22
  inputs = tokenizer(text, return_tensors="pt", padding=True, truncation=True, max_length=512).to('cuda')
23
  with torch.no_grad():
 
25
  return outputs.last_hidden_state.mean(dim=1).squeeze().cpu().numpy()
26
 
27
  def reduce_to_3d(embedding):
 
28
  return embedding[:3]
29
 
30
  @spaces.GPU
 
52
  ],
53
  outputs=gr.Plot(),
54
  title="3D Embedding Comparison",
55
+ description="Compare the embeddings of two strings visualized in 3D space using Mistral 7B."
56
  )
57
 
58
  iface.launch()