Spaces:
Runtime error
Runtime error
Create process.py
Browse files- process.py +189 -0
process.py
ADDED
@@ -0,0 +1,189 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import matplotlib.pyplot as plt
|
2 |
+
import streamlit as st
|
3 |
+
import seaborn as sns
|
4 |
+
from interact_with_llm import Agent
|
5 |
+
import json
|
6 |
+
import numpy as np
|
7 |
+
|
8 |
+
|
9 |
+
agent = Agent()
|
10 |
+
QUESTION_ANSWER_MAP = {
|
11 |
+
"Question1: Are you usually?": {
|
12 |
+
"A 'Good Mixer with groups of people": "Extrovert",
|
13 |
+
"Rather quiet and reserved": "Introvert"
|
14 |
+
},
|
15 |
+
"Question2: Among your friends, you are?": {
|
16 |
+
"Full of news about everybody": "Extrovert",
|
17 |
+
"One of the last to hear what is going on": "Introvert"
|
18 |
+
},
|
19 |
+
"Question3: In doing something that many other people do, you would rather?": {
|
20 |
+
"Invent a way of your own": "Intuition",
|
21 |
+
"Do it in the accepted way": "Sensing"
|
22 |
+
},
|
23 |
+
"Question4: Do you admire the people who are?": {
|
24 |
+
"Normal-acting to never make themselves the center of attention": "Sensing",
|
25 |
+
"Too original and individual to care whether they are the center of attention or not": "Intuition"
|
26 |
+
},
|
27 |
+
"Question5: Do you more often let?": {
|
28 |
+
"Your heart rule your head": "Feeling",
|
29 |
+
"Your head rule your heart": "Thinking"
|
30 |
+
},
|
31 |
+
"Question6: Do you usually?": {
|
32 |
+
"Value emotion more than logic": "Feeling",
|
33 |
+
"Value logic more than feelings": "Thinking"
|
34 |
+
},
|
35 |
+
"Question7: When you go somewhere for the day, you would rather": {
|
36 |
+
"Plan what you will do and when": "Judging",
|
37 |
+
"Just go": "Perceiving"
|
38 |
+
},
|
39 |
+
"Question8: When you have a special job to do, you like to": {
|
40 |
+
"Organize it carefully before you start": "Judging",
|
41 |
+
"Find out what is necessary as you go along": "Perceiving"
|
42 |
+
}
|
43 |
+
}
|
44 |
+
final = {"E_I": 0, "S_N":0, "T_F":0, "J_P":0 }
|
45 |
+
personality_map = {"Extrovert":("E_I", 0), "Introvert":("E_I", 50),
|
46 |
+
"Sensing":("S_N", 0), "Intuition":("S_N", 50),
|
47 |
+
"Thinking":("T_F", 0), "Feeling":("T_F", 50),
|
48 |
+
"Judging":("J_P", 0), "Perceiving":("J_P", 50)}
|
49 |
+
def process_choice(dic):
|
50 |
+
final = {"J_P":0 ,"T_F":0, "S_N":0, "E_I": 0 }
|
51 |
+
for k, v in dic.items():
|
52 |
+
pair = personality_map[QUESTION_ANSWER_MAP[k][v]]
|
53 |
+
final[pair[0]] += pair[1]
|
54 |
+
return final
|
55 |
+
|
56 |
+
|
57 |
+
def process_other(other):
|
58 |
+
agent = Agent()
|
59 |
+
result = {}
|
60 |
+
for k,v in other.items():
|
61 |
+
pair = agent.send_msg({k:v})
|
62 |
+
result[pair[0]] = pair[1]
|
63 |
+
final = {"J_P":0 ,"T_F":0, "S_N":0, "E_I": 0 }
|
64 |
+
for k,v in result.items():
|
65 |
+
pair = personality_map[k]
|
66 |
+
try:
|
67 |
+
final[pair[0]] += 0.5 * int(v)
|
68 |
+
except:
|
69 |
+
final[pair[0]] += 25
|
70 |
+
return final
|
71 |
+
|
72 |
+
def analyze_post(post):
|
73 |
+
agent = Agent()
|
74 |
+
|
75 |
+
result = agent.send_post(post)
|
76 |
+
print(result)
|
77 |
+
data = json.loads(result)
|
78 |
+
final = {"J_P":0 ,"T_F":0, "S_N":0, "E_I": 0 }
|
79 |
+
final['E_I'] = 100 - data["Extrovert-Introvert"]["score"]
|
80 |
+
final['S_N'] = 100 - data["Sensing-Intuition"]["score"]
|
81 |
+
final["T_F"] = 100 - data["Thinking-Feeling"]["score"]
|
82 |
+
final["J_P"] = 100 - data["Judging-Perceiving"]["score"]
|
83 |
+
return final
|
84 |
+
|
85 |
+
|
86 |
+
|
87 |
+
def generate_image(final):
|
88 |
+
# 初始化画布和坐标轴
|
89 |
+
fig, ax = plt.subplots(figsize=(6, 6))
|
90 |
+
|
91 |
+
# 设置渐变颜色
|
92 |
+
colors = [
|
93 |
+
['#FF6347', '#FFD700'],
|
94 |
+
['#00FA9A', '#32CD32'],
|
95 |
+
['#6495ED', '#4682B4'],
|
96 |
+
['#DDA0DD', '#8A2BE2']
|
97 |
+
]
|
98 |
+
|
99 |
+
# 创建一个大的图像数组
|
100 |
+
img = np.zeros((100, 100, 3), dtype=np.uint8)
|
101 |
+
|
102 |
+
sorted_keys = ["E_I", "S_N", "T_F", "J_P"]
|
103 |
+
|
104 |
+
for idx, k in enumerate(sorted_keys):
|
105 |
+
v = final[k]
|
106 |
+
if idx == 0: # 左上
|
107 |
+
img[int(50*(1-v/100)):50, :50] = np.array([int(colors[idx][0][i:i+2], 16) for i in (1, 3, 5)])
|
108 |
+
img[:int(50*(1-v/100)), :50] = np.array([int(colors[idx][1][i:i+2], 16) for i in (1, 3, 5)])
|
109 |
+
elif idx == 1: # 右上
|
110 |
+
img[int(50*(1-v/100)):50, 50:] = np.array([int(colors[idx][0][i:i+2], 16) for i in (1, 3, 5)])
|
111 |
+
img[:int(50*(1-v/100)), 50:] = np.array([int(colors[idx][1][i:i+2], 16) for i in (1, 3, 5)])
|
112 |
+
elif idx == 2: # 左下
|
113 |
+
img[50+int(50*(1-v/100)):, :50] = np.array([int(colors[idx][0][i:i+2], 16) for i in (1, 3, 5)])
|
114 |
+
img[50:int(50*(1-v/100))+50, :50] = np.array([int(colors[idx][1][i:i+2], 16) for i in (1, 3, 5)])
|
115 |
+
else: # 右下
|
116 |
+
img[50+int(50*(1-v/100)):, 50:] = np.array([int(colors[idx][0][i:i+2], 16) for i in (1, 3, 5)])
|
117 |
+
img[50:int(50*(1-v/100))+50, 50:] = np.array([int(colors[idx][1][i:i+2], 16) for i in (1, 3, 5)])
|
118 |
+
|
119 |
+
ax.imshow(img)
|
120 |
+
ax.axis('off') # 不显示坐标轴
|
121 |
+
|
122 |
+
# 在每个部分中添加文本
|
123 |
+
for idx, k in enumerate(sorted_keys):
|
124 |
+
v = final[k]
|
125 |
+
if idx == 0:
|
126 |
+
ax.text(25, 25, f"{k[0]} {100-v:.0f}%\n{k[2]} {v:.0f}%", ha='center', va='center', color='black', fontsize=12)
|
127 |
+
elif idx == 1:
|
128 |
+
ax.text(75, 25, f"{k[0]} {100-v:.0f}%\n{k[2]} {v:.0f}%", ha='center', va='center', color='black', fontsize=12)
|
129 |
+
elif idx == 2:
|
130 |
+
ax.text(25, 75, f"{k[0]} {100-v:.0f}%\n{k[2]} {v:.0f}%", ha='center', va='center', color='black', fontsize=12)
|
131 |
+
else:
|
132 |
+
ax.text(75, 75, f"{k[0]} {100-v:.0f}%\n{k[2]} {v:.0f}%", ha='center', va='center', color='black', fontsize=12)
|
133 |
+
|
134 |
+
# 使用st.pyplot显示图表
|
135 |
+
st.pyplot(fig)
|
136 |
+
|
137 |
+
|
138 |
+
|
139 |
+
|
140 |
+
|
141 |
+
# def generate_image(final):
|
142 |
+
|
143 |
+
|
144 |
+
# # 初始化列表来保存标签和值
|
145 |
+
# labels = []
|
146 |
+
# values_l = []
|
147 |
+
# values_r = []
|
148 |
+
|
149 |
+
# sorted_keys = ["J_P","T_F","S_N", "E_I"]
|
150 |
+
# # 遍历字典,更新标签和值列表
|
151 |
+
# for k in sorted_keys:
|
152 |
+
# v = final[k]
|
153 |
+
# labels.append(k)
|
154 |
+
# values_l.append(100-v)
|
155 |
+
# values_r.append(v)
|
156 |
+
|
157 |
+
# personality = ''
|
158 |
+
|
159 |
+
# for k in sorted_keys:
|
160 |
+
# v = final[k]
|
161 |
+
# if v < 50:
|
162 |
+
# personality += k[0]
|
163 |
+
# else:
|
164 |
+
# personality += k[2]
|
165 |
+
# personality = personality[::-1]
|
166 |
+
# fig, ax = plt.subplots(figsize=(10,6))
|
167 |
+
|
168 |
+
# # 使用Seaborn的更漂亮的默认样式
|
169 |
+
# sns.set(style="whitegrid")
|
170 |
+
|
171 |
+
# # 为每个键绘制两个条形图,一个为E/S/T/J值,另一个为I/N/F/P值
|
172 |
+
# width = 0.4
|
173 |
+
# ind = range(len(labels))
|
174 |
+
# p1 = ax.barh(ind, values_l, width, color=sns.color_palette("coolwarm", 4)[2], label='E/S/T/J')
|
175 |
+
# p2 = ax.barh(ind, values_r, width, left=values_l, color=sns.color_palette("coolwarm", 4)[0], label='I/N/F/P')
|
176 |
+
|
177 |
+
# # 设置标签和标题
|
178 |
+
# ax.set_yticks(ind)
|
179 |
+
# ax.set_yticklabels([label[0] for label in labels])
|
180 |
+
# ax.set_xticks([0, 50, 100])
|
181 |
+
# ax.set_xlabel('Percentage', fontsize=14)
|
182 |
+
# ax.set_title(f'Your MBTI persopnality may be {personality}', fontsize=16)
|
183 |
+
# ax.legend(loc = 'upper right', bbox_to_anchor=(1.15, 1.15))
|
184 |
+
# # 显示右侧的'I/N/F/P'标签
|
185 |
+
# for i, label in enumerate(labels):
|
186 |
+
# ax.text(105, i, label[2], ha='center', va='center', color='black', fontsize=12)
|
187 |
+
# # 使用st.pyplot显示图表
|
188 |
+
# st.pyplot(fig)
|
189 |
+
|