rachith commited on
Commit
6b9b713
·
1 Parent(s): 73ab6a3

preloading models

Browse files
Files changed (1) hide show
  1. app.py +14 -7
app.py CHANGED
@@ -2,15 +2,22 @@ import gradio as gr
2
  from transformers import AutoModel, AutoTokenizer
3
  from sklearn.neighbors import NearestNeighbors
4
 
 
 
 
 
 
 
 
 
 
5
 
6
- models = ['cardiffnlp/twitter-roberta-base-jun2022',
7
- 'cardiffnlp/twitter-roberta-base-2019-90m']
8
 
9
  def topk_model(MODEL):
10
  # MODEL = "cardiffnlp/twitter-roberta-base-jun2022"
11
- model = AutoModel.from_pretrained(MODEL)
12
- tokenizer = AutoTokenizer.from_pretrained(MODEL)
13
- embedding_matrix = model.embeddings.word_embeddings.weight
14
  embedding_matrix = embedding_matrix.detach().numpy()
15
 
16
  knn_model = NearestNeighbors(n_neighbors=500,
@@ -22,7 +29,7 @@ def topk_model(MODEL):
22
 
23
  distances, indices = nbrs.kneighbors(embedding_matrix)
24
 
25
- return distances,indices,tokenizer
26
 
27
 
28
  title = "How does a word's meaning change with time?"
@@ -52,7 +59,7 @@ def topk(word,model):
52
  # demo.launch()
53
 
54
  interface = gr.Interface(fn=topk,
55
- inputs=[gr.Textbox(label="Word"), gr.Dropdown(models)],
56
  outputs=gr.outputs.Textbox()
57
  )
58
  interface.launch()
 
2
  from transformers import AutoModel, AutoTokenizer
3
  from sklearn.neighbors import NearestNeighbors
4
 
5
+ available_models = ['cardiffnlp/twitter-roberta-base-2019-90m',
6
+ 'cardiffnlp/twitter-roberta-base-jun2020']
7
+
8
+ models = {}
9
+ tokenizers = {}
10
+
11
+ for MODEL in available_models:
12
+ models[MODEL] = AutoModel.from_pretrained(MODEL)
13
+ tokenizers[MODEL] = AutoTokenizer.from_pretrained(MODEL)
14
 
 
 
15
 
16
  def topk_model(MODEL):
17
  # MODEL = "cardiffnlp/twitter-roberta-base-jun2022"
18
+ # model = AutoModel.from_pretrained(MODEL)
19
+ # tokenizer = AutoTokenizer.from_pretrained(MODEL)
20
+ embedding_matrix = models[MODEL].embeddings.word_embeddings.weight
21
  embedding_matrix = embedding_matrix.detach().numpy()
22
 
23
  knn_model = NearestNeighbors(n_neighbors=500,
 
29
 
30
  distances, indices = nbrs.kneighbors(embedding_matrix)
31
 
32
+ return distances,indices,tokenizers[MODEL]
33
 
34
 
35
  title = "How does a word's meaning change with time?"
 
59
  # demo.launch()
60
 
61
  interface = gr.Interface(fn=topk,
62
+ inputs=[gr.Textbox(label="Word"), gr.Dropdown(available_models)],
63
  outputs=gr.outputs.Textbox()
64
  )
65
  interface.launch()