-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.py
168 lines (144 loc) · 5.14 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
from tutor_model import Tutor
import templates
import os
import streamlit as st
import streamlit.components.v1 as components
st.title("TUTOR-AI")
st.sidebar.markdown("# Setup\n")
api_key_input = st.sidebar.text_input(
"OpenAI API Key",
type="password",
placeholder="Paste your OpenAI API key here (sk-...)",
help="You can get your API key from https://platform.openai.com/account/api-keys.",
)
st.sidebar.markdown("---")
st.sidebar.markdown("Made by [email protected]")
st.sidebar.markdown("---")
role_input = st.selectbox(
"Select a Situation",
options=templates.role_list,
)
if role_input == "Learn":
Learn = st.text_input(
"learning level:",
placeholder="subject",
help="Enter the subject which you want to learn",
)
st.session_state["Learn"] = (
Learn if Learn else "math"
)
if "started" not in st.session_state:
st.session_state.started = False
def start():
st.session_state.started = True
st.session_state.ques_num = 0
st.button(label=f"Start {role_input}", type="primary", on_click=start)
st.markdown("\n---\n")
if st.session_state.started:
if not api_key_input:
st.error("Please enter your Open AI API key")
st.stop()
if role_input == "Learn":
tutor = Tutor(api_key_input, role_input, st.session_state.Learn)
else:
tutor = Tutor(api_key_input, role_input)
if "questions" not in st.session_state:
with st.spinner("Generating Questions..."):
questions = tutor.get_questions()
st.session_state.questions = questions
else:
questions = st.session_state.questions
questions = list(
filter(lambda x: (x.strip() != "" and x.strip() != "."), questions)
)
if st.session_state.ques_num >= len(questions):
st.success("Congratulation. You have Completed the tution.")
st.markdown("Your Score is:")
st.markdown(f"## {st.session_state.cumulative_score/15} / 10")
st.stop()
def update_ques_num():
st.session_state.ques_num += 1
with st.chat_message("tutor"):
st.markdown("**Question**")
st.write(questions[st.session_state.ques_num])
if "answer" not in st.session_state:
st.text_area("Write your answer here...", key="answer")
st.button(
label="Submit",
type="primary",
)
else:
with st.chat_message("user"):
st.markdown("**Answer**")
st.write(st.session_state.answer)
with st.spinner("Analyzing Your Answer..."):
rating = tutor.rate_answer(
questions[st.session_state.ques_num], st.session_state.answer
)
suggestion = tutor.get_suggestion(
questions[st.session_state.ques_num], st.session_state.answer
)
if "cumulative_score" not in st.session_state:
st.session_state.cumulative_score = (
int(list(rating.values())[0])
+ int(list(rating.values())[1])
+ int(list(rating.values())[2])
)
else:
st.session_state.cumulative_score += (
int(list(rating.values())[0])
+ int(list(rating.values())[1])
+ int(list(rating.values())[2])
)
print(st.session_state.cumulative_score)
col2, col3 = st.columns(2)
stylesheet = """
<style>
.rating-container {
font-family: Poppins, Seouge-UI, sans-serif;
display: flex;
flex-direction: column;
align-items: center;
padding: 1rem;
border-radius: 1rem;
text-align: center;
margin: 0.5rem;
}
.rating-container h1 {
font-weight: 700;
font-size: 2rem;
opacity: 0.6;
}
.rating-container h3 {
font-weight: 400;
font-size: 1rem;
}
</style>
"""
with col2:
components.html(
f"""{stylesheet}
<div class='rating-container' style='background-color:#B7A4FF;'>
<h3>{list(rating.keys())[1]}</h3>
<h1>{list(rating.values())[1]}</h1>
</div>
"""
)
with col3:
components.html(
f"""{stylesheet}
<div class='rating-container' style='background-color:#F1AA1E;'>
<h3>{list(rating.keys())[2]}</h3>
<h1>{list(rating.values())[2]}</h1>
</div>
"""
)
with st.chat_message("assistant"):
st.markdown("**Suggestion**")
st.info(suggestion)
st.button(
label="Next",
type="primary",
on_click=update_ques_num,
key=st.session_state.ques_num,
)