Akjava commited on
Commit
97a93cd
1 Parent(s): a6fc944
Files changed (6) hide show
  1. .gitignore +1 -0
  2. app.py +154 -0
  3. demo_footer.html +3 -0
  4. demo_header.html +16 -0
  5. demo_tools.html +6 -0
  6. requirements.txt +4 -0
.gitignore ADDED
@@ -0,0 +1 @@
 
 
1
+ __pycache__
app.py ADDED
@@ -0,0 +1,154 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import spaces
2
+ import gradio as gr
3
+ import numpy as np
4
+ import pandas as pd
5
+ import time
6
+
7
+ css="""
8
+ #col-left {
9
+ margin: 0 auto;
10
+ max-width: 640px;
11
+ }
12
+ #col-right {
13
+ margin: 0 auto;
14
+ max-width: 640px;
15
+ }
16
+ .grid-container {
17
+ display: flex;
18
+ align-items: center;
19
+ justify-content: center;
20
+ gap:10px
21
+ }
22
+
23
+ .image {
24
+ width: 128px;
25
+ height: 128px;
26
+ object-fit: cover;
27
+ }
28
+
29
+ .text {
30
+ font-size: 16px;
31
+ }
32
+ """
33
+ emotion_columns = ['admiration', 'amusement', 'anger', 'annoyance', 'approval',
34
+ 'caring', 'confusion', 'curiosity', 'desire', 'disappointment',
35
+ 'disapproval', 'disgust', 'embarrassment', 'excitement', 'fear',
36
+ 'gratitude', 'grief', 'joy', 'love', 'nervousness', 'optimism',
37
+ 'pride', 'realization', 'relief', 'remorse', 'sadness', 'surprise','neutral']
38
+
39
+ def load_text(text_path: str) -> str:
40
+ with open(text_path, 'r', encoding='utf-8') as f:
41
+ text = f.read()
42
+
43
+ return text
44
+
45
+ def select_checkbox(name):
46
+ if name =="All":
47
+ return gr.CheckboxGroup(value=emotion_columns)
48
+ elif name =="None":
49
+ return []
50
+ elif name =="Positive":
51
+ return ["admiration","amusement","approval","caring","curiosity","desire","excitement","gratitude","joy","love","optimism","pride","relief"]
52
+ elif name =="Negative":
53
+ return ["anger","annoyance","disappointment","disapproval","disgust","fear","grief","embarrassment","remorse","sadness"]
54
+ else:
55
+ return ["confusion","nervousness","neutral","realization","surprise"]
56
+ def process_datas(checked_emotions,mode="filter",max_data=100,skip_data=0):
57
+ checked_emotions = sorted(checked_emotions)
58
+
59
+ df = pd.read_parquet("hf://datasets/google-research-datasets/go_emotions/raw/train-00000-of-00001.parquet")
60
+
61
+
62
+ def filter_emotions(emotions):
63
+ unchecked = emotion_columns.copy()
64
+ condition_checked = np.all(df[emotions] == 1, axis=1)
65
+ for emotion in checked_emotions:
66
+ unchecked.remove(emotion)
67
+ condition_unchecked = np.all(df[unchecked] == 0, axis=1)
68
+ filtered_df = df[condition_checked & condition_unchecked]
69
+ return filtered_df
70
+
71
+ def df_to_text(df):
72
+ df = df.iloc[skip_data:]
73
+ if len(filtered_df) == 0:
74
+ return ""
75
+ texts=(df.head(max_data)[['text']].to_string(index=False,max_colwidth=None))
76
+ trimmed_texts = [line.strip() for line in texts.split('\n')[1:] if line.strip()]
77
+ return "\n".join(trimmed_texts)
78
+
79
+ if mode == "filter":
80
+ filtered_df = filter_emotions(checked_emotions)
81
+ count = (len(filtered_df))
82
+ trimmed_texts = df_to_text(filtered_df)
83
+
84
+ last_count = min(count,(skip_data+max_data))
85
+ label = f"{skip_data+1} - {last_count} of {count}"
86
+
87
+ label_texts = [f"[{emotion}]" for emotion in checked_emotions]
88
+
89
+ output_text = "+".join(label_texts)+"\n"+trimmed_texts
90
+ output_label = label
91
+ else:
92
+ max_data = max(1,int(max_data/len(checked_emotions)))
93
+ text_arrays = []
94
+ for emotion in checked_emotions:
95
+ text_arrays.append(f"[{emotion}]")
96
+ filtered_df = filter_emotions([emotion])
97
+ trimmed_texts = df_to_text(filtered_df)
98
+ text_arrays.append(trimmed_texts)
99
+ text_arrays.append("\n")
100
+ print(text_arrays)
101
+ output_text = "\n".join(text_arrays)
102
+ output_label = f"{len(checked_emotions)} x {max_data}"
103
+
104
+
105
+ return output_text,output_label,",".join(checked_emotions)
106
+
107
+
108
+ with gr.Blocks(css=css, elem_id="demo-container") as demo:
109
+ with gr.Column():
110
+ gr.HTML(load_text("demo_header.html"))
111
+ gr.HTML(load_text("demo_tools.html"))
112
+ with gr.Row():
113
+ with gr.Column():
114
+ with gr.Row(equal_height=True):
115
+ mode_group = gr.Radio(choices=["filter","list"],label="Mode",value="filter")
116
+ selection_name = gr.Dropdown(label="Select",choices=["All","None","Positive","Negative","Neutral"],value="All")
117
+ selection_btn= gr.Button("Update Selection")
118
+ checkbox_group = gr.CheckboxGroup(choices=emotion_columns,label="Emotions",value=["love"])
119
+ btn= gr.Button("View Data",variant="primary")
120
+ with gr.Row():
121
+ max_data = gr.Slider(
122
+ label="Max Data",
123
+ minimum=0,
124
+ maximum=540,
125
+ step=10,
126
+ value=50,info="returning large data is heavy action,if you need more copy the space")
127
+ skip_data = gr.Slider(
128
+ label="Skip Data",
129
+ minimum=0,
130
+ maximum=100000,
131
+ step=1,
132
+ value=0)
133
+ with gr.Column():
134
+ with gr.Row():
135
+ data_size = gr.Textbox(label="Data Count",scale=1)
136
+ checked_size = gr.Textbox(label="Checked",scale=2)
137
+
138
+ text_out = gr.TextArea(label="Output", elem_id="output-text")
139
+
140
+ btn.click(fn=process_datas, inputs=[checkbox_group,mode_group,max_data,skip_data], outputs =[text_out,data_size,checked_size], api_name='infer')
141
+ selection_btn.click(fn=select_checkbox,inputs=[selection_name],outputs=[checkbox_group])
142
+ gr.Examples(
143
+ examples=[
144
+
145
+ ],
146
+ inputs=[],
147
+
148
+ )
149
+ gr.HTML(
150
+ gr.HTML(load_text("demo_footer.html"))
151
+ )
152
+
153
+ if __name__ == "__main__":
154
+ demo.launch()
demo_footer.html ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ <div>
2
+ <P> Images are generated with <a href="https://huggingface.co/black-forest-labs/FLUX.1-schnell">FLUX.1-schnell</a> and licensed under <a href="http://www.apache.org/licenses/LICENSE-2.0">the Apache 2.0 License</a>
3
+ </div>
demo_header.html ADDED
@@ -0,0 +1,16 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <div style="text-align: center;">
2
+ <h1>
3
+ Simple GoEmotions Viewer
4
+ </h1>
5
+ <div class="grid-container">
6
+ <img src="https://akjava.github.io/AIDiagramChatWithVoice-FaceCharacter/webp/128/00350245.webp" alt="Webp-talk-head" class="image">
7
+
8
+ <p class="text">
9
+ This Space use <a href="http://www.apache.org/licenses/LICENSE-2.0">the Apache 2.0</a> Licensed <a href="https://huggingface.co/datasets/google-research-datasets/go_emotions">GoEmotions</a> <br>
10
+ It is clear to me that this license applies to the annotated data. <br>
11
+ However, I am uncertain whether the license extends to the Reddit comments as well.<br>
12
+
13
+ </p>
14
+ </div>
15
+
16
+ </div>
demo_tools.html ADDED
@@ -0,0 +1,6 @@
 
 
 
 
 
 
 
1
+ <div style="text-align: center;">
2
+ <p>
3
+
4
+ </p>
5
+ <p></p>
6
+ </div>
requirements.txt ADDED
@@ -0,0 +1,4 @@
 
 
 
 
 
1
+ numpy
2
+ panda
3
+ spaces
4
+