aokitools commited on
Commit
20e2df3
·
1 Parent(s): af3ebb2
Files changed (1) hide show
  1. app.py +382 -0
app.py ADDED
@@ -0,0 +1,382 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ 
2
+ print("START")
3
+
4
+ # // ---------------------------------------------- // #
5
+
6
+ def f_base64_encode(s1):
7
+ try:
8
+ import base64
9
+ result = base64.b64encode(s1.encode()).decode()
10
+ pass
11
+ except Exception as e:
12
+ result = "ERROR: {0}".format(e)
13
+
14
+ import traceback
15
+ print(traceback.format_exc())
16
+ pass
17
+
18
+ return result
19
+ pass
20
+
21
+ def f_base64_decode(s1):
22
+ try:
23
+ import base64
24
+ result = base64.b64decode(s1.encode()).decode()
25
+ pass
26
+ except Exception as e:
27
+ result = "ERROR: {0}".format(e)
28
+
29
+ import traceback
30
+ print(traceback.format_exc())
31
+ pass
32
+
33
+ return result
34
+ pass
35
+
36
+ # // ---------------------------------------------- // #
37
+
38
+ def f_json2jsonlines(s1):
39
+ try:
40
+ import json
41
+ d1 = json.loads(s1)
42
+ result = json.dumps(d1, ensure_ascii=False)
43
+ pass
44
+ except Exception as e:
45
+ result = "ERROR: {0}".format(e)
46
+
47
+ import traceback
48
+ print(traceback.format_exc())
49
+ pass
50
+
51
+ return result
52
+ pass
53
+
54
+ def f_jsonlines2json(s1):
55
+ try:
56
+ l1 = s1.splitlines()
57
+ l2 = []
58
+ for i1 in l1:
59
+ s2 = None
60
+
61
+ try:
62
+ import json
63
+ d1 = json.loads(i1)
64
+ s2 = json.dumps(d1, ensure_ascii=False, indent=2)
65
+ pass
66
+ except Exception as e:
67
+ s2 = "ERROR: {0}".format(e)
68
+
69
+ import traceback
70
+ print(traceback.format_exc())
71
+ pass
72
+
73
+ l2.append(s2)
74
+ pass
75
+
76
+ result = "\n".join(l2)
77
+ pass
78
+ except Exception as e:
79
+ result = "ERROR: {0}".format(e)
80
+
81
+ import traceback
82
+ print(traceback.format_exc())
83
+ pass
84
+
85
+ return result
86
+ pass
87
+
88
+
89
+ # // ---------------------------------------------- // #
90
+
91
+ def f_sort(s1, e_radio1, e_checkbox1, e_checkbox2):
92
+ try:
93
+ b_sort1 = False
94
+ if e_radio1 == "降順":
95
+ b_sort1 = True
96
+ pass
97
+
98
+ l1 = s1.splitlines()
99
+
100
+ if e_checkbox2:
101
+ l1 = map(lambda x: x.strip(), l1)
102
+ pass
103
+
104
+ if e_checkbox1:
105
+ l1 = list(set(l1))
106
+ pass
107
+
108
+ l1.sort(reverse=b_sort1)
109
+
110
+ result = "\n".join(l1)
111
+ pass
112
+ except Exception as e:
113
+ result = "ERROR: {0}".format(e)
114
+
115
+ import traceback
116
+ print(traceback.format_exc())
117
+ pass
118
+
119
+ return result
120
+ pass
121
+
122
+ # // ---------------------------------------------- // #
123
+
124
+ def f_hash(s1):
125
+ result = {}
126
+
127
+ try:
128
+ import hashlib
129
+
130
+ s2 = hashlib.md5(s1.encode()).hexdigest()
131
+ pass
132
+ except Exception as e:
133
+ s2 = "ERROR: {0}".format(e)
134
+
135
+ import traceback
136
+ print(traceback.format_exc())
137
+ pass
138
+ result["e_output1"] = s2
139
+
140
+ try:
141
+ import hashlib
142
+
143
+ s2 = hashlib.sha1(s1.encode()).hexdigest()
144
+ pass
145
+ except Exception as e:
146
+ s2 = "ERROR: {0}".format(e)
147
+
148
+ import traceback
149
+ print(traceback.format_exc())
150
+ pass
151
+ result["e_output2"] = s2
152
+
153
+ try:
154
+ import hashlib
155
+
156
+ s2 = hashlib.sha256(s1.encode()).hexdigest()
157
+ pass
158
+ except Exception as e:
159
+ s2 = "ERROR: {0}".format(e)
160
+
161
+ import traceback
162
+ print(traceback.format_exc())
163
+ pass
164
+ result["e_output3"] = s2
165
+
166
+ try:
167
+ import hashlib
168
+
169
+ s2 = hashlib.sha512(s1.encode()).hexdigest()
170
+ pass
171
+ except Exception as e:
172
+ s2 = "ERROR: {0}".format(e)
173
+
174
+ import traceback
175
+ print(traceback.format_exc())
176
+ pass
177
+ result["e_output4"] = s2
178
+
179
+ return (result["e_output1"], result["e_output2"], result["e_output3"], result["e_output4"])
180
+ pass
181
+
182
+ # // ---------------------------------------------- // #
183
+
184
+ def f_janome(s1):
185
+
186
+ try:
187
+ b_sort1 = False
188
+ if e_radio1 == "降順":
189
+ b_sort1 = True
190
+ pass
191
+
192
+ l1 = s1.splitlines()
193
+ l2 = []
194
+
195
+ from janome.tokenizer import Tokenizer
196
+ tokenizer = Tokenizer()
197
+ for i1 in l1:
198
+ for token in tokenizer.tokenize(i1):
199
+ d1 = {}
200
+ d1["token"] = "{0}".format(token.surface)
201
+ d1["label"] = "{0}".format(token.part_of_speech.split(",")[0])
202
+
203
+ s3 = "{0}".format(token.reading)
204
+
205
+ if s3 == "*":
206
+ s3 = d1["token"]
207
+ pass
208
+
209
+ d1["reading"] = s3
210
+
211
+ import json
212
+ s2 = json.dumps(d1, ensure_ascii=False)
213
+
214
+ l2.append(s2)
215
+ pass
216
+ pass
217
+
218
+ result = "\n".join(l2)
219
+ pass
220
+ except Exception as e:
221
+ result = "ERROR: {0}".format(e)
222
+
223
+ import traceback
224
+ print(traceback.format_exc())
225
+ pass
226
+
227
+ return result
228
+ pass
229
+
230
+ # // ---------------------------------------------- // #
231
+
232
+ def f_generatepassword(e_number1, e_checkbox1, e_checkbox2, e_checkbox3, e_checkbox4, e_slider1):
233
+
234
+ try:
235
+ l2 = []
236
+
237
+ import string
238
+
239
+ n1 = int(e_number1)
240
+ n2 = int(e_slider1)
241
+
242
+ characters = ""
243
+
244
+ if e_checkbox1:
245
+ characters += string.digits
246
+ pass
247
+
248
+ if e_checkbox2:
249
+ characters += string.ascii_lowercase
250
+ pass
251
+
252
+ if e_checkbox3:
253
+ characters += string.ascii_uppercase
254
+ pass
255
+
256
+ if e_checkbox4:
257
+ characters += "!#$%&()*+,-./:;<=>?@[]^_{|}~."
258
+ pass
259
+
260
+ import random
261
+ for i1 in range(n2):
262
+ s2 = ""
263
+ if len(characters) > 0:
264
+ l1 = [random.choice(characters) for i2 in range(n1)]
265
+ s2 = "".join(l1)
266
+ pass
267
+
268
+ l2.append(s2)
269
+ pass
270
+
271
+ result = "\n".join(l2)
272
+ pass
273
+ except Exception as e:
274
+ result = "ERROR: {0}".format(e)
275
+
276
+ import traceback
277
+ print(traceback.format_exc())
278
+ pass
279
+
280
+ return result
281
+ pass
282
+
283
+ # // ---------------------------------------------- // #
284
+
285
+ import gradio as gr
286
+
287
+ with gr.Blocks() as sample01:
288
+ gr.Markdown("aokitools")
289
+
290
+ with gr.Tab("パスワード生成"):
291
+ e_number1 = gr.Number(value=8, minimum=1, maximum=1024, label="桁数")
292
+
293
+ e_checkbox1 = gr.Checkbox(value=True, label="数字を含める")
294
+ e_checkbox2 = gr.Checkbox(value=True, label="小文字を含める")
295
+ e_checkbox3 = gr.Checkbox(value=True, label="大文字を含める")
296
+ e_checkbox4 = gr.Checkbox(value=False, label="記号を含める")
297
+
298
+ e_slider1 = gr.Slider(value=100, minimum=100, maximum=10000, step=100, label="生成する数", interactive=True)
299
+
300
+ e_output = gr.TextArea(show_label=True, label="TEXT")
301
+
302
+ with gr.Row():
303
+ e_button1 = gr.Button("生成")
304
+ e_button1.click(f_generatepassword, inputs=[e_number1, e_checkbox1, e_checkbox2, e_checkbox3, e_checkbox4, e_slider1], outputs=[e_output])
305
+ pass
306
+ pass
307
+
308
+ with gr.Tab("JSON整形"):
309
+ e_input = gr.TextArea(show_label=True, label="JSON LINES")
310
+ e_output = gr.TextArea(show_label=True, label="JSON")
311
+
312
+ with gr.Row():
313
+ e_button1 = gr.Button("JSON に変換")
314
+ e_button1.click(f_jsonlines2json, inputs=[e_input], outputs=[e_output])
315
+
316
+ e_button1 = gr.Button("JSON LINES に変換")
317
+ e_button1.click(f_json2jsonlines, inputs=[e_output], outputs=[e_input])
318
+ pass
319
+ pass
320
+
321
+ with gr.Tab("並べ替え/重複排除"):
322
+ e_input = gr.TextArea(show_label=True, label="TEXT")
323
+ e_checkbox1 = gr.Checkbox(label="重複排除")
324
+ e_checkbox2 = gr.Checkbox(label="前後の空白を除去")
325
+ e_radio1 = gr.Radio(["昇順", "降順"], value="昇順", label="sort order")
326
+
327
+ with gr.Row():
328
+ e_button1 = gr.Button("並べ替え")
329
+ e_button1.click(f_sort, inputs=[e_input, e_radio1, e_checkbox1, e_checkbox2], outputs=[e_input])
330
+ pass
331
+ pass
332
+
333
+
334
+ with gr.Tab("形態素解析"):
335
+ e_input = gr.TextArea(show_label=True, label="TEXT")
336
+ e_output = gr.TextArea(show_label=True, label="JSON")
337
+
338
+ with gr.Row():
339
+ e_button1 = gr.Button("形態素解析")
340
+ e_button1.click(f_janome, inputs=[e_input], outputs=[e_output])
341
+ pass
342
+ pass
343
+
344
+
345
+ with gr.Tab("hash"):
346
+ e_input = gr.TextArea(show_label=True, label="TEXT")
347
+
348
+ e_output1 = gr.Textbox(show_label=True, label="MD5")
349
+ e_output2 = gr.Textbox(show_label=True, label="SHA1")
350
+ e_output3 = gr.Textbox(show_label=True, label="SHA256")
351
+ e_output4 = gr.Textbox(show_label=True, label="SHA512")
352
+
353
+ with gr.Row():
354
+ e_button1 = gr.Button("hashを計算")
355
+ e_button1.click(f_hash, inputs=[e_input], outputs=[e_output1, e_output2, e_output3, e_output4])
356
+ pass
357
+ pass
358
+
359
+
360
+ with gr.Tab("base64"):
361
+ e_input = gr.TextArea(show_label=True, label="TEXT")
362
+ e_output = gr.TextArea(show_label=True, label="BASE64")
363
+
364
+ with gr.Row():
365
+ e_button1 = gr.Button("エンコード text >> base64")
366
+ e_button1.click(f_base64_encode, inputs=[e_input], outputs=[e_output])
367
+
368
+ e_button1 = gr.Button("デコード base64 >> text")
369
+ e_button1.click(f_base64_decode, inputs=[e_output], outputs=[e_input])
370
+ pass
371
+ pass
372
+
373
+
374
+ pass
375
+
376
+ sample01.queue()
377
+ sample01.launch(share=True)
378
+
379
+ # // ---------------------------------------------- // #
380
+
381
+ print("END")
382
+