Spaces:
Runtime error
Runtime error
initial working test
Browse files- app.py +45 -0
- requirements.txt +3 -0
app.py
ADDED
|
@@ -0,0 +1,45 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from transformers import AutoModel, AutoTokenizer
|
| 3 |
+
from sklearn.neighbors import NearestNeighbors
|
| 4 |
+
|
| 5 |
+
|
| 6 |
+
|
| 7 |
+
MODEL = "cardiffnlp/twitter-roberta-base-jun2022"
|
| 8 |
+
model = AutoModel.from_pretrained(MODEL)
|
| 9 |
+
tokenizer = AutoTokenizer.from_pretrained(MODEL)
|
| 10 |
+
embedding_matrix = model.embeddings.word_embeddings.weight
|
| 11 |
+
embedding_matrix = embedding_matrix.detach().numpy()
|
| 12 |
+
|
| 13 |
+
knn_model = NearestNeighbors(n_neighbors=500,
|
| 14 |
+
metric='cosine',
|
| 15 |
+
algorithm='auto',
|
| 16 |
+
n_jobs=3)
|
| 17 |
+
|
| 18 |
+
nbrs = knn_model.fit(embedding_matrix)
|
| 19 |
+
|
| 20 |
+
distances, indices = nbrs.kneighbors(embedding_matrix)
|
| 21 |
+
|
| 22 |
+
|
| 23 |
+
title = "How does a word's meaning change with time?"
|
| 24 |
+
|
| 25 |
+
|
| 26 |
+
def topk(word):
|
| 27 |
+
outs = []
|
| 28 |
+
index = tokenizer.encode(f'{word}')
|
| 29 |
+
for i in indices[index[1]]:
|
| 30 |
+
outs.append(tokenizer.decode(i))
|
| 31 |
+
print(tokenizer.decode(i))
|
| 32 |
+
|
| 33 |
+
with gr.Blocks() as demo:
|
| 34 |
+
gr.Markdown(f" # {title}")
|
| 35 |
+
# gr.Markdown(f" ## {description1}")
|
| 36 |
+
# gr.Markdown(f"{description2}")
|
| 37 |
+
# gr.Markdown(f"{description3}")
|
| 38 |
+
with gr.Row():
|
| 39 |
+
word = gr.Textbox(label="Word")
|
| 40 |
+
with gr.Row():
|
| 41 |
+
greet_btn = gr.Button("Compute")
|
| 42 |
+
with gr.Row():
|
| 43 |
+
greet_btn.click(fn=topk, inputs=[word], outputs=gr.outputs.Textbox())
|
| 44 |
+
|
| 45 |
+
demo.launch()
|
requirements.txt
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
transformers
|
| 2 |
+
torch
|
| 3 |
+
scikit-learn
|