TRACES veneta commited on
Commit
c1dcf33
·
0 Parent(s):

Duplicate from veneta/preprocessing

Browse files

Co-authored-by: Veneta Kireva <veneta@users.noreply.huggingface.co>

Files changed (6) hide show
  1. .gitattributes +34 -0
  2. README.md +13 -0
  3. __init__.py +0 -0
  4. app.py +87 -0
  5. helper_funcs.py +175 -0
  6. requirements.txt +0 -0
.gitattributes ADDED
@@ -0,0 +1,34 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ *.7z filter=lfs diff=lfs merge=lfs -text
2
+ *.arrow filter=lfs diff=lfs merge=lfs -text
3
+ *.bin filter=lfs diff=lfs merge=lfs -text
4
+ *.bz2 filter=lfs diff=lfs merge=lfs -text
5
+ *.ckpt filter=lfs diff=lfs merge=lfs -text
6
+ *.ftz filter=lfs diff=lfs merge=lfs -text
7
+ *.gz filter=lfs diff=lfs merge=lfs -text
8
+ *.h5 filter=lfs diff=lfs merge=lfs -text
9
+ *.joblib filter=lfs diff=lfs merge=lfs -text
10
+ *.lfs.* filter=lfs diff=lfs merge=lfs -text
11
+ *.mlmodel filter=lfs diff=lfs merge=lfs -text
12
+ *.model filter=lfs diff=lfs merge=lfs -text
13
+ *.msgpack filter=lfs diff=lfs merge=lfs -text
14
+ *.npy filter=lfs diff=lfs merge=lfs -text
15
+ *.npz filter=lfs diff=lfs merge=lfs -text
16
+ *.onnx filter=lfs diff=lfs merge=lfs -text
17
+ *.ot filter=lfs diff=lfs merge=lfs -text
18
+ *.parquet filter=lfs diff=lfs merge=lfs -text
19
+ *.pb filter=lfs diff=lfs merge=lfs -text
20
+ *.pickle filter=lfs diff=lfs merge=lfs -text
21
+ *.pkl filter=lfs diff=lfs merge=lfs -text
22
+ *.pt filter=lfs diff=lfs merge=lfs -text
23
+ *.pth filter=lfs diff=lfs merge=lfs -text
24
+ *.rar filter=lfs diff=lfs merge=lfs -text
25
+ *.safetensors filter=lfs diff=lfs merge=lfs -text
26
+ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
27
+ *.tar.* filter=lfs diff=lfs merge=lfs -text
28
+ *.tflite filter=lfs diff=lfs merge=lfs -text
29
+ *.tgz filter=lfs diff=lfs merge=lfs -text
30
+ *.wasm filter=lfs diff=lfs merge=lfs -text
31
+ *.xz filter=lfs diff=lfs merge=lfs -text
32
+ *.zip filter=lfs diff=lfs merge=lfs -text
33
+ *.zst filter=lfs diff=lfs merge=lfs -text
34
+ *tfevents* filter=lfs diff=lfs merge=lfs -text
README.md ADDED
@@ -0,0 +1,13 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ title: Preprocessing
3
+ emoji: 🔥
4
+ colorFrom: gray
5
+ colorTo: purple
6
+ sdk: gradio
7
+ sdk_version: 3.32.0
8
+ app_file: app.py
9
+ pinned: false
10
+ duplicated_from: veneta/preprocessing
11
+ ---
12
+
13
+ Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
__init__.py ADDED
File without changes
app.py ADDED
@@ -0,0 +1,87 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import pandas as pd
2
+ import gradio as gr
3
+
4
+ from helper_funcs import functions, INPUT_FILE_TYPE, OUTPUT_FILE_TYPE
5
+
6
+
7
+ def run_function(selected_function, file_obj, input_column, output_column, output_type):
8
+ if 'json' in file_obj.name.lower():
9
+ df = pd.read_json(file_obj.name)
10
+ if any([x in file_obj.name.lower() for x in ['csv', 'txt']]):
11
+ df = pd.read_csv(file_obj.name)
12
+
13
+ output_file = 'result' + output_type
14
+
15
+ if input_column not in list(df.columns):
16
+ raise gr.Error("Input column name: such column does not exist in dataframe!")
17
+
18
+ return functions[selected_function](df, input_column, output_column, output_file)
19
+
20
+
21
+ app = gr.Blocks()
22
+
23
+ with app:
24
+ gr.Markdown(
25
+ """
26
+ # Instructions
27
+ 1. Upload CSV file to process.
28
+ 2. Enter the name of the column containing the values to process.
29
+ 3. Enter the name of the column in which to save the output.
30
+ 4. Select function to process the data.
31
+ 5. Click Process.
32
+ """
33
+ )
34
+ with gr.Row():
35
+ with gr.Column():
36
+ gr.Markdown(
37
+ """
38
+ # Input
39
+ """
40
+ )
41
+ file_obj = gr.File(
42
+ label="Input File",
43
+ file_count="single",
44
+ file_types=INPUT_FILE_TYPE
45
+ )
46
+ input_column = gr.Textbox(
47
+ label="Input column name",
48
+ info="Please enter the name of the column you want to process (as it appears in your dataset)",
49
+ lines=1,
50
+ )
51
+ output_column = gr.Textbox(
52
+ label="Output column name",
53
+ info="Please enter the name of the column you want to save the result to",
54
+ lines=1,
55
+ )
56
+ selected_function = gr.Dropdown(
57
+ list(functions.keys()),
58
+ label="Select processing",
59
+ info=""
60
+ )
61
+ output_type = gr.Dropdown(
62
+ list(OUTPUT_FILE_TYPE),
63
+ label="Select the output file type",
64
+ info=""
65
+ )
66
+
67
+ with gr.Column():
68
+ gr.Markdown(
69
+ """
70
+ # Output
71
+ """
72
+ )
73
+ output_dataframe = gr.Dataframe(
74
+ label="Output Data"
75
+ )
76
+ output_csv = gr.File(
77
+ label="Output File",
78
+ file_types=OUTPUT_FILE_TYPE
79
+ )
80
+
81
+ gr.Button("Process").click(
82
+ run_function,
83
+ inputs=[selected_function, file_obj, input_column, output_column, output_type],
84
+ outputs=[output_dataframe, output_csv]
85
+ )
86
+
87
+ app.launch()
helper_funcs.py ADDED
@@ -0,0 +1,175 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import ast
2
+ import warnings
3
+
4
+ import classla
5
+ import pandas as pd
6
+
7
+ classla.download('bg')
8
+ classla_nlp = classla.Pipeline('bg')
9
+
10
+ warnings.filterwarnings('ignore')
11
+
12
+ INPUT_FILE_TYPE = ['.csv', '.json', '.txt']
13
+ OUTPUT_FILE_TYPE = ['.csv', '.xlsx']
14
+
15
+
16
+ def to_output(df, output_file):
17
+ if 'xlsx' in output_file:
18
+ df.to_excel(output_file, index=False)
19
+ if 'csv' in output_file:
20
+ df.to_csv(output_file, index=False)
21
+ return df.head(10), output_file
22
+
23
+
24
+ def remove_duplicates(df, input_column, output_column, output_file):
25
+ df.drop_duplicates(subset=[input_column], inplace=True)
26
+ return to_output(df, output_file)
27
+
28
+
29
+ def remove_links(df, input_column, output_column, output_file):
30
+ link_regex = r'(https?:\/\/(?:www\.|(?!www))[a-zA-Z0-9][a-zA-Z0-9-]+[a-zA-Z0-9]\.[^\s]{2,}|www\.[a-zA-Z0-9][a-zA-Z0-9-]+[a-zA-Z0-9]\.[^\s]{2,}|https?:\/\/(?:www\.|(?!www))[a-zA-Z0-9]+\.[^\s]{2,}|www\.[a-zA-Z0-9]+\.[^\s]{2,})'
31
+
32
+ if input_column != output_column:
33
+ df[output_column] = df[input_column]
34
+
35
+ df_links = df[df[output_column].str.contains(link_regex, regex=True, na=False)]
36
+ df = pd.concat([df, df_links, df_links]).drop_duplicates(keep=False)
37
+ df_links[output_column] = df_links[output_column].str.replace(link_regex, '', regex=True)
38
+ df = pd.concat([df, df_links])
39
+ return to_output(df, output_file)
40
+
41
+
42
+ def remove_emails(df, input_column, output_column, output_file):
43
+ email_regex = r'\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Z|a-z]{2,}\b'
44
+
45
+ if input_column != output_column:
46
+ df[output_column] = df[input_column]
47
+
48
+ df_email = df[df[output_column].str.contains(email_regex, regex=True, na=False)]
49
+ df = pd.concat([df, df_email, df_email]).drop_duplicates(keep=False)
50
+ df_email[output_column] = df_email[output_column].str.replace(email_regex, '<EMAIL>', regex=True)
51
+ df = pd.concat([df, df_email])
52
+ return to_output(df, output_file)
53
+
54
+
55
+ def remove_phones(df, input_column, output_column, output_file):
56
+ phone_regex = r'(?<!\S)(\+|0)[1-9][0-9 \-\(\)]{7,32}'
57
+
58
+ if input_column != output_column:
59
+ df[output_column] = df[input_column]
60
+
61
+ df_phone = df[df[output_column].str.contains(phone_regex, regex=True, na=False)]
62
+ df = pd.concat([df, df_phone, df_phone]).drop_duplicates(keep=False)
63
+ df_phone[output_column] = df_phone[output_column].str.replace(phone_regex, '<PHONE>', regex=True)
64
+ df = pd.concat([df, df_phone])
65
+ return to_output(df, output_file)
66
+
67
+
68
+ def get_sentences(df, input_column, output_column, output_file):
69
+ def split_sentences(input_list=None):
70
+ if input_list is None:
71
+ input_list = []
72
+ temp = []
73
+ res = []
74
+ for idx in range(len(input_list)):
75
+ if input_list[idx][0] <= input_list[idx - 1][0]:
76
+ res.append(" ".join([x[1] for x in temp]))
77
+ temp = []
78
+ temp.append(input_list[idx])
79
+ res.append(" ".join([x[1] for x in temp]))
80
+ res.pop(0) # first element is always [], so it is removed
81
+ return res
82
+
83
+ if input_column != output_column:
84
+ df[output_column] = df[input_column]
85
+
86
+ sentences_separated = []
87
+
88
+ for index in range(df.shape[0]):
89
+ row_nlp = classla_nlp(df.iloc[index][input_column])
90
+ row_result_upos = row_nlp.get('upos')
91
+ row_id = row_nlp.get('id')
92
+ row_text = row_nlp.get('text')
93
+
94
+ row_result = [[row_id[x], row_text[x]] for x in range(len(row_id)) if
95
+ row_result_upos[x] != 'PUNCT'] # filter punctuation
96
+ row_result = split_sentences(input_list=row_result) # splitting messages
97
+
98
+ sentences_separated.append(row_result)
99
+
100
+ df[output_column] = sentences_separated
101
+ return to_output(df, output_file)
102
+
103
+
104
+ def get_classla_ner(df, input_column, output_column, output_file):
105
+ def sentence_classla(sentence_list):
106
+ result_ner = list()
107
+ for sentence in sentence_list:
108
+ current_nlp = classla_nlp(sentence).to_dict()
109
+ sentence_ner = [word['ner'] for word in current_nlp[0][0]]
110
+ result_ner.append(sentence_ner)
111
+ return result_ner
112
+
113
+ df[input_column] = df[input_column].apply(lambda x: ast.literal_eval(x))
114
+
115
+ if input_column != output_column:
116
+ df[output_column] = df[input_column]
117
+
118
+ clarin_classla_result = [sentence_classla(df.iloc[index][input_column]) for index in range(df.shape[0])]
119
+ df[output_column] = [clarin_classla_result[index] for index in range(df.shape[0])]
120
+ return to_output(df, output_file)
121
+
122
+
123
+ def get_classla_all(df, input_column, output_column, output_file):
124
+ def sentence_classla(sentence_list):
125
+ result_all = list()
126
+ for sentence in sentence_list:
127
+ current_nlp = classla_nlp(sentence).to_dict()
128
+ result_all.append(current_nlp)
129
+ return result_all
130
+
131
+ df[input_column] = df[input_column].apply(lambda x: ast.literal_eval(x))
132
+
133
+ if input_column != output_column:
134
+ df[output_column] = df[input_column]
135
+
136
+ clarin_classla_result = [sentence_classla(df.iloc[index][input_column]) for index in range(df.shape[0])]
137
+ df[output_column] = [clarin_classla_result[index] for index in range(df.shape[0])]
138
+ return to_output(df, output_file)
139
+
140
+
141
+ def run_all(df, input_column, output_column, output_file):
142
+ def load_file(output_file):
143
+ df = None
144
+ if 'xlsx' in output_file:
145
+ df = pd.read_excel(output_file)
146
+ if 'csv' in output_file:
147
+ df = pd.read_csv(output_file)
148
+ return df
149
+
150
+ _, _ = remove_duplicates(df, input_column, output_column, output_file)
151
+ df = load_file(output_file)
152
+ _, _ = remove_links(df, input_column, 'removed_links', output_file)
153
+ df = load_file(output_file)
154
+ _, _ = remove_emails(df, 'removed_links', 'removed_emails', output_file)
155
+ df = load_file(output_file)
156
+ _, _ = remove_phones(df, 'removed_emails', 'removed_phones', output_file)
157
+ df = load_file(output_file)
158
+ _, _ = get_sentences(df, 'removed_phones', 'extracted_sentences', output_file)
159
+ df = load_file(output_file)
160
+ _, _ = get_classla_all(df, 'extracted_sentences', 'classla_all', output_file)
161
+ df = load_file(output_file)
162
+ _, _ = get_classla_ner(df, 'extracted_sentences', 'classla_ner', output_file)
163
+ return df.head(10), output_file
164
+
165
+
166
+ functions = {
167
+ 'remove duplicate rows': remove_duplicates,
168
+ 'remove links': remove_links,
169
+ 'remove e-mails': remove_emails,
170
+ 'remove phone numbers': remove_phones,
171
+ 'separate sentences': get_sentences,
172
+ 'Classla NER': get_classla_ner,
173
+ 'Classla full result': get_classla_all,
174
+ 'run all': run_all
175
+ }
requirements.txt ADDED
Binary file (2.72 kB). View file