Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,69 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from testpack import generate_questionnaire
|
| 2 |
+
import streamlit as st
|
| 3 |
+
from read_files import read_docx, read_pdf
|
| 4 |
+
sts = st.session_state
|
| 5 |
+
|
| 6 |
+
def callback():
|
| 7 |
+
with st.spinner("문제를 생성 중입니다."):
|
| 8 |
+
sts.questionnaire = generate_questionnaire(sts.model, sts.input_text, sts.language, sts.num_questions)
|
| 9 |
+
sts.current_step = "B"
|
| 10 |
+
|
| 11 |
+
def callback2():
|
| 12 |
+
sts.current_step = "C"
|
| 13 |
+
|
| 14 |
+
if "current_step" not in sts:
|
| 15 |
+
sts.current_step = "A"
|
| 16 |
+
if "input_text" not in sts:
|
| 17 |
+
sts.input_text = ""
|
| 18 |
+
|
| 19 |
+
st.title("모의고사 자동제작 서비스")
|
| 20 |
+
|
| 21 |
+
if sts.current_step == "A":
|
| 22 |
+
format = st.radio("파일 유형 선택", ["직접 입력", ".pdf", ".docx"], index=0, key="format")
|
| 23 |
+
if format == "직접 입력":
|
| 24 |
+
sts.input_text = st.text_area("내용 입력", sts.input_text)
|
| 25 |
+
elif format == ".pdf":
|
| 26 |
+
pdf_loc = st.file_uploader("최대 200MB까지 업로드할 수 있습니다")
|
| 27 |
+
if pdf_loc:
|
| 28 |
+
sts.input_text = read_pdf(pdf_loc)
|
| 29 |
+
elif format == ".docx":
|
| 30 |
+
docx_loc = st.file_uploader("최대 200MB까지 업로드할 수 있습니다")
|
| 31 |
+
if docx_loc:
|
| 32 |
+
sts.input_text = read_docx(docx_loc)
|
| 33 |
+
|
| 34 |
+
with st.form("maker"):
|
| 35 |
+
model = st.radio("모델 선택 (GPT-4를 권장합니다.)", ["GPT-4", "GPT-3.5"], index=0, key="model")
|
| 36 |
+
language = st.radio("언어 선택 (영어가 더 정확합니다.)", ["한국어", "English"], index=0, key="language")
|
| 37 |
+
num_questions = st.slider("생성할 문제 수 설정 (인공지능의 한계로 동일한 문제가 여러 개 생성될 수 있음)", min_value=1, max_value=20, value=5, key="num_questions")
|
| 38 |
+
if sts.input_text:
|
| 39 |
+
st.form_submit_button("모의고사 생성하기", on_click=callback)
|
| 40 |
+
else:
|
| 41 |
+
st.form_submit_button(":red[내용을 입력하거나 파일을 업로드해 주세요.]", disabled=True)
|
| 42 |
+
|
| 43 |
+
if sts.current_step == "B":
|
| 44 |
+
with st.form("main_quiz"):
|
| 45 |
+
for q_no, question in enumerate(sts.questionnaire):
|
| 46 |
+
labels = question["selection"]
|
| 47 |
+
st.radio(question["question"], range(len(labels)), index=0, format_func=labels.__getitem__, key=f"chosen_{q_no}")
|
| 48 |
+
st.form_submit_button("제출하기", on_click=callback2)
|
| 49 |
+
|
| 50 |
+
if sts.current_step == "C":
|
| 51 |
+
score_list = [0] * len(sts.questionnaire)
|
| 52 |
+
for q_no, question in enumerate(sts.questionnaire):
|
| 53 |
+
selection_list = []
|
| 54 |
+
for s_no, sel in enumerate(question["selection"]):
|
| 55 |
+
if s_no == sts[f"chosen_{q_no}"]:
|
| 56 |
+
if s_no == question["correct"]:
|
| 57 |
+
score_list[q_no] = 1
|
| 58 |
+
selection_list.append(f":green[{sel}]")
|
| 59 |
+
else:
|
| 60 |
+
selection_list.append(f":red[{sel}]")
|
| 61 |
+
else:
|
| 62 |
+
if s_no == question["correct"]:
|
| 63 |
+
selection_list.append(f":green[{sel}]")
|
| 64 |
+
else:
|
| 65 |
+
selection_list.append(sel)
|
| 66 |
+
st.radio(question["question"], selection_list, index=sts[f"chosen_{q_no}"], disabled=True)
|
| 67 |
+
result = "✅" if score_list[q_no] == 1 else "❌"
|
| 68 |
+
st.write(f"{result} {question['explanation']}")
|
| 69 |
+
st.write(f"{len(sts.questionnaire)}개 중 {sum(score_list)}개 맞추셨습니다. 다시 이용하고 싶으시면 새로고침해 주세요.")
|