Aryan J Chugh
commited on
Commit
•
a14b305
1
Parent(s):
e89c31d
Added examples and changed header
Browse files
app.py
CHANGED
@@ -10,28 +10,17 @@ glove_vectors = gensim.downloader.load('glove-twitter-25')
|
|
10 |
labels = np.load('pca_labels.npy')
|
11 |
vectors = np.load('pca_vectors.npy')
|
12 |
|
|
|
|
|
13 |
|
14 |
with gr.Blocks() as demo:
|
15 |
gr.Markdown("""
|
16 |
# ![ai bloq logo https://www.aibloq.com](https://aibloq.com/_next/image?url=%2FLogo.png&w=48&q=75) [Ai Bloq](https://www.aibloq.com)
|
17 |
# How machines understand natural language
|
18 |
-
## This NLP example is a part of Ai Bloq's Blog: **[How
|
19 |
### For more such content and illustrative explanations visit [Ai Bloq's Resources](https://www.aibloq.com) and explore different machine learning and deep learning concepts
|
20 |
## **To create industry level artificially intelligent services and application sign up for a free demo account at [Ai Bloq](https://www.aibloq.com) :- A No-Code data science platform with industry level auto scaling capabilities**
|
21 |
""")
|
22 |
-
with gr.Tab("Visualize words"):
|
23 |
-
sentence_input_viz = gr.Textbox(label="Enter a sentence")
|
24 |
-
pca_output = gr.Plot()
|
25 |
-
|
26 |
-
generate_pca_button = gr.Button("Visualize words in 3D space")
|
27 |
-
with gr.Tab("View word vectors"):
|
28 |
-
sentence_input = gr.Textbox(label="Enter a sentence")
|
29 |
-
vectors_output = gr.Plot()
|
30 |
-
|
31 |
-
generate_vectors_button = gr.Button("Generate vectors")
|
32 |
-
|
33 |
-
with gr.Accordion("Words not present in the vocabulary"):
|
34 |
-
excl_words_md = gr.Markdown("Enter a sentence and generate vectors first")
|
35 |
|
36 |
def break_words(input_sentence):
|
37 |
|
@@ -48,10 +37,10 @@ with gr.Blocks() as demo:
|
|
48 |
|
49 |
for word in words:
|
50 |
|
51 |
-
if glove_vectors.key_to_index.get(word.strip(), None) == None:
|
52 |
excluded_words_state.append(word.strip())
|
53 |
else:
|
54 |
-
final_words.append(word.strip())
|
55 |
|
56 |
if len(final_words) == 0:
|
57 |
raise gr.Error("No word is present in the vocabulary, please try with another sentence")
|
@@ -127,6 +116,40 @@ with gr.Blocks() as demo:
|
|
127 |
|
128 |
return [go.Figure(data=traces), excluded_words_state]
|
129 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
130 |
generate_vectors_button.click(generate_vectors, inputs=sentence_input, outputs=[vectors_output, excl_words_md])
|
131 |
generate_pca_button.click(generate_pca_plot, inputs=sentence_input_viz, outputs=[pca_output, excl_words_md])
|
132 |
|
|
|
10 |
labels = np.load('pca_labels.npy')
|
11 |
vectors = np.load('pca_vectors.npy')
|
12 |
|
13 |
+
sentence_examples = ["How did a bear climb up the tree ?", "It is a nice sunny day in India", "I am very excited to learn the concepts of NLP"]
|
14 |
+
|
15 |
|
16 |
with gr.Blocks() as demo:
|
17 |
gr.Markdown("""
|
18 |
# ![ai bloq logo https://www.aibloq.com](https://aibloq.com/_next/image?url=%2FLogo.png&w=48&q=75) [Ai Bloq](https://www.aibloq.com)
|
19 |
# How machines understand natural language
|
20 |
+
## This NLP example is a part of Ai Bloq's Blog: **[Natural Language Processing: How Neural Word Embeddings Enable Machines to Understand Text](https://medium.com/@aryan_93507/how-do-machines-understand-text-via-natural-language-processing-nlp-41aeb853ef52)**
|
21 |
### For more such content and illustrative explanations visit [Ai Bloq's Resources](https://www.aibloq.com) and explore different machine learning and deep learning concepts
|
22 |
## **To create industry level artificially intelligent services and application sign up for a free demo account at [Ai Bloq](https://www.aibloq.com) :- A No-Code data science platform with industry level auto scaling capabilities**
|
23 |
""")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
24 |
|
25 |
def break_words(input_sentence):
|
26 |
|
|
|
37 |
|
38 |
for word in words:
|
39 |
|
40 |
+
if glove_vectors.key_to_index.get(word.strip().lower(), None) == None:
|
41 |
excluded_words_state.append(word.strip())
|
42 |
else:
|
43 |
+
final_words.append(word.strip().lower())
|
44 |
|
45 |
if len(final_words) == 0:
|
46 |
raise gr.Error("No word is present in the vocabulary, please try with another sentence")
|
|
|
116 |
|
117 |
return [go.Figure(data=traces), excluded_words_state]
|
118 |
|
119 |
+
excl_words_md = None
|
120 |
+
|
121 |
+
with gr.Tab("Visualize words"):
|
122 |
+
sentence_input_viz = gr.Textbox(label="Enter a sentence")
|
123 |
+
pca_output = gr.Plot()
|
124 |
+
|
125 |
+
generate_pca_button = gr.Button("Visualize words in 3D space")
|
126 |
+
|
127 |
+
gr.Markdown("## Sentence Examples")
|
128 |
+
gr.Examples(
|
129 |
+
sentence_examples,
|
130 |
+
sentence_input_viz,
|
131 |
+
[pca_output, excl_words_md],
|
132 |
+
generate_pca_plot,
|
133 |
+
# cache_examples=True,
|
134 |
+
)
|
135 |
+
with gr.Tab("View word vectors"):
|
136 |
+
sentence_input = gr.Textbox(label="Enter a sentence")
|
137 |
+
vectors_output = gr.Plot()
|
138 |
+
|
139 |
+
generate_vectors_button = gr.Button("Generate vectors")
|
140 |
+
|
141 |
+
gr.Markdown("## Sentence Examples")
|
142 |
+
gr.Examples(
|
143 |
+
sentence_examples,
|
144 |
+
sentence_input,
|
145 |
+
[vectors_output, excl_words_md],
|
146 |
+
generate_vectors,
|
147 |
+
# cache_examples=True,
|
148 |
+
)
|
149 |
+
|
150 |
+
with gr.Accordion("Words not present in the vocabulary"):
|
151 |
+
excl_words_md = gr.Markdown("Enter a sentence and generate vectors first")
|
152 |
+
|
153 |
generate_vectors_button.click(generate_vectors, inputs=sentence_input, outputs=[vectors_output, excl_words_md])
|
154 |
generate_pca_button.click(generate_pca_plot, inputs=sentence_input_viz, outputs=[pca_output, excl_words_md])
|
155 |
|