-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
148 lines (124 loc) Β· 4.58 KB
/
app.py
File metadata and controls
148 lines (124 loc) Β· 4.58 KB
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
import streamlit as st
import tempfile
from main import run_question_pipeline
from utils import extract_text_from_pdf, extract_text_from_docx, generate_pdf
from markdown import markdown
import re
from docx import Document
def strip_markdown(md_text):
html = markdown(md_text)
clean_text = re.sub('<[^<]+?>', '', html) # remove HTML tags
return clean_text
st.set_page_config(page_title="AI Question Paper Generator", layout="centered")
st.title("π AI Question Paper Generator")
uploaded_file = st.file_uploader("Upload Study Material (PDF or DOCX)", type=["pdf", "docx"])
if uploaded_file:
with tempfile.NamedTemporaryFile(delete=False, suffix=uploaded_file.name[-4:]) as tmp_file:
tmp_file.write(uploaded_file.read())
file_path = tmp_file.name
# Extract text from file
if uploaded_file.name.endswith(".pdf"):
raw_text = extract_text_from_pdf(file_path)
else:
raw_text = extract_text_from_docx(file_path)
st.success("π Study material loaded successfully!")
col1, col2 = st.columns([2, 3])
with col1:
st.markdown(
"<h4 style='font-size:1.66rem;'>π― Select Difficulty </h4>",
unsafe_allow_html=True
)
st.markdown("""
<style>
div[data-baseweb="select"] {
width: 65% !important;
float: right;
}
</style>
""", unsafe_allow_html=True)
with col2:
difficulty = st.selectbox(
label=" ", # Empty label
options=["Easy", "Medium", "Hard"],
index=1,
key="difficulty_level"
)
col1, col2 = st.columns([2, 3])
with col1:
st.markdown(
"<h4 style='font-size:1.66rem;'>π’ Select Total Marks</h4>",
unsafe_allow_html=True
)
st.markdown("""
<style>
div[data-baseweb="select"] {
width: 65% !important;
float: right;
}
</style>
""", unsafe_allow_html=True)
with col2:
total_marks = st.selectbox(
label=" ",
options=[10, 20, 30, 40, 50, 60, 70, 80, 90, 100],
index=0,
key="total_marks"
)
st.subheader("βοΈ Customize Question Types")
# Row 1: MCQ, 1-mark, 2-mark
col1, col2, col3 = st.columns(3)
with col1:
mcq_count = st.number_input("MCQs (1 mark)", min_value=0, value=0)
with col2:
q1 = st.number_input("1-mark Questions", min_value=0, value=0)
with col3:
q2 = st.number_input("2-mark Questions", min_value=0, value=0)
# Row 2: 3-mark, 4-mark, 5-mark
col4, col5, col6 = st.columns(3)
with col4:
q3 = st.number_input("3-mark Questions", min_value=0, value=0)
with col5:
q4 = st.number_input("4-mark Questions", min_value=0, value=0)
with col6:
q5 = st.number_input("5-mark Questions", min_value=0, value=0)
# Row 3: 10-mark, 15-mark, 20-mark
col7, col8, col9 = st.columns(3)
with col7:
q10 = st.number_input("10-mark Questions", min_value=0, value=0)
with col8:
q15 = st.number_input("15-mark Questions", min_value=0, value=0)
with col9:
q20 = st.number_input("20-mark Questions", min_value=0, value=0)
actual_total = (
mcq_count * 1 + q1 * 1 + q2 * 2 + q3 * 3 + q4 * 4 +
q5 * 5 + q10 * 10 + q15 * 15 + q20 * 20
)
if actual_total != total_marks:
st.warning(f"β οΈ Total marks ({actual_total}) do not match selected value ({total_marks}). Please adjust.")
if st.button("π Generate Question Paper"):
with st.spinner("Generating... Please wait..."):
counts = {
"mcq": mcq_count,
"q1": q1,
"q2": q2,
"q3": q3,
"q4": q4,
"q5": q5,
"q10": q10,
"q15": q15,
"q20": q20
}
result = run_question_pipeline(raw_text, total_marks, counts, difficulty)
if "error" in result:
st.error(f"β Error: {result['error']}")
else:
st.subheader("π Generated Question Paper")
st.markdown(result["questions"])
clean_text = strip_markdown(result["questions"])
pdf_bytes = generate_pdf("not_used", clean_text)
st.download_button(
label="π₯ Download Question Paper (PDF)",
data=pdf_bytes,
file_name="question_paper.pdf",
mime="application/pdf"
)