Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -43,6 +43,7 @@ with col2:
|
|
| 43 |
value=(25, 75)
|
| 44 |
)
|
| 45 |
|
|
|
|
| 46 |
|
| 47 |
if uploaded_file is not None:
|
| 48 |
|
|
@@ -80,127 +81,126 @@ if uploaded_file is not None:
|
|
| 80 |
st.write(summ)
|
| 81 |
|
| 82 |
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
format_list
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
st.session_state.page_index
|
| 113 |
-
|
| 114 |
-
st.session_state.
|
| 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 |
-
# Next button in col3, right aligned
|
| 143 |
-
with col3:
|
| 144 |
-
st.button("Next", on_click=turn_page, args=("next",))
|
| 145 |
-
|
| 146 |
-
# Display HTML box
|
| 147 |
-
st.markdown(st.session_state.current_text)
|
| 148 |
-
|
| 149 |
-
def render_markdown_to_html(markdown_str):
|
| 150 |
-
return markdown.markdown(markdown_str)
|
| 151 |
-
|
| 152 |
-
def create_pdf_from_markdown_strings(markdown_strings):
|
| 153 |
-
html_pages = [render_markdown_to_html(md) for md in markdown_strings]
|
| 154 |
-
|
| 155 |
-
# Combine HTML content with page breaks and add a style section for font size and margins
|
| 156 |
-
combined_html = '''
|
| 157 |
-
<html>
|
| 158 |
-
<head>
|
| 159 |
-
<style>
|
| 160 |
-
.page {
|
| 161 |
-
font-size: 16pt; /* Adjust the font size as needed */
|
| 162 |
-
}
|
| 163 |
-
</style>
|
| 164 |
-
</head>
|
| 165 |
-
<body>
|
| 166 |
-
'''
|
| 167 |
-
for i, page in enumerate(html_pages):
|
| 168 |
-
combined_html += f'<div class="page">{page}</div>'
|
| 169 |
-
if i < len(html_pages) - 1: # Only add page break after if it's not the last page
|
| 170 |
-
combined_html += '<div style="page-break-after: always;"></div>'
|
| 171 |
-
combined_html += '</body></html>'
|
| 172 |
-
|
| 173 |
-
# PDF options: landscape orientation and page size
|
| 174 |
-
options = {
|
| 175 |
-
'page-size': 'A4',
|
| 176 |
-
'orientation': 'Landscape'
|
| 177 |
-
}
|
| 178 |
-
|
| 179 |
-
return combined_html
|
| 180 |
-
|
| 181 |
-
def generate_pdf(html_string):
|
| 182 |
-
css = """
|
| 183 |
-
@page {
|
| 184 |
-
size: A4 landscape;
|
| 185 |
-
margin: 20mm;
|
| 186 |
-
}
|
| 187 |
-
body {
|
| 188 |
-
font-family: sans-serif;
|
| 189 |
-
}
|
| 190 |
-
"""
|
| 191 |
-
pdf = BytesIO()
|
| 192 |
-
HTML(string=html_string).write_pdf(pdf, stylesheets=[CSS(string=css)])
|
| 193 |
-
pdf.seek(0)
|
| 194 |
-
return pdf
|
| 195 |
-
|
| 196 |
-
html_content = create_pdf_from_markdown_strings(st.session_state.summ_text)
|
| 197 |
-
pdf_file = generate_pdf(html_content)
|
| 198 |
-
|
| 199 |
-
# Provide download link
|
| 200 |
-
st.download_button(
|
| 201 |
-
label="Download PDF",
|
| 202 |
-
data=pdf_file,
|
| 203 |
-
file_name="slides.pdf",
|
| 204 |
-
mime="application/pdf"
|
| 205 |
-
)
|
| 206 |
-
|
|
|
|
| 43 |
value=(25, 75)
|
| 44 |
)
|
| 45 |
|
| 46 |
+
summ_text = None
|
| 47 |
|
| 48 |
if uploaded_file is not None:
|
| 49 |
|
|
|
|
| 81 |
st.write(summ)
|
| 82 |
|
| 83 |
|
| 84 |
+
if summ_text is not None:
|
| 85 |
+
|
| 86 |
+
# Function to render HTML content
|
| 87 |
+
def format(text_list):
|
| 88 |
+
format_list = []
|
| 89 |
+
for text in text_list:
|
| 90 |
+
# Split text by periods
|
| 91 |
+
sentences = text.split('.')
|
| 92 |
+
# Create HTML list items
|
| 93 |
+
list_items = "".join([f"- {sentence.strip()}.\n" for sentence in sentences if sentence.strip()])
|
| 94 |
+
format_list.append(list_items)
|
| 95 |
+
return format_list
|
| 96 |
+
|
| 97 |
+
# Initialize session state for page index and text
|
| 98 |
+
if 'page_index' not in st.session_state:
|
| 99 |
+
st.session_state.page_index = 0
|
| 100 |
+
|
| 101 |
+
if 'summ_text' not in st.session_state:
|
| 102 |
+
st.session_state.summ_text = format(summ_text)
|
| 103 |
+
|
| 104 |
+
if 'current_text' not in st.session_state:
|
| 105 |
+
st.session_state.current_text = st.session_state.summ_text[st.session_state.page_index]
|
| 106 |
+
|
| 107 |
+
|
| 108 |
+
|
| 109 |
+
# Function to handle page turn
|
| 110 |
+
def turn_page(direction):
|
| 111 |
+
if direction == "next" and st.session_state.page_index < len(summ_text) - 1:
|
| 112 |
+
st.session_state.page_index += 1
|
| 113 |
+
elif direction == "prev" and st.session_state.page_index > 0:
|
| 114 |
+
st.session_state.page_index -= 1
|
| 115 |
+
st.session_state.current_text = st.session_state.summ_text[st.session_state.page_index]
|
| 116 |
+
|
| 117 |
+
# Function to update the current text based on text_area changes
|
| 118 |
+
def update_text():
|
| 119 |
+
st.session_state.summ_text[st.session_state.page_index] = st.session_state.text_area_value
|
| 120 |
+
st.session_state.current_text = st.session_state.text_area_value
|
| 121 |
+
|
| 122 |
+
|
| 123 |
+
# Display editable text box
|
| 124 |
+
text = st.text_area("Edit Text", st.session_state.current_text, height=200, key="text_area_value", on_change=update_text)
|
| 125 |
+
|
| 126 |
+
# Display page turner controls
|
| 127 |
+
col1, col2, col3 = st.columns([2.25, 12, 1.7])
|
| 128 |
+
|
| 129 |
+
# Previous button in col1
|
| 130 |
+
with col1:
|
| 131 |
+
st.button("Previous", on_click=turn_page, args=("prev",))
|
| 132 |
+
|
| 133 |
+
# Center aligned text in col2
|
| 134 |
+
with col2:
|
| 135 |
+
st.markdown(
|
| 136 |
+
f'<div style="display: flex; justify-content: center; align-items: center; height: 100%;">'
|
| 137 |
+
f'Page {st.session_state.page_index + 1} of {len(summ_text)}'
|
| 138 |
+
f'</div>',
|
| 139 |
+
unsafe_allow_html=True
|
| 140 |
+
)
|
| 141 |
+
|
| 142 |
+
# Next button in col3, right aligned
|
| 143 |
+
with col3:
|
| 144 |
+
st.button("Next", on_click=turn_page, args=("next",))
|
| 145 |
+
|
| 146 |
+
# Display HTML box
|
| 147 |
+
st.markdown(st.session_state.current_text)
|
| 148 |
+
|
| 149 |
+
def render_markdown_to_html(markdown_str):
|
| 150 |
+
return markdown.markdown(markdown_str)
|
| 151 |
+
|
| 152 |
+
def create_pdf_from_markdown_strings(markdown_strings):
|
| 153 |
+
html_pages = [render_markdown_to_html(md) for md in markdown_strings]
|
| 154 |
+
|
| 155 |
+
# Combine HTML content with page breaks and add a style section for font size and margins
|
| 156 |
+
combined_html = '''
|
| 157 |
+
<html>
|
| 158 |
+
<head>
|
| 159 |
+
<style>
|
| 160 |
+
.page {
|
| 161 |
+
font-size: 16pt; /* Adjust the font size as needed */
|
| 162 |
+
}
|
| 163 |
+
</style>
|
| 164 |
+
</head>
|
| 165 |
+
<body>
|
| 166 |
+
'''
|
| 167 |
+
for i, page in enumerate(html_pages):
|
| 168 |
+
combined_html += f'<div class="page">{page}</div>'
|
| 169 |
+
if i < len(html_pages) - 1: # Only add page break after if it's not the last page
|
| 170 |
+
combined_html += '<div style="page-break-after: always;"></div>'
|
| 171 |
+
combined_html += '</body></html>'
|
| 172 |
+
|
| 173 |
+
# PDF options: landscape orientation and page size
|
| 174 |
+
options = {
|
| 175 |
+
'page-size': 'A4',
|
| 176 |
+
'orientation': 'Landscape'
|
| 177 |
+
}
|
| 178 |
+
|
| 179 |
+
return combined_html
|
| 180 |
+
|
| 181 |
+
def generate_pdf(html_string):
|
| 182 |
+
css = """
|
| 183 |
+
@page {
|
| 184 |
+
size: A4 landscape;
|
| 185 |
+
margin: 20mm;
|
| 186 |
+
}
|
| 187 |
+
body {
|
| 188 |
+
font-family: sans-serif;
|
| 189 |
+
}
|
| 190 |
+
"""
|
| 191 |
+
pdf = BytesIO()
|
| 192 |
+
HTML(string=html_string).write_pdf(pdf, stylesheets=[CSS(string=css)])
|
| 193 |
+
pdf.seek(0)
|
| 194 |
+
return pdf
|
| 195 |
+
|
| 196 |
+
html_content = create_pdf_from_markdown_strings(st.session_state.summ_text)
|
| 197 |
+
pdf_file = generate_pdf(html_content)
|
| 198 |
+
|
| 199 |
+
# Provide download link
|
| 200 |
+
st.download_button(
|
| 201 |
+
label="Download PDF",
|
| 202 |
+
data=pdf_file,
|
| 203 |
+
file_name="slides.pdf",
|
| 204 |
+
mime="application/pdf"
|
| 205 |
)
|
| 206 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|