sophiamyang commited on
Commit
cef1936
1 Parent(s): 10339b7

multiple apps

Browse files
Files changed (5) hide show
  1. Dockerfile +11 -0
  2. hvplot_interactive.py +53 -0
  3. requirements.txt +6 -0
  4. text_analysis.py +352 -0
  5. trivia.py +168 -0
Dockerfile ADDED
@@ -0,0 +1,11 @@
 
 
 
 
 
 
 
 
 
 
 
 
1
+ FROM python:3.9
2
+
3
+ WORKDIR /code
4
+
5
+ COPY ./requirements.txt /code/requirements.txt
6
+
7
+ RUN pip install --no-cache-dir --upgrade -r /code/requirements.txt
8
+
9
+ COPY . .
10
+
11
+ CMD ["panel", "serve", "/code/hvplot_interactive.py", "/code/text_analysis.py", "/code/trivia.py", "--address", "0.0.0.0", "--port", "7860", "--allow-websocket-origin", "sophiamyang-panel-apps.hf.space"]
hvplot_interactive.py ADDED
@@ -0,0 +1,53 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import panel as pn
2
+ pn.extension('tabulator', sizing_mode="stretch_width")
3
+
4
+ import hvplot.pandas
5
+
6
+ # Load Data
7
+ from bokeh.sampledata.autompg import autompg_clean as df
8
+
9
+ # Make DataFrame Pipeline Interactive
10
+ idf = df.interactive()
11
+
12
+ # Define Panel widgets
13
+ cylinders = pn.widgets.IntSlider(name='Cylinders', start=4, end=8, step=2)
14
+ mfr = pn.widgets.ToggleGroup(
15
+ name='MFR',
16
+ options=['ford', 'chevrolet', 'honda', 'toyota', 'audi'],
17
+ value=['ford', 'chevrolet', 'honda', 'toyota', 'audi'],
18
+ button_type='success')
19
+ yaxis = pn.widgets.RadioButtonGroup(
20
+ name='Y axis',
21
+ options=['hp', 'weight'],
22
+ button_type='success'
23
+ )
24
+
25
+ # Combine pipeline and widgets
26
+ ipipeline = (
27
+ idf[
28
+ (idf.cyl == cylinders) &
29
+ (idf.mfr.isin(mfr))
30
+ ]
31
+ .groupby(['origin', 'mpg'])[yaxis].mean()
32
+ .to_frame()
33
+ .reset_index()
34
+ .sort_values(by='mpg')
35
+ .reset_index(drop=True)
36
+ )
37
+
38
+ # Pipe to hvplot
39
+ ihvplot = ipipeline.hvplot(x='mpg', y=yaxis, by='origin', color=["#ff6f69", "#ffcc5c", "#88d8b0"], line_width=6, height=400)
40
+
41
+ # Pipe to table
42
+ itable = ipipeline.pipe(pn.widgets.Tabulator, pagination='remote', page_size=10)
43
+ itable
44
+
45
+ # Layout using Template
46
+ template = pn.template.FastListTemplate(
47
+ title='Interactive DataFrame Dashboards with hvplot .interactive',
48
+ sidebar=[cylinders, 'Manufacturers', mfr, 'Y axis' , yaxis],
49
+ main=[ihvplot.panel(), itable.panel()],
50
+ accent_base_color="#88d8b0",
51
+ header_background="#88d8b0",
52
+ )
53
+ template.servable()
requirements.txt ADDED
@@ -0,0 +1,6 @@
 
 
 
 
 
 
 
1
+ panel
2
+ hvplot
3
+ requests
4
+ pandas
5
+ textblob
6
+ scikit-learn==0.24.2
text_analysis.py ADDED
@@ -0,0 +1,352 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #!/usr/bin/env python
2
+ # coding: utf-8
3
+
4
+ # In[1]:
5
+
6
+
7
+ import panel as pn
8
+ import requests
9
+ import pandas as pd
10
+ from textblob import TextBlob
11
+ pn.extension()
12
+ pn.extension('tabulator')
13
+ import warnings
14
+ warnings.filterwarnings('ignore')
15
+
16
+
17
+ # In[2]:
18
+
19
+
20
+ sample_text = """
21
+ Happiness is a very complicated thing. Happiness can be used both in emotional or mental state context and can vary largely from a feeling from contentment to very intense feeling of joy. It can also mean a life of satisfaction, good well-being and so many more. Happiness is a very difficult phenomenon to use words to describe as it is something that can be felt only. Happiness is very important if we want to lead a very good life. Sadly, happiness is absent from the lives of a lot of people nowadays. We all have our own very different concept of happiness. Some of us are of the opinion that we can get happiness through money, others believe they can only get true happiness in relationships, some even feel that happiness can only be gotten when they are excelling in their profession.
22
+ As we might probably know, happiness is nothing more than the state of one being content and happy. A lot of people in the past, present and some (even in the future will) have tried to define and explain what they think happiness really is. So far, the most reasonable one is the one that sees happiness as something that can only come from within a person and should not be sought for outside in the world.
23
+ Some very important points about happiness are discussed below:
24
+ 1. Happiness can’t be bought with Money:
25
+ A lot of us try to find happiness where it is not. We associate and equate money with happiness. If at all there is happiness in money then all of the rich people we have around us would never feel sad. What we have come to see is that even the rich amongst us are the ones that suffer depression, relationship problems, stress, fear and even anxiousness. A lot of celebrities and successful people have committed suicide, this goes a long way to show that money or fame does not guarantee happiness. This does not mean that it is a bad thing to be rich and go after money. When you have money, you can afford many things that can make you and those around you very happy.
26
+ 2. Happiness can only come from within:
27
+ There is a saying that explains that one can only get true happiness when one comes to the realisation that only one can make himself/herself happy. We can only find true happiness within ourselves and we can’t find it in other people. This saying and its meaning is always hammered on in different places but we still refuse to fully understand it and put it into good use. It is very important that we understand that happiness is nothing more than the state of a person’s mind. Happiness cannot come from all the physical things we see around us. Only we through our positive emotions that we can get through good thoughts have the ability to create true happiness.
28
+ Our emotions are created by our thoughts. Therefore, it is very important that we work on having only positive thoughts and this can be achieved when we see life in a positive light."""
29
+
30
+
31
+ # In[3]:
32
+
33
+
34
+ # from nltk.corpus import stopwords
35
+ # stoplist = stopwords.words('english') + ['though']
36
+ stoplist = ['i',
37
+ 'me',
38
+ 'my',
39
+ 'myself',
40
+ 'we',
41
+ 'our',
42
+ 'ours',
43
+ 'ourselves',
44
+ 'you',
45
+ "you're",
46
+ "you've",
47
+ "you'll",
48
+ "you'd",
49
+ 'your',
50
+ 'yours',
51
+ 'yourself',
52
+ 'yourselves',
53
+ 'he',
54
+ 'him',
55
+ 'his',
56
+ 'himself',
57
+ 'she',
58
+ "she's",
59
+ 'her',
60
+ 'hers',
61
+ 'herself',
62
+ 'it',
63
+ "it's",
64
+ 'its',
65
+ 'itself',
66
+ 'they',
67
+ 'them',
68
+ 'their',
69
+ 'theirs',
70
+ 'themselves',
71
+ 'what',
72
+ 'which',
73
+ 'who',
74
+ 'whom',
75
+ 'this',
76
+ 'that',
77
+ "that'll",
78
+ 'these',
79
+ 'those',
80
+ 'am',
81
+ 'is',
82
+ 'are',
83
+ 'was',
84
+ 'were',
85
+ 'be',
86
+ 'been',
87
+ 'being',
88
+ 'have',
89
+ 'has',
90
+ 'had',
91
+ 'having',
92
+ 'do',
93
+ 'does',
94
+ 'did',
95
+ 'doing',
96
+ 'a',
97
+ 'an',
98
+ 'the',
99
+ 'and',
100
+ 'but',
101
+ 'if',
102
+ 'or',
103
+ 'because',
104
+ 'as',
105
+ 'until',
106
+ 'while',
107
+ 'of',
108
+ 'at',
109
+ 'by',
110
+ 'for',
111
+ 'with',
112
+ 'about',
113
+ 'against',
114
+ 'between',
115
+ 'into',
116
+ 'through',
117
+ 'during',
118
+ 'before',
119
+ 'after',
120
+ 'above',
121
+ 'below',
122
+ 'to',
123
+ 'from',
124
+ 'up',
125
+ 'down',
126
+ 'in',
127
+ 'out',
128
+ 'on',
129
+ 'off',
130
+ 'over',
131
+ 'under',
132
+ 'again',
133
+ 'further',
134
+ 'then',
135
+ 'once',
136
+ 'here',
137
+ 'there',
138
+ 'when',
139
+ 'where',
140
+ 'why',
141
+ 'how',
142
+ 'all',
143
+ 'any',
144
+ 'both',
145
+ 'each',
146
+ 'few',
147
+ 'more',
148
+ 'most',
149
+ 'other',
150
+ 'some',
151
+ 'such',
152
+ 'no',
153
+ 'nor',
154
+ 'not',
155
+ 'only',
156
+ 'own',
157
+ 'same',
158
+ 'so',
159
+ 'than',
160
+ 'too',
161
+ 'very',
162
+ 's',
163
+ 't',
164
+ 'can',
165
+ 'will',
166
+ 'just',
167
+ 'don',
168
+ "don't",
169
+ 'should',
170
+ "should've",
171
+ 'now',
172
+ 'd',
173
+ 'll',
174
+ 'm',
175
+ 'o',
176
+ 're',
177
+ 've',
178
+ 'y',
179
+ 'ain',
180
+ 'aren',
181
+ "aren't",
182
+ 'couldn',
183
+ "couldn't",
184
+ 'didn',
185
+ "didn't",
186
+ 'doesn',
187
+ "doesn't",
188
+ 'hadn',
189
+ "hadn't",
190
+ 'hasn',
191
+ "hasn't",
192
+ 'haven',
193
+ "haven't",
194
+ 'isn',
195
+ "isn't",
196
+ 'ma',
197
+ 'mightn',
198
+ "mightn't",
199
+ 'mustn',
200
+ "mustn't",
201
+ 'needn',
202
+ "needn't",
203
+ 'shan',
204
+ "shan't",
205
+ 'shouldn',
206
+ "shouldn't",
207
+ 'wasn',
208
+ "wasn't",
209
+ 'weren',
210
+ "weren't",
211
+ 'won',
212
+ "won't",
213
+ 'wouldn',
214
+ "wouldn't",
215
+ 'though']
216
+
217
+
218
+ # In[4]:
219
+
220
+
221
+ def get_sentiment(text):
222
+ return pn.pane.Markdown(f"""
223
+ Polarity (range from -1 negative to 1 positive): {TextBlob(text).polarity} \n
224
+ Subjectivity (range from 0 objective to 1 subjective): {TextBlob(text).subjectivity}
225
+ """)
226
+
227
+
228
+ # In[5]:
229
+
230
+
231
+ def get_ngram(text):
232
+ from sklearn.feature_extraction.text import CountVectorizer
233
+ c_vec = CountVectorizer(stop_words=stoplist, ngram_range=(2,3))
234
+ # matrix of ngrams
235
+ try:
236
+ ngrams = c_vec.fit_transform([text])
237
+ except ValueError: # if less than 2 words, return empty result
238
+ return pn.widgets.Tabulator(width=600)
239
+ # count frequency of ngrams
240
+ count_values = ngrams.toarray().sum(axis=0)
241
+ # list of ngrams
242
+ vocab = c_vec.vocabulary_
243
+ df_ngram = pd.DataFrame(sorted([(count_values[i],k) for k,i in vocab.items()], reverse=True)
244
+ ).rename(columns={0: 'frequency', 1:'bigram/trigram'})
245
+ df_ngram['polarity'] = df_ngram['bigram/trigram'].apply(lambda x: TextBlob(x).polarity)
246
+ df_ngram['subjective'] = df_ngram['bigram/trigram'].apply(lambda x: TextBlob(x).subjectivity)
247
+ return pn.widgets.Tabulator(df_ngram, width=600, height=300)
248
+
249
+
250
+ # In[6]:
251
+
252
+
253
+ def get_ntopics(text, ntopics):
254
+ from sklearn.feature_extraction.text import TfidfVectorizer
255
+ from sklearn.decomposition import NMF
256
+ from sklearn.pipeline import make_pipeline
257
+ tfidf_vectorizer = TfidfVectorizer(stop_words=stoplist, ngram_range=(2,3))
258
+ nmf = NMF(n_components=ntopics)
259
+ pipe = make_pipeline(tfidf_vectorizer, nmf)
260
+ try:
261
+ pipe.fit([text])
262
+ except ValueError: # if less than 2 words, return empty result
263
+ return
264
+ message = ""
265
+ for topic_idx, topic in enumerate(nmf.components_):
266
+ message += "####Topic #%d: " % topic_idx
267
+ message += ", ".join([tfidf_vectorizer.get_feature_names()[i]
268
+ for i in topic.argsort()[:-3 - 1:-1]])
269
+ message += "\n"
270
+ return pn.pane.Markdown(message)
271
+
272
+
273
+ # In[7]:
274
+
275
+
276
+ explanation = pn.pane.Markdown("""
277
+ This app provides a simple text analysis for a given input text or text file. \n
278
+ - Sentiment analysis uses [TextBlob](https://textblob.readthedocs.io/).
279
+ - N-gram analysis uses [scikit-learn](https://scikit-learn.org/stable/modules/generated/sklearn.feature_extraction.text.CountVectorizer.html) to see which words show up together.
280
+ - Topic modeling uses [scikit-learn](https://scikit-learn.org/stable/auto_examples/applications/plot_topics_extraction_with_nmf_lda.html) NMF model and we can change the number of topics we'd like to see in the result.
281
+ """)
282
+
283
+ def get_text_results(_):
284
+ return pn.Column(
285
+ explanation,
286
+ pn.pane.Markdown("""
287
+ ##Sentiment analysis:"""),
288
+ get_sentiment(text_widget.value.replace("\n", "")),
289
+ pn.pane.Markdown("##N-gram analysis (bigram/trigram):"),
290
+ get_ngram(text_widget.value.replace("\n", "")),
291
+ pn.pane.Markdown("##Topic modeling:"),
292
+ get_ntopics(text_widget.value.replace("\n", ""), ntopics_widget.value)
293
+ )
294
+
295
+
296
+ # In[8]:
297
+
298
+
299
+ button = pn.widgets.Button(name="Click me to run!")
300
+
301
+
302
+ # In[9]:
303
+
304
+
305
+ file_input_widget = pn.widgets.FileInput()
306
+ def update_text_widget(event):
307
+ text_widget.value = event.new.decode("utf-8")
308
+ # when the value of file_input_widget changes,
309
+ # run this function to update the text of the text widget
310
+ file_input_widget.param.watch(update_text_widget, "value");
311
+
312
+
313
+ # In[10]:
314
+
315
+
316
+ text_widget = pn.widgets.TextAreaInput(value=sample_text, height=300, name='Add text')
317
+
318
+
319
+ # In[11]:
320
+
321
+
322
+ ntopics_widget = pn.widgets.IntSlider(name='Number of topics', start=2, end=10, step=1, value=3)
323
+
324
+
325
+ # In[12]:
326
+
327
+
328
+ interactive = pn.bind(get_text_results, button)
329
+
330
+
331
+ # Layout using Template
332
+ template = pn.template.FastListTemplate(
333
+ title='Simple Text Analysis',
334
+ sidebar=[
335
+ button,
336
+ ntopics_widget,
337
+ text_widget,
338
+ "Upload a text file",
339
+ file_input_widget
340
+ ],
341
+ main=[pn.panel(interactive, loading_indicator=True)],
342
+ accent_base_color="#88d8b0",
343
+ header_background="#88d8b0",
344
+ )
345
+ template.servable()
346
+
347
+
348
+ # In[ ]:
349
+
350
+
351
+
352
+
trivia.py ADDED
@@ -0,0 +1,168 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #!/usr/bin/env python
2
+ # coding: utf-8
3
+
4
+ # In[ ]:
5
+
6
+
7
+ import panel as pn
8
+ import requests
9
+ import pandas as pd
10
+ pn.extension()
11
+
12
+
13
+ # In[ ]:
14
+
15
+
16
+ def get_data(num_questions, difficulty, category):
17
+ url = f"https://opentdb.com/api.php?amount={num_questions}&category={category_match[category]}&difficulty={difficulty}&type=boolean"
18
+ df = pd.DataFrame(
19
+ requests.get(url).json()['results']
20
+ )
21
+ return df
22
+
23
+
24
+ # In[ ]:
25
+
26
+
27
+ category = pn.widgets.Select(
28
+ name='Category',
29
+ options=[
30
+ 'General Knowledge',
31
+ 'Film',
32
+ 'Music',
33
+ 'Video Games',
34
+ 'Science & Nature',
35
+ 'Computers',
36
+ 'Geography',
37
+ 'History',
38
+ 'Politics',
39
+ 'Animals',
40
+ 'Japanese Anime & Manga'
41
+ ],
42
+ value='General Knowledge'
43
+ )
44
+ category
45
+
46
+
47
+ # In[ ]:
48
+
49
+
50
+ category_match = {
51
+ 'General Knowledge': 9,
52
+ 'Books': 10,
53
+ 'Film': 11,
54
+ 'Music': 12,
55
+ 'Musicals & Theatres': 13,
56
+ 'Television': 14,
57
+ 'Video Games': 15,
58
+ 'Board Games': 16,
59
+ 'Science & Nature': 17,
60
+ 'Computers': 18,
61
+ 'Mathematics': 19,
62
+ 'Mythology': 20,
63
+ 'Sports': 21,
64
+ 'Geography': 22,
65
+ 'History': 23,
66
+ 'Politics': 24,
67
+ 'Art': 25,
68
+ 'Celebrities': 26,
69
+ 'Animals': 27,
70
+ 'Vehicles': 28,
71
+ 'Comics': 29,
72
+ 'Gadgets': 30,
73
+ 'Japanese Anime & Manga': 31,
74
+ 'Cartoon & Animations': 32
75
+ }
76
+
77
+
78
+ # In[ ]:
79
+
80
+
81
+ difficulty = pn.widgets.Select(
82
+ name='Difficulty',
83
+ options=['easy', 'medium', 'hard'],
84
+ value='easy'
85
+ )
86
+ difficulty
87
+
88
+
89
+ # In[ ]:
90
+
91
+
92
+ num_questions = pn.widgets.DiscreteSlider(
93
+ name='Number of Questions',
94
+ options=[5, 10, 15, 20], value=5
95
+ )
96
+ num_questions
97
+
98
+
99
+ # In[ ]:
100
+
101
+
102
+ def question_list(i, df):
103
+
104
+ button_true = pn.widgets.Button(name='True')
105
+ button_false = pn.widgets.Button(name='False')
106
+
107
+ text = pn.widgets.StaticText(value='')
108
+
109
+ def processing_button_true(event):
110
+ if df.correct_answer[i] == 'True':
111
+ text.value = 'Correct!'
112
+ else:
113
+ text.value = 'Incorrect!'
114
+
115
+ def processing_button_false(event):
116
+ if df.correct_answer[i] == 'False':
117
+ text.value = 'Correct!'
118
+ else:
119
+ text.value = 'Incorrect!'
120
+
121
+ button_true.on_click(processing_button_true)
122
+ button_false.on_click(processing_button_false)
123
+ return pn.Column(
124
+ pn.pane.Markdown(f"""
125
+  
126
+ #Question {i+1}:
127
+ ### {df.question[i]}
128
+ """),
129
+
130
+ pn.Row(button_true,button_false),
131
+ text)
132
+
133
+
134
+ # In[ ]:
135
+
136
+
137
+ def get_data_and_questions(num_questions, difficulty, category):
138
+ df = get_data(num_questions, difficulty, category)
139
+ question_pane = [question_list(i, df) for i in range(len(df))]
140
+ trivia_pane = pn.Column(*question_pane)
141
+ return trivia_pane
142
+
143
+
144
+ # In[ ]:
145
+
146
+
147
+ interactive = pn.bind(get_data_and_questions, num_questions, difficulty, category)
148
+
149
+
150
+ # In[ ]:
151
+
152
+
153
+ # Layout using Template
154
+ template = pn.template.FastListTemplate(
155
+ title='Trivia Game',
156
+ sidebar=[num_questions, difficulty, category],
157
+ main=[interactive],
158
+ accent_base_color="#88d8b0",
159
+ header_background="#88d8b0",
160
+ )
161
+ template.servable()
162
+
163
+
164
+ # In[ ]:
165
+
166
+
167
+
168
+