-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathui.py
100 lines (79 loc) · 4.32 KB
/
ui.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
import audio
import llm_module
import streamlit as st
from data import append_new_exercise
# Function to disable Create, Update and Delete buttons
def disable():
if "authentication_status" not in st.session_state:
st.session_state.authentication_status = None
return True
elif st.session_state["authentication_status"] == True:
return False
else:
return True
# Function to display a selected subsubsection
def show_subsubsection(selected_subsection_data):
language = selected_subsection_data.get("language")
exercises = selected_subsection_data.get('exercises')
for item in exercises:
exercise_number = item.get('exercise_number')
st.markdown(f"#### Exercise {exercise_number}")
sentence = item.get('sentence')
st.markdown(f"**A)** {sentence}")
if len(sentence) > 1:
st.audio(audio.generate_audio(sentence, language))
answer = item.get('answer')
st.markdown(f"**B)** {answer}")
if len(answer) > 1:
st.audio(audio.generate_audio(answer))
# Function to display a selected subsection
def show_subsection(selected_subsection_data, disable_button):
container = st.container()
section_name= selected_subsection_data.get("section")
subsection_number = selected_subsection_data.get("subsection_number")
subsection_name = selected_subsection_data.get('subsection_name')
language = selected_subsection_data.get("language")
instruction = selected_subsection_data.get("instruction")
container.info(f"{instruction}")
exercises = selected_subsection_data.get('exercises')
# Create buttons to navigate to the next and previous exercises
col1, _, col2 = st.columns([1, 0.2, 1])
with col1:
if st.button("⏮️", use_container_width=True):
st.session_state['current_exercise'] -= 1
if st.session_state['current_exercise'] == 0:
st.session_state['current_exercise'] = len(exercises)
# Set the query parameters to navigate to the previous exercise
st.experimental_set_query_params(subsection=st.session_state['current_subsection'], exercise=st.session_state['current_exercise'])
with col2:
if st.button("⏭️", use_container_width=True):
st.session_state['current_exercise'] += 1
if st.session_state['current_exercise'] == len(exercises)+1:
st.session_state['current_exercise'] = 1
# Set the query parameters to navigate to the next exercise
st.experimental_set_query_params(subsection=st.session_state['current_subsection'], exercise=st.session_state['current_exercise'])
if 1<= st.session_state['current_exercise'] <= len(exercises):
exercise = exercises[st.session_state['current_exercise']-1]
exercise_number = exercise.get("exercise_number")
container.markdown(f"#### Exercise {exercise_number}")
sentence = exercise.get('sentence')
container.markdown(f"**Sentence**: {sentence}")
container.audio(audio.generate_audio(sentence, language=language))
with container.expander("Answer"):
answer = exercise.get('answer')
st.markdown(f"{answer}")
st.audio(audio.generate_audio(answer))
st.sidebar.write("---")
if st.sidebar.button('Generate (_Cohere LLM_)', type='secondary', use_container_width=False):
with st.spinner("Please wait..."):
new_sentences_json = llm_module.generate_additional_sentences(exercises, section_name, subsection_number, subsection_name, instruction, language=language, start=len(exercises)+1)
st.session_state["new_sentences_json"] = new_sentences_json[0]
if st.session_state["new_sentences_json"]:
show_subsubsection(st.session_state["new_sentences_json"])
st.write("---")
st.write(st.session_state["new_sentences_json"])
append_button = st.button("Append", disabled=disable_button)
if append_button:
append_new_exercise(st.session_state["new_sentences_json"]["exercises"], section_name, subsection_number)
def logo():
st.sidebar.write("<br><div style='text-align: center; font-size: small;'>Made by </div><div style='text-align: center; font-size: small;'><a href='https://github.com/adinmg/MongoSpeakApp'>@adinmg</a></div>", unsafe_allow_html=True)