|
import streamlit as st |
|
import pandas as pd |
|
from plms.language_model import TransformersQG |
|
import os |
|
import numpy as np |
|
|
|
st.set_page_config(page_icon='🧪', page_title='ViQAG for Vietnamese Education', layout='wide', initial_sidebar_state="collapsed") |
|
|
|
with open(r"./static/styles.css") as f: |
|
st.markdown(f"<style>{f.read()}</style>", unsafe_allow_html=True) |
|
|
|
st.markdown(f""" |
|
<div class=logo_area> |
|
<img src="./app/static/AlphaEdu_logo_trans.png"/> |
|
</div> |
|
""", unsafe_allow_html=True) |
|
st.markdown("<h1 style='text-align: center;'>AlphaEdu</h1>", unsafe_allow_html=True) |
|
|
|
|
|
|
|
if 'output' not in st.session_state: |
|
st.session_state.output = '' |
|
|
|
def file_selector(folder_path=r'./Resources/'): |
|
filenames = os.listdir(folder_path) |
|
return filenames |
|
filenames = file_selector() |
|
|
|
def load_grades(file_name, folder_path=r'./Resources/'): |
|
file_path = f"{folder_path}{file_name}" |
|
df = pd.read_csv(file_path) |
|
list_grades = df['grade'].drop_duplicates().values |
|
return list_grades, df |
|
|
|
def load_chapters(df, grade_name): |
|
df_raw = df[df['grade'] == grade_name] |
|
list_chapters = df_raw['chapter'].drop_duplicates().values |
|
return list_chapters, df |
|
|
|
def load_lessons(df, grade_name, chapter_name): |
|
df_raw = df[(df['grade'] == grade_name) & (df['chapter'] == chapter_name)] |
|
return df_raw['lesson'].drop_duplicates().values |
|
|
|
def load_context(df, grade_name, chapter_name, lesson_name): |
|
context = df[(df['grade'] == grade_name) & (df['chapter'] == chapter_name) & (df['lesson'] == lesson_name)]['context'].values |
|
return len(context), context |
|
|
|
def generateQA(context, model_path = 'shnl/vit5-vinewsqa-qg-ae'): |
|
unique_qa_pairs = set() |
|
model = TransformersQG(model=model_path, max_length=512) |
|
output = model.generate_qa(context) |
|
qa_pairs = '' |
|
for item in output: |
|
question, answer = item |
|
if (question, answer) not in unique_qa_pairs: |
|
qa_pairs += f'question: {question} \nanswer: {answer} [SEP] ' |
|
unique_qa_pairs.add((question, answer)) |
|
qa = '\n\n'.join(qa_pairs.split(' [SEP] ')) |
|
return qa |
|
|
|
|
|
|
|
col_1, col_2, col_3, col_4, col_5 = st.columns(spec=[1, 1, 3, 3, 1]) |
|
|
|
col_1.markdown("<h8 style='text-align: left; font-weight: normal'>Select your subject:</h8>", unsafe_allow_html=True) |
|
subject = col_1.selectbox(label='Select your subject:', options=filenames, label_visibility='collapsed') |
|
|
|
col_2.markdown("<h8 style='text-align: left; font-weight: normal'>Select your grade:</h8>", unsafe_allow_html=True) |
|
list_grades, df = load_grades(file_name=subject) |
|
grade = col_2.selectbox(label='Select your grade:', options=list_grades, label_visibility='collapsed') |
|
|
|
col_3.markdown("<h8 style='text-align: left; font-weight: normal'>Select your chapter:</h8>", unsafe_allow_html=True) |
|
list_chapters, df = load_chapters(df=df, grade_name=grade) |
|
chapter = col_3.selectbox(label='Select your chapter:', options=list_chapters, label_visibility='collapsed') |
|
|
|
col_4.markdown("<h8 style='text-align: left; font-weight: normal'>Select your lesson:</h8>", unsafe_allow_html=True) |
|
lesson_names = load_lessons(df=df, grade_name=grade, chapter_name=chapter) |
|
lesson = col_4.selectbox(label='Select your lesson:', options=lesson_names, label_visibility='collapsed') |
|
|
|
col_5.markdown("<h8 style='text-align: left; font-weight: normal'>Paragraph:</h8>", unsafe_allow_html=True) |
|
total_paragraph, context_values = load_context(df=df, grade_name=grade, chapter_name=chapter, lesson_name=lesson) |
|
|
|
paragraph_idx = col_5.selectbox(label='Select your lesson:', options=list(np.arange(1, total_paragraph + 1)), label_visibility='collapsed') |
|
|
|
col_11, col_21 = st.columns(spec=[9, 1]) |
|
col_11.markdown("<h8 style='text-align: left; font-weight: normal'>Paragraph related:</h8>", unsafe_allow_html=True) |
|
col_11.text_area(label='Paragraph related', label_visibility='collapsed', height=300, value=context_values[paragraph_idx - 1]) |
|
|
|
col_21.markdown("<h8 style='text-align: left; font-weight: normal'>QAG modes:</h8>", unsafe_allow_html=True) |
|
col_21.selectbox(label='QAG model:', options=['ViT5-ViNewsQA'], label_visibility='collapsed') |
|
|
|
col_21.markdown("<h8 style='text-align: left; font-weight: normal'>Options:</h8>", unsafe_allow_html=True) |
|
btn_show_answer = col_21.toggle(label='Show answers', disabled=False) |
|
|
|
btn_generate = col_21.button(label='Generate questions', use_container_width=True) |
|
|
|
if btn_generate == True: |
|
with st.spinner(text='Generating QA pairs from the selected context. Please wait ...'): |
|
st.session_state.output = generateQA(context=context_values) |
|
|
|
if btn_show_answer: |
|
if st.session_state.output != '': |
|
st.markdown("<h8 style='text-align: left; font-weight: normal'>Your questions and answers has been generated:</h8>", unsafe_allow_html=True) |
|
st.code(body=st.session_state.output, language='wiki') |
|
else: |
|
pass |
|
else: |
|
if st.session_state.output != '': |
|
st.markdown("<h8 style='text-align: left; font-weight: normal'>Your questions and answers has been generated:</h8>", unsafe_allow_html=True) |
|
output_no_answer = st.session_state.output.split(' [SEP] ')[0].split(', answer: ')[0].replace('question: ', '') |
|
st.code(body=output_no_answer, language='wiki') |
|
else: |
|
pass |