aokitools commited on
Commit
7ad874f
·
1 Parent(s): b52186e
Files changed (2) hide show
  1. app.py +186 -0
  2. requirements.txt +7 -0
app.py CHANGED
@@ -427,6 +427,171 @@ def f_formatunicode(e_text1, e_radio1):
427
 
428
  # // ---------------------------------------------- // #
429
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
430
  import gradio as gr
431
 
432
  with gr.Blocks() as sample01:
@@ -576,6 +741,27 @@ with gr.Blocks() as sample01:
576
  pass
577
 
578
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
579
  pass
580
 
581
  sample01.queue()
 
427
 
428
  # // ---------------------------------------------- // #
429
 
430
+ def f_tsne(e_text1, e_checkbox1, e_radio1, e_radio2):
431
+ result = {}
432
+ result["output1"] = e_text1
433
+ result["output2"] = None
434
+ result["output3"] = ""
435
+
436
+ try:
437
+ s1 = e_text1
438
+
439
+ n_dimension = 3
440
+
441
+ if e_radio1 == "3D":
442
+ n_dimension = 3
443
+ pass
444
+ else:
445
+ n_dimension = 2
446
+ pass
447
+
448
+ list_vectors = []
449
+ list_groups = []
450
+ list_labels = []
451
+
452
+ list1 = s1.splitlines()
453
+
454
+ import random
455
+ random.shuffle(list1)
456
+
457
+ index1 = 0
458
+ for i1 in list1[:1000]:
459
+ i1 = list1[index1].strip()
460
+
461
+ if len(i1) == 0:
462
+ continue
463
+ pass
464
+
465
+ ss1 = i1.split()
466
+
467
+ s1 = None
468
+ s2 = None
469
+
470
+ if e_checkbox1:
471
+ s1 = ss1[0]
472
+ s2 = " ".join(ss1[1:])
473
+ pass
474
+ else:
475
+ s1 = ""
476
+ s2 = " ".join(ss1[0:])
477
+ pass
478
+
479
+ import io
480
+ buf = io.StringIO(s2)
481
+
482
+ import numpy
483
+ l1 = numpy.loadtxt(buf, delimiter=" ")
484
+
485
+ s3 = "0"
486
+
487
+ list_groups.append(s3)
488
+ list_labels.append(s1)
489
+ list_vectors.append(l1)
490
+
491
+ index1 += 1
492
+ pass
493
+
494
+ import numpy
495
+ X = numpy.array(list_vectors)
496
+
497
+ tsne = None
498
+
499
+ if e_radio2 == "TSNE":
500
+ from sklearn.manifold import TSNE
501
+ tsne = TSNE(
502
+ n_components=n_dimension,
503
+ init='random',
504
+ method='barnes_hut',
505
+ n_iter=1000,
506
+ verbose=2
507
+ ).fit_transform(X)
508
+ pass
509
+ else:
510
+ import umap
511
+ from scipy.sparse.csgraph import connected_components
512
+
513
+ tsne = umap.UMAP(
514
+ n_components=n_dimension,
515
+ init='random',
516
+ verbose=2
517
+ ).fit_transform(X)
518
+ pass
519
+
520
+ if n_dimension == 3:
521
+ x = tsne[:,0]
522
+ y = tsne[:,1]
523
+ z = tsne[:,2]
524
+
525
+ l1 = []
526
+ for i1 in range(len(x)):
527
+ s1 = "{0:.3f} {1:.3f} {2:.3f}".format(x[i1], y[i1], z[i1])
528
+ l1.append(s1)
529
+ pass
530
+ result["output3"] = "\n".join(l1)
531
+ pass
532
+ else:
533
+ x = tsne[:,0]
534
+ y = tsne[:,1]
535
+
536
+ l1 = []
537
+ for i1 in range(len(x)):
538
+ s1 = "{0:.3f} {1:.3f}".format(x[i1], y[i1])
539
+ l1.append(s1)
540
+ pass
541
+ result["output3"] = "\n".join(l1)
542
+ pass
543
+
544
+ import plotly.graph_objects as go
545
+ trace1 = None
546
+ if n_dimension == 3:
547
+ trace1 = go.Scatter3d(
548
+ x=tsne[:,0],
549
+ y=tsne[:,1],
550
+ z=tsne[:,2],
551
+ text=list_labels,
552
+ mode="markers",
553
+ marker=dict(
554
+ sizemode="diameter",
555
+ color = "rgb(0, 0, 255)",
556
+ colorscale = "Portland",
557
+ line=dict(color="rgb(255, 255, 255)"),
558
+ opacity=0.9,
559
+ size=2
560
+ )
561
+ )
562
+ pass
563
+ else:
564
+ trace1 = go.Scatter(
565
+ x=tsne[:,0],
566
+ y=tsne[:,1],
567
+ text=list_labels,
568
+ mode="markers",
569
+ marker=dict(
570
+ sizemode="diameter",
571
+ color = "rgb(0, 0, 255)",
572
+ colorscale = "Portland",
573
+ line=dict(color="rgb(255, 255, 255)"),
574
+ opacity=0.9,
575
+ size=10
576
+ )
577
+ )
578
+ pass
579
+ data=[trace1]
580
+ fig = go.Figure(data=data)
581
+ result["output2"] = fig
582
+ pass
583
+ except Exception as e:
584
+ result["output1"] = "ERROR: {0}".format(e)
585
+
586
+ import traceback
587
+ print(traceback.format_exc())
588
+ pass
589
+
590
+ return (result["output1"], result["output2"], result["output3"])
591
+ pass
592
+
593
+ # // ---------------------------------------------- // #
594
+
595
  import gradio as gr
596
 
597
  with gr.Blocks() as sample01:
 
741
  pass
742
 
743
 
744
+ with gr.Tab("次元削減"):
745
+ e_plot1 = gr.Plot()
746
+ e_button1 = gr.Button("プロット")
747
+ e_radio1 = gr.Radio(["2D", "3D"], value="2D", label="dimension")
748
+ e_radio2 = gr.Radio(["TSNE", "UMAP"], value="TSNE", label="algorithm")
749
+ e_checkbox1 = gr.Checkbox(label="1カラム目をラベルとして設定")
750
+ e_text1 = gr.TextArea(show_label=True, label="INPUT VECTOR")
751
+
752
+
753
+ gr.Markdown("""
754
+ フォーマット
755
+ - ベクトル数値列
756
+ - 空白区切り
757
+ - 31行以上~1000行まで
758
+ """)
759
+
760
+ e_text2 = gr.TextArea(show_label=True, label="OUTPUT VECTOR")
761
+ e_button1.click(f_tsne, inputs=[e_text1, e_checkbox1, e_radio1, e_radio2], outputs=[e_text1, e_plot1, e_text2])
762
+ pass
763
+
764
+
765
  pass
766
 
767
  sample01.queue()
requirements.txt CHANGED
@@ -2,5 +2,12 @@ janome
2
  lxml
3
  jsbeautifier
4
  jaconv
 
 
 
 
 
 
 
5
 
6
 
 
2
  lxml
3
  jsbeautifier
4
  jaconv
5
+ plotly
6
+ numpy
7
+ sklearn
8
+ umap-learn
9
+ scipy
10
+
11
+
12
 
13