Flux9665 commited on
Commit
425b806
β€’
1 Parent(s): 1fc013d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +147 -0
app.py CHANGED
@@ -0,0 +1,147 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import json
2
+ import pickle
3
+
4
+ import gradio as gr
5
+ import matplotlib.pyplot as plt
6
+ import numpy as np
7
+
8
+
9
+ def plot_scalar_on_scale(scalar_value, distance_type):
10
+ # Ensure the scalar is within bounds
11
+ scalar_value = np.clip(scalar_value, 0.0, 1.0)
12
+
13
+ # Create a figure and axis
14
+ fig, ax = plt.subplots(figsize=(8, 2))
15
+
16
+ # Create a horizontal gradient (from close to distant)
17
+ gradient = np.linspace(0, 1, 256).reshape(1, -1)
18
+ ax.imshow(gradient, extent=[0, 1, 0, 1], aspect='auto', cmap='viridis_r')
19
+
20
+ # Plot the scalar value as a vertical line
21
+ ax.axvline(x=scalar_value, color='white', lw=5)
22
+
23
+ # Add a dot at the scalar position
24
+ ax.plot(scalar_value, 0.5, 'o', color='white', markersize=16)
25
+
26
+ # Add labels
27
+ ax.text(0, -0.2, 'Close', ha='center', va='center', fontsize=12)
28
+ ax.text(1, -0.2, 'Distant', ha='center', va='center', fontsize=12)
29
+
30
+ # Customize the axis
31
+ ax.set_xticks([]) # Remove x-axis ticks
32
+ ax.set_yticks([]) # Remove y-axis ticks
33
+ ax.set_xlim(0, 1)
34
+ ax.set_ylim(0, 1)
35
+ ax.set_title(distance_type)
36
+
37
+ # Remove spines for a cleaner look
38
+ ax.spines['top'].set_visible(False)
39
+ ax.spines['right'].set_visible(False)
40
+ ax.spines['bottom'].set_visible(False)
41
+ ax.spines['left'].set_visible(False)
42
+
43
+ return fig
44
+
45
+ # Show the plot
46
+ # plt.tight_layout()
47
+ # plt.show()
48
+
49
+
50
+ def load_json_from_path(path):
51
+ with open(path, "r", encoding="utf8") as f:
52
+ obj = json.loads(f.read())
53
+
54
+ return obj
55
+
56
+
57
+ class Measurer:
58
+ def __init__(self):
59
+
60
+ # learned dist
61
+ tree_lookup_path = "lang_1_to_lang_2_to_learned_dist.json"
62
+ self.learned_dist_func = load_json_from_path(tree_lookup_path)
63
+
64
+ # tree dist
65
+ tree_lookup_path = "lang_1_to_lang_2_to_tree_dist.json"
66
+ self.tree_dist_func = load_json_from_path(tree_lookup_path)
67
+
68
+ # map dist
69
+ map_lookup_path = "lang_1_to_lang_2_to_map_dist.json"
70
+ self.map_dist_func = load_json_from_path(map_lookup_path)
71
+ largest_value_map_dist = 0.0
72
+ for _, values in self.map_dist_func.items():
73
+ for _, value in values.items():
74
+ largest_value_map_dist = max(largest_value_map_dist, value)
75
+ for key1 in self.map_dist_func:
76
+ for key2 in self.map_dist_func[key1]:
77
+ self.map_dist_func[key1][key2] = self.map_dist_func[key1][key2] / largest_value_map_dist
78
+
79
+ # ASP
80
+ asp_dict_path = "asp_dict.pkl"
81
+ with open(asp_dict_path, 'rb') as dictfile:
82
+ asp_sim = pickle.load(dictfile)
83
+ lang_list = list(asp_sim.keys())
84
+ self.asp_dist_func = dict()
85
+ seen_langs = set()
86
+ for lang_1 in lang_list:
87
+ if lang_1 not in seen_langs:
88
+ seen_langs.add(lang_1)
89
+ self.asp_dist_func[lang_1] = dict()
90
+ for index, lang_2 in enumerate(lang_list):
91
+ if lang_2 not in seen_langs: # it's symmetric
92
+ self.asp_dist_func[lang_1][lang_2] = 1 - asp_sim[lang_1][index]
93
+
94
+ def get_dists(self, l1, l2):
95
+ if l1 in self.tree_dist_func:
96
+ tree_dist = self.tree_dist_func[l1][l2]
97
+ else:
98
+ tree_dist = self.tree_dist_func[l2][l1]
99
+ if l1 in self.map_dist_func:
100
+ map_dist = self.map_dist_func[l1][l2]
101
+ else:
102
+ map_dist = self.map_dist_func[l2][l1]
103
+ if l1 in self.asp_dist_func:
104
+ asp_dist = self.asp_dist_func[l1][l2]
105
+ else:
106
+ asp_dist = self.asp_dist_func[l2][l1]
107
+ if l1 in self.learned_dist_func:
108
+ learned_dist = self.learned_dist_func[l1][l2]
109
+ else:
110
+ learned_dist = self.learned_dist_func[l2][l1]
111
+ return tree_dist, map_dist, asp_dist, learned_dist
112
+
113
+ def measure(self, l1, l2):
114
+ if l1 == l2:
115
+ f1 = plot_scalar_on_scale(0.0, "Language Family Tree Distance")
116
+ f2 = plot_scalar_on_scale(0.0, "Distance on the Globe")
117
+ f3 = plot_scalar_on_scale(0.0, "Distance between Phoneme-Sets")
118
+ f4 = plot_scalar_on_scale(0.0, "Learned Distance Function")
119
+ else:
120
+ tree_dist, map_dist, asp_dist, learned_dist = self.get_dists(l1.split(" ")[-1].split("(")[1].split(")")[0],
121
+ l2.split(" ")[-1].split("(")[1].split(")")[0])
122
+ f1 = plot_scalar_on_scale(tree_dist, "Language Family Tree Distance")
123
+ f2 = plot_scalar_on_scale(map_dist, "Distance on the Globe")
124
+ f3 = plot_scalar_on_scale(asp_dist, "Distance between Phoneme-Sets")
125
+ f4 = plot_scalar_on_scale(learned_dist, "Learned Distance Function")
126
+
127
+ return f1, f2, f3, f4
128
+
129
+
130
+ if __name__ == '__main__':
131
+ m = Measurer()
132
+ iso_to_name = load_json_from_path("iso_to_fullname.json")
133
+ text_selection = [f"{iso_to_name[iso_code]} ({iso_code})" for iso_code in iso_to_name]
134
+ iface = gr.Interface(fn=m.measure,
135
+ inputs=[gr.Dropdown(text_selection,
136
+ type="value",
137
+ value='English (eng)',
138
+ label="Select the fist Language (type on your keyboard to find it quickly)"),
139
+ gr.Dropdown(text_selection,
140
+ type="value",
141
+ value='German (deu)',
142
+ label="Select the second Language (type on your keyboard to find it quickly)")],
143
+ outputs=[gr.Plot(),
144
+ gr.Plot(),
145
+ gr.Plot(),
146
+ gr.Plot()])
147
+ iface.launch()