DanielSc4 commited on
Commit
9cbeac4
1 Parent(s): ee9ec43
Files changed (2) hide show
  1. app.py +64 -6
  2. requirements.txt +4 -0
app.py CHANGED
@@ -2,18 +2,76 @@ import gradio as gr
2
 
3
  import pandas as pd
4
  import matplotlib.pyplot as plt
 
 
 
5
 
6
- def greet(name):
7
- pd.read_csv('./data/results.csv', index_col=0)
8
-
9
- return "Hello " + name + "!!"
 
10
 
11
 
12
 
 
 
 
13
 
 
14
 
15
 
 
16
 
 
 
 
 
 
17
 
18
- iface = gr.Interface(fn=greet, inputs="text", outputs="text")
19
- iface.launch()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2
 
3
  import pandas as pd
4
  import matplotlib.pyplot as plt
5
+ import numpy as np
6
+ from sklearn.decomposition import LatentDirichletAllocation
7
+ from sklearn.feature_extraction.text import CountVectorizer
8
 
9
+ def concat_comments(sup_comment: list[str], comment: list[str]) -> list[str]:
10
+ format_s = "{s}\n{c}"
11
+ return [
12
+ format_s.format(s=s, c=c) for s, c in zip(sup_comment, comment)
13
+ ]
14
 
15
 
16
 
17
+ def main(button, chose_context):
18
+ df = pd.read_csv('./data/results.csv', index_col=0)
19
+ print(chose_context)
20
 
21
+ data = concat_comments(df.sup_comment, df.comment)
22
 
23
 
24
+ subreddits = df.subreddit.value_counts().index[:22]
25
 
26
+ weight_counts = {
27
+ t: [
28
+ df[df.Topic_key_word == t].subreddit.value_counts()[subreddit] / df.subreddit.value_counts()[subreddit] for subreddit in subreddits
29
+ ] for t in topics
30
+ }
31
 
32
+ irony_percs = {
33
+ t: [
34
+ len(
35
+ df[df.subreddit == subreddit][(df[df.subreddit == subreddit].Topic_key_word == t) & (df[df.subreddit == subreddit].label == 1)]
36
+ ) /
37
+ len(
38
+ df[df.subreddit == subreddit]
39
+ ) for subreddit in subreddits
40
+ ] for t in topics
41
+ }
42
+ width = 0.9
43
+
44
+ fig, ax = plt.subplots(figsize = (10, 7))
45
+ plt.axhline(0.5, color = 'red', ls=":", alpha = .3)
46
+
47
+ bottom = np.zeros(len(subreddits))
48
+
49
+ for k, v in weight_counts.items():
50
+ p = ax.bar(subreddits, v, width, label=k, bottom=bottom)
51
+ ax.bar(subreddits, irony_percs[k], width - 0.01, bottom=bottom, color = 'black', edgecolor = 'white', alpha = .2, hatch = '\\')
52
+ bottom += v
53
+
54
+ ax.set_title("Perc of topics for each subreddit")
55
+ ax.legend(loc="upper right")
56
+ plt.xticks(rotation=70)
57
+
58
+ plt.show()
59
+
60
+
61
+ with gr.Blocks() as demo:
62
+ button = gr.Radio(
63
+ label="Plot type",
64
+ choices=['scatter_plot', 'heatmap', 'us_map', 'interactive_barplot', "radial", "multiline"], value='scatter_plot'
65
+ )
66
+ chose_context = gr.Radio(
67
+ label="Context LDA",
68
+ choices=['comment', 'sup comment', 'sup comment + comment'], value='scatter_plot'
69
+ )
70
+ plot = gr.Plot(label="Plot")
71
+ button.change(main, inputs=[button, chose_context], outputs=[plot])
72
+ demo.load(main, inputs=[button], outputs=[plot])
73
+
74
+
75
+ # iface = gr.Interface(fn=greet, inputs="text", outputs="text")
76
+ if __name__ == "__main__":
77
+ demo.launch()
requirements.txt CHANGED
@@ -0,0 +1,4 @@
 
 
 
 
 
1
+ nltk
2
+ spacy
3
+ gensim
4
+ sklearn