Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,180 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
from process import process_choice,process_other, generate_image, analyze_post
|
3 |
+
|
4 |
+
# 定义问题列表
|
5 |
+
QUESTIONS = [
|
6 |
+
"Question1: Are you usually?",
|
7 |
+
"Question2: Among your friends, you are?",
|
8 |
+
"Question3: In doing something that many other people do, you would rather?",
|
9 |
+
"Question4: Do you admire the people who are?",
|
10 |
+
"Question5: Do you more often let?",
|
11 |
+
"Question6: Do you usually?",
|
12 |
+
"Question7: When you go somewhere for the day, you would rather",
|
13 |
+
"Question8: When you have a special job to do, you like to"
|
14 |
+
]
|
15 |
+
|
16 |
+
OPTIONS = {
|
17 |
+
QUESTIONS[0]: ["A 'Good Mixer with groups of people", "Rather quiet and reserved"], # Extrovert (E) vs. Introvert (I)
|
18 |
+
QUESTIONS[1]: ["Full of news about everybody", "One of the last to hear what is going on"], #Extrovert (E) vs. Introvert (I)
|
19 |
+
QUESTIONS[2]: ["Invent a way of your own", "Do it in the accepted way "], #Intuition, Sensing
|
20 |
+
QUESTIONS[3]: ["Normal-acting to never make themselves the center of attention", "Too original and individual to care whether they are the center of attention or not"],# Sensing, Intuition
|
21 |
+
QUESTIONS[4]: ["Your heart rule your head", "Your head rule your heart"], ## Feeling, Thinking
|
22 |
+
QUESTIONS[5]: ["Value emotion more than logic", "Value logic more than feelings"], # Thinking, Feeling
|
23 |
+
QUESTIONS[6]: ["Plan what you will do and when", "Just go"], # Judging, Perceiving
|
24 |
+
QUESTIONS[7]: ["Organize it carefully before you start", "Find out what is necessary as you go along"] # Judging, Perceiving
|
25 |
+
}
|
26 |
+
|
27 |
+
def main():
|
28 |
+
# 页面选择
|
29 |
+
page = st.sidebar.radio("Choose Test Method", ["Questionnaire", "Post upload"])
|
30 |
+
|
31 |
+
if page == "Questionnaire":
|
32 |
+
questionnaire()
|
33 |
+
elif page == "Post upload":
|
34 |
+
post()
|
35 |
+
|
36 |
+
def questionnaire():
|
37 |
+
custom_css = """
|
38 |
+
<style>
|
39 |
+
body {
|
40 |
+
font-family: Arial, Helvetica, sans-serif;
|
41 |
+
}
|
42 |
+
h1 {
|
43 |
+
font-size: 52px;
|
44 |
+
}
|
45 |
+
h2 {
|
46 |
+
color: #4A90E2;
|
47 |
+
font-size: 36px;
|
48 |
+
}
|
49 |
+
label[data-baseweb="radio"] div[data-testid="stMarkdownContainer"] p {
|
50 |
+
font-size: 20px !important;
|
51 |
+
line-height: 24px !important;
|
52 |
+
margin-top: 5px !important;
|
53 |
+
}
|
54 |
+
label[data-baseweb="radio"] .st-c9 {
|
55 |
+
margin-top: 8px !important;
|
56 |
+
}
|
57 |
+
.question-text {
|
58 |
+
font-size: 40px;
|
59 |
+
font-weight: bold;
|
60 |
+
margin-bottom: 20px;
|
61 |
+
}
|
62 |
+
|
63 |
+
</style>
|
64 |
+
"""
|
65 |
+
st.markdown(custom_css, unsafe_allow_html=True)
|
66 |
+
|
67 |
+
st.title("Questionnaire")
|
68 |
+
|
69 |
+
|
70 |
+
# 使用session_state来跟踪当前的问题索引
|
71 |
+
if "current_question_index" not in st.session_state:
|
72 |
+
st.session_state.current_question_index = 0
|
73 |
+
|
74 |
+
# 初始化两个答案字典
|
75 |
+
if "answers_choices" not in st.session_state:
|
76 |
+
st.session_state.answers_choices = {}
|
77 |
+
if "answers_other" not in st.session_state:
|
78 |
+
st.session_state.answers_other = {}
|
79 |
+
|
80 |
+
# 显示当前问题,使用Markdown增大字体并添加额外的空间'
|
81 |
+
|
82 |
+
|
83 |
+
cols = st.columns([4, 1])
|
84 |
+
# 在左侧列显示问题
|
85 |
+
current_question = QUESTIONS[st.session_state.current_question_index]
|
86 |
+
cols[0].markdown(f'<div class="question-text">{current_question}</div>', unsafe_allow_html=True)
|
87 |
+
|
88 |
+
option_a, option_b = OPTIONS[current_question]
|
89 |
+
cols[0].markdown('<div class="custom-radio">', unsafe_allow_html=True)
|
90 |
+
selected_option = cols[0].radio("", [option_a, option_b, "Other"])
|
91 |
+
cols[0].markdown('</div>', unsafe_allow_html=True)
|
92 |
+
|
93 |
+
user_answer = None
|
94 |
+
with st.container():
|
95 |
+
st.markdown("---")
|
96 |
+
if selected_option == "Other":
|
97 |
+
# 使用.get()方法获取值,并在键不存在时提供一个默认值
|
98 |
+
default_value = st.session_state.answers_other.get(st.session_state.current_question_index, "")
|
99 |
+
user_answer = st.text_input("Please write your answer:", default_value)
|
100 |
+
else:
|
101 |
+
user_answer = selected_option
|
102 |
+
|
103 |
+
# 清除按钮
|
104 |
+
if cols[1].button("Clear ALL"):
|
105 |
+
st.session_state.current_question_index = 0
|
106 |
+
st.session_state.answers_choices.clear()
|
107 |
+
st.session_state.answers_other.clear()
|
108 |
+
st.experimental_rerun()
|
109 |
+
|
110 |
+
# 首个问题
|
111 |
+
elif st.session_state.current_question_index == 0:
|
112 |
+
if st.button("Next"):
|
113 |
+
if selected_option == "Other":
|
114 |
+
st.session_state.answers_other[current_question] = user_answer
|
115 |
+
else:
|
116 |
+
st.session_state.answers_choices[current_question] = user_answer
|
117 |
+
st.session_state.current_question_index += 1
|
118 |
+
st.experimental_rerun()
|
119 |
+
|
120 |
+
# 最后一个问题
|
121 |
+
elif st.session_state.current_question_index == len(QUESTIONS) - 1:
|
122 |
+
cols = st.columns([1, 1, 1])
|
123 |
+
prev_button, _, submit_button = cols
|
124 |
+
|
125 |
+
if prev_button.button("Prev"):
|
126 |
+
st.session_state.current_question_index -= 1
|
127 |
+
st.experimental_rerun()
|
128 |
+
elif submit_button.button("Submit"):
|
129 |
+
if selected_option == "Other":
|
130 |
+
st.session_state.answers_other[current_question] = user_answer
|
131 |
+
|
132 |
+
else:
|
133 |
+
st.session_state.answers_choices[current_question] = user_answer
|
134 |
+
|
135 |
+
final0 = process_choice(st.session_state.answers_choices)
|
136 |
+
final1 = process_other(st.session_state.answers_other)
|
137 |
+
final = {key: final0[key]+ final1[key] for key in set(final0) | set(final1)}
|
138 |
+
generate_image(final)
|
139 |
+
|
140 |
+
|
141 |
+
|
142 |
+
# 中间的问题
|
143 |
+
else:
|
144 |
+
cols = st.columns([1, 1, 1])
|
145 |
+
prev_button, next_button, _ = cols
|
146 |
+
|
147 |
+
if prev_button.button("Prev"):
|
148 |
+
st.session_state.current_question_index -= 1
|
149 |
+
st.experimental_rerun()
|
150 |
+
elif next_button.button("Next"):
|
151 |
+
if selected_option == "Other":
|
152 |
+
st.session_state.answers_other[current_question] = user_answer
|
153 |
+
|
154 |
+
else:
|
155 |
+
st.session_state.answers_choices[current_question] = user_answer
|
156 |
+
st.session_state.current_question_index += 1
|
157 |
+
st.experimental_rerun()
|
158 |
+
|
159 |
+
|
160 |
+
|
161 |
+
|
162 |
+
|
163 |
+
def post():
|
164 |
+
# 设置页面标题
|
165 |
+
st.title("Post Analysis")
|
166 |
+
|
167 |
+
# 创建文本输入框
|
168 |
+
user_input = st.text_area("Enter your post here:")
|
169 |
+
|
170 |
+
# 创建提交按钮
|
171 |
+
if st.button("Submit"):
|
172 |
+
# 在这里调用你的文本分析函数
|
173 |
+
final = analyze_post(user_input) # 假设你有一个analyze_text函数来进行文本分析
|
174 |
+
|
175 |
+
st.subheader("Analysis Result:")
|
176 |
+
generate_image(final)
|
177 |
+
|
178 |
+
|
179 |
+
if __name__ == "__main__":
|
180 |
+
main()
|