Spaces:
Configuration error
Configuration error
Upload app.py
Browse files
app.py
ADDED
@@ -0,0 +1,88 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
from ui_utils import check_password
|
3 |
+
from pdf_to_quizz import pdf_to_quizz
|
4 |
+
from text_to_quizz import txt_to_quizz
|
5 |
+
from generate_pdf import generate_pdf_quiz
|
6 |
+
import json
|
7 |
+
|
8 |
+
import asyncio
|
9 |
+
|
10 |
+
st.title("PDF to Quiz (:-)(-: )")
|
11 |
+
|
12 |
+
def build_question(count, json_question):
|
13 |
+
|
14 |
+
if json_question.get(f"question") is not None:
|
15 |
+
st.write("Question: ", json_question.get(f"question", ""))
|
16 |
+
choices = ['A', 'B', 'C', 'D']
|
17 |
+
selected_answer = st.selectbox(f"Selectionnez votre réponse:", choices, key=f"select_{count}")
|
18 |
+
for choice in choices:
|
19 |
+
choice_str = json_question.get(f"{choice}", "None")
|
20 |
+
st.write(f"{choice} : {choice_str}")
|
21 |
+
|
22 |
+
color = ""
|
23 |
+
if st.button("Soumettre", key=f"button_{count}"):
|
24 |
+
rep = json_question.get(f"reponse")
|
25 |
+
if selected_answer in rep:
|
26 |
+
color = ":green"
|
27 |
+
st.write(f":green[Bonne réponse: {rep}]")
|
28 |
+
|
29 |
+
else:
|
30 |
+
color = ":red"
|
31 |
+
st.write(f":red[Mauvause réponse. La bonne réponse est {rep}].")
|
32 |
+
|
33 |
+
st.write(f"{color}[Votre réponse: {selected_answer}]")
|
34 |
+
|
35 |
+
count += 1
|
36 |
+
|
37 |
+
return count
|
38 |
+
|
39 |
+
# Upload PDF file
|
40 |
+
uploaded_file = st.file_uploader(":female-student:", type=["pdf"])
|
41 |
+
txt = st.text_area('Taper le texte à partir duquel vous voulez générer le quizz')
|
42 |
+
|
43 |
+
if st.button("Générer Quiz", key=f"button_generer"):
|
44 |
+
if txt is not None:
|
45 |
+
with st.spinner("Génération du quizz..."):
|
46 |
+
st.session_state['questions'] = txt_to_quizz(txt)
|
47 |
+
st.write("Quizz généré avec succès!")
|
48 |
+
|
49 |
+
if uploaded_file is not None:
|
50 |
+
old_file_name = st.session_state.get('uploaded_file_name', None)
|
51 |
+
if (old_file_name != uploaded_file.name):
|
52 |
+
# Convert PDF to text
|
53 |
+
with st.spinner("Génération du quizz..."):
|
54 |
+
|
55 |
+
with open(f"data/{uploaded_file.name}", "wb") as f:
|
56 |
+
f.write(uploaded_file.getvalue())
|
57 |
+
|
58 |
+
# Initialize session state
|
59 |
+
st.session_state['uploaded_file_name'] = uploaded_file.name
|
60 |
+
st.session_state['questions'] = pdf_to_quizz(f"data/{uploaded_file.name}")
|
61 |
+
|
62 |
+
st.write("Quizz généré avec succès!")
|
63 |
+
|
64 |
+
if ('questions' in st.session_state):
|
65 |
+
# Display question
|
66 |
+
count = 0
|
67 |
+
for json_question in st.session_state['questions']:
|
68 |
+
|
69 |
+
count = build_question(count, json_question)
|
70 |
+
|
71 |
+
# generate pdf quiz
|
72 |
+
if st.button("Générer PDF Quiz", key=f"button_generer_quiz"):
|
73 |
+
with st.spinner("Génération du quizz en PDF..."):
|
74 |
+
json_questions = st.session_state['questions']
|
75 |
+
# save into a file
|
76 |
+
file_name = uploaded_file.name
|
77 |
+
|
78 |
+
# remove extension .pdf from file name
|
79 |
+
if file_name.endswith(".pdf"):
|
80 |
+
file_name = file_name[:-4]
|
81 |
+
|
82 |
+
with open(f"data/quiz-{file_name}.json", "w", encoding='latin-1', errors='ignore') as f:
|
83 |
+
str = json.dumps(json_questions)
|
84 |
+
f.write(str)
|
85 |
+
|
86 |
+
generate_pdf_quiz(f"data/quiz-{file_name}.json", json_questions)
|
87 |
+
|
88 |
+
st.write("PDF Quiz généré avec succés!")
|