Spaces:
Sleeping
Sleeping
Update src/streamlit_app.py
Browse files- src/streamlit_app.py +29 -17
src/streamlit_app.py
CHANGED
|
@@ -21,14 +21,17 @@ def find_sections(pdf_bytes, marker_pattern):
|
|
| 21 |
# Example pattern: r"^(Question|Q)\s*(\d+)"
|
| 22 |
matches = re.finditer(marker_pattern, text, re.IGNORECASE | re.MULTILINE)
|
| 23 |
for match in matches:
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
key = f"Q{
|
| 27 |
if key not in [item['key'] for item in found_items]:
|
| 28 |
-
found_items.append({'key': key, 'page': page_num})
|
| 29 |
|
| 30 |
if not found_items:
|
| 31 |
return {}
|
|
|
|
|
|
|
|
|
|
| 32 |
|
| 33 |
# Determine page ranges
|
| 34 |
for i, item in enumerate(found_items):
|
|
@@ -95,7 +98,7 @@ if st.button("π Process PDFs", disabled=(not all([q_file, r_file, s_file]))):
|
|
| 95 |
s_sections = find_sections(s_bytes, marker_pattern)
|
| 96 |
|
| 97 |
# Get a unique, sorted list of all question keys found (e.g., Q1, Q2, Q10)
|
| 98 |
-
all_keys = sorted(list(set(q_sections.keys()) | set(r_sections.keys()) | set(s_sections.keys())), key=lambda x: int(x
|
| 99 |
|
| 100 |
if not all_keys:
|
| 101 |
st.error("Could not find any sections with the provided marker. Please check your PDFs or refine the marker text.")
|
|
@@ -127,13 +130,16 @@ if st.button("π Process PDFs", disabled=(not all([q_file, r_file, s_file]))):
|
|
| 127 |
st.markdown("#### Question")
|
| 128 |
if item['question']:
|
| 129 |
with st.expander("ποΈ Preview"):
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
|
|
|
|
|
|
|
|
|
| 133 |
st.download_button(
|
| 134 |
label="β¬οΈ Download PDF",
|
| 135 |
data=item['question'],
|
| 136 |
-
file_name=f"{item['key']}_question.pdf",
|
| 137 |
mime="application/pdf"
|
| 138 |
)
|
| 139 |
else:
|
|
@@ -143,13 +149,16 @@ if st.button("π Process PDFs", disabled=(not all([q_file, r_file, s_file]))):
|
|
| 143 |
st.markdown("#### Rubric")
|
| 144 |
if item['rubric']:
|
| 145 |
with st.expander("ποΈ Preview"):
|
| 146 |
-
|
| 147 |
-
|
| 148 |
-
|
|
|
|
|
|
|
|
|
|
| 149 |
st.download_button(
|
| 150 |
label="β¬οΈ Download PDF",
|
| 151 |
data=item['rubric'],
|
| 152 |
-
file_name=f"{item['key']}_rubric.pdf",
|
| 153 |
mime="application/pdf"
|
| 154 |
)
|
| 155 |
else:
|
|
@@ -159,13 +168,16 @@ if st.button("π Process PDFs", disabled=(not all([q_file, r_file, s_file]))):
|
|
| 159 |
st.markdown("#### Solution")
|
| 160 |
if item['solution']:
|
| 161 |
with st.expander("ποΈ Preview"):
|
| 162 |
-
|
| 163 |
-
|
| 164 |
-
|
|
|
|
|
|
|
|
|
|
| 165 |
st.download_button(
|
| 166 |
label="β¬οΈ Download PDF",
|
| 167 |
data=item['solution'],
|
| 168 |
-
file_name=f"{item['key']}_solution.pdf",
|
| 169 |
mime="application/pdf"
|
| 170 |
)
|
| 171 |
else:
|
|
|
|
| 21 |
# Example pattern: r"^(Question|Q)\s*(\d+)"
|
| 22 |
matches = re.finditer(marker_pattern, text, re.IGNORECASE | re.MULTILINE)
|
| 23 |
for match in matches:
|
| 24 |
+
# We use the raw number found for sorting later
|
| 25 |
+
question_num_str = match.group(1)
|
| 26 |
+
key = f"Q{question_num_str}"
|
| 27 |
if key not in [item['key'] for item in found_items]:
|
| 28 |
+
found_items.append({'key': key, 'page': page_num, 'num': int(question_num_str)})
|
| 29 |
|
| 30 |
if not found_items:
|
| 31 |
return {}
|
| 32 |
+
|
| 33 |
+
# Sort items numerically to handle Q1, Q2, Q10 correctly
|
| 34 |
+
found_items.sort(key=lambda x: x['num'])
|
| 35 |
|
| 36 |
# Determine page ranges
|
| 37 |
for i, item in enumerate(found_items):
|
|
|
|
| 98 |
s_sections = find_sections(s_bytes, marker_pattern)
|
| 99 |
|
| 100 |
# Get a unique, sorted list of all question keys found (e.g., Q1, Q2, Q10)
|
| 101 |
+
all_keys = sorted(list(set(q_sections.keys()) | set(r_sections.keys()) | set(s_sections.keys())), key=lambda x: int(re.search(r'\d+', x).group()))
|
| 102 |
|
| 103 |
if not all_keys:
|
| 104 |
st.error("Could not find any sections with the provided marker. Please check your PDFs or refine the marker text.")
|
|
|
|
| 130 |
st.markdown("#### Question")
|
| 131 |
if item['question']:
|
| 132 |
with st.expander("ποΈ Preview"):
|
| 133 |
+
try:
|
| 134 |
+
preview_doc = fitz.open(stream=item['question'], filetype="pdf")
|
| 135 |
+
pix = preview_doc[0].get_pixmap()
|
| 136 |
+
st.image(pix.tobytes())
|
| 137 |
+
except Exception as e:
|
| 138 |
+
st.error(f"Could not generate preview: {e}")
|
| 139 |
st.download_button(
|
| 140 |
label="β¬οΈ Download PDF",
|
| 141 |
data=item['question'],
|
| 142 |
+
file_name=f"{item['key'].lower().replace(' ','_')}_question.pdf",
|
| 143 |
mime="application/pdf"
|
| 144 |
)
|
| 145 |
else:
|
|
|
|
| 149 |
st.markdown("#### Rubric")
|
| 150 |
if item['rubric']:
|
| 151 |
with st.expander("ποΈ Preview"):
|
| 152 |
+
try:
|
| 153 |
+
preview_doc = fitz.open(stream=item['rubric'], filetype="pdf")
|
| 154 |
+
pix = preview_doc[0].get_pixmap()
|
| 155 |
+
st.image(pix.tobytes())
|
| 156 |
+
except Exception as e:
|
| 157 |
+
st.error(f"Could not generate preview: {e}")
|
| 158 |
st.download_button(
|
| 159 |
label="β¬οΈ Download PDF",
|
| 160 |
data=item['rubric'],
|
| 161 |
+
file_name=f"{item['key'].lower().replace(' ','_')}_rubric.pdf",
|
| 162 |
mime="application/pdf"
|
| 163 |
)
|
| 164 |
else:
|
|
|
|
| 168 |
st.markdown("#### Solution")
|
| 169 |
if item['solution']:
|
| 170 |
with st.expander("ποΈ Preview"):
|
| 171 |
+
try:
|
| 172 |
+
preview_doc = fitz.open(stream=item['solution'], filetype="pdf")
|
| 173 |
+
pix = preview_doc[0].get_pixmap()
|
| 174 |
+
st.image(pix.tobytes())
|
| 175 |
+
except Exception as e:
|
| 176 |
+
st.error(f"Could not generate preview: {e}")
|
| 177 |
st.download_button(
|
| 178 |
label="β¬οΈ Download PDF",
|
| 179 |
data=item['solution'],
|
| 180 |
+
file_name=f"{item['key'].lower().replace(' ','_')}_solution.pdf",
|
| 181 |
mime="application/pdf"
|
| 182 |
)
|
| 183 |
else:
|