Update app.py
Browse files
app.py
CHANGED
|
@@ -4,7 +4,6 @@ import os
|
|
| 4 |
import glob
|
| 5 |
import asyncio
|
| 6 |
import hashlib
|
| 7 |
-
import base64
|
| 8 |
import unicodedata
|
| 9 |
import streamlit as st
|
| 10 |
from PIL import Image
|
|
@@ -19,38 +18,13 @@ from reportlab.pdfbase.ttfonts import TTFont
|
|
| 19 |
|
| 20 |
st.set_page_config(layout="wide", initial_sidebar_state="collapsed")
|
| 21 |
|
| 22 |
-
def
|
| 23 |
-
|
| 24 |
-
for line in markdown_text.splitlines():
|
| 25 |
-
if line.strip().startswith("#"):
|
| 26 |
-
title = line.strip().lstrip("#").strip()
|
| 27 |
-
title = re.sub(r'[^\w\s-]', '', title)
|
| 28 |
-
if title:
|
| 29 |
-
return title.replace(" ", "_")
|
| 30 |
-
return "output"
|
| 31 |
-
|
| 32 |
-
async def generate_audio(text, voice, markdown_text):
|
| 33 |
-
# Generate an audio file using the file title from the markdown.
|
| 34 |
-
title = get_file_title_from_markdown(markdown_text)
|
| 35 |
-
filename = f"{title}.mp3"
|
| 36 |
communicate = edge_tts.Communicate(text, voice)
|
| 37 |
await communicate.save(filename)
|
| 38 |
return filename
|
| 39 |
|
| 40 |
-
def
|
| 41 |
-
# Generate a base64 download link for a given file.
|
| 42 |
-
with open(file, "rb") as f:
|
| 43 |
-
b64 = base64.b64encode(f.read()).decode()
|
| 44 |
-
if file_type == "mp3":
|
| 45 |
-
mime = "audio/mpeg"
|
| 46 |
-
elif file_type == "pdf":
|
| 47 |
-
mime = "application/pdf"
|
| 48 |
-
else:
|
| 49 |
-
mime = "application/octet-stream"
|
| 50 |
-
return f'<a href="data:{mime};base64,{b64}" download="{os.path.basename(file)}">Download {os.path.basename(file)}</a>'
|
| 51 |
-
|
| 52 |
-
def apply_emoji_font(text, font_name):
|
| 53 |
-
# Replace emojis in the text with HTML font tags using the specified font.
|
| 54 |
emoji_pattern = re.compile(
|
| 55 |
r"([\U0001F300-\U0001F5FF"
|
| 56 |
r"\U0001F600-\U0001F64F"
|
|
@@ -59,25 +33,25 @@ def apply_emoji_font(text, font_name):
|
|
| 59 |
r"\U0001F780-\U0001F7FF"
|
| 60 |
r"\U0001F800-\U0001F8FF"
|
| 61 |
r"\U0001F900-\U0001F9FF"
|
| 62 |
-
r"\
|
| 63 |
-
r"\
|
| 64 |
r"\u2600-\u26FF"
|
| 65 |
r"\u2700-\u27BF]+)"
|
| 66 |
)
|
| 67 |
def replace_emoji(match):
|
| 68 |
emoji = match.group(1)
|
| 69 |
emoji = unicodedata.normalize('NFC', emoji)
|
| 70 |
-
return f'<font face="{
|
| 71 |
segments = []
|
| 72 |
last_pos = 0
|
| 73 |
for match in emoji_pattern.finditer(text):
|
| 74 |
start, end = match.span()
|
| 75 |
if last_pos < start:
|
| 76 |
-
segments.append(f'<font face="
|
| 77 |
segments.append(replace_emoji(match))
|
| 78 |
last_pos = end
|
| 79 |
if last_pos < len(text):
|
| 80 |
-
segments.append(f'<font face="
|
| 81 |
return ''.join(segments)
|
| 82 |
|
| 83 |
def markdown_to_pdf_content(markdown_text, render_with_bold, auto_bold_numbers):
|
|
@@ -89,7 +63,6 @@ def markdown_to_pdf_content(markdown_text, render_with_bold, auto_bold_numbers):
|
|
| 89 |
if not line or line.startswith('# '):
|
| 90 |
continue
|
| 91 |
if render_with_bold:
|
| 92 |
-
# Replace markdown bold markers with HTML bold tags.
|
| 93 |
line = re.sub(r'\*\*(.*?)\*\*', r'<b>\1</b>', line)
|
| 94 |
if auto_bold_numbers and number_pattern.match(line):
|
| 95 |
if not (line.startswith("<b>") and line.endswith("</b>")):
|
|
@@ -98,18 +71,17 @@ def markdown_to_pdf_content(markdown_text, render_with_bold, auto_bold_numbers):
|
|
| 98 |
total_lines = len(pdf_content)
|
| 99 |
return pdf_content, total_lines
|
| 100 |
|
| 101 |
-
def create_pdf(markdown_text, base_font_size, render_with_bold, auto_bold_numbers, enlarge_numbered, num_columns
|
| 102 |
buffer = io.BytesIO()
|
| 103 |
page_width = A4[0] * 2
|
| 104 |
page_height = A4[1]
|
| 105 |
-
doc = SimpleDocTemplate(buffer, pagesize=(page_width, page_height),
|
| 106 |
-
leftMargin=36, rightMargin=36, topMargin=36, bottomMargin=36)
|
| 107 |
styles = getSampleStyleSheet()
|
| 108 |
spacer_height = 10
|
|
|
|
| 109 |
pdf_content, total_lines = markdown_to_pdf_content(markdown_text, render_with_bold, auto_bold_numbers)
|
| 110 |
-
# Define paragraph styles.
|
| 111 |
item_style = ParagraphStyle(
|
| 112 |
-
'ItemStyle', parent=styles['Normal'], fontName=
|
| 113 |
fontSize=base_font_size, leading=base_font_size * 1.15, spaceAfter=1
|
| 114 |
)
|
| 115 |
bold_style = ParagraphStyle(
|
|
@@ -119,33 +91,28 @@ def create_pdf(markdown_text, base_font_size, render_with_bold, auto_bold_number
|
|
| 119 |
numbered_bold_style = ParagraphStyle(
|
| 120 |
'NumberedBoldStyle', parent=styles['Normal'], fontName="NotoEmoji-Bold",
|
| 121 |
fontSize=base_font_size + 1 if enlarge_numbered else base_font_size,
|
| 122 |
-
leading=(base_font_size + 1) * 1.15 if enlarge_numbered else base_font_size * 1.15,
|
| 123 |
-
spaceAfter=1
|
| 124 |
)
|
| 125 |
section_style = ParagraphStyle(
|
| 126 |
-
'SectionStyle', parent=styles['Heading2'], fontName=
|
| 127 |
-
textColor=colors.darkblue, fontSize=base_font_size * 1.1,
|
| 128 |
-
leading=base_font_size * 1.32, spaceAfter=2
|
| 129 |
)
|
| 130 |
try:
|
| 131 |
available_font_files = glob.glob("*.ttf")
|
| 132 |
if not available_font_files:
|
| 133 |
st.error("No .ttf font files found in the current directory.")
|
| 134 |
return
|
| 135 |
-
|
| 136 |
-
selected_bold_font = None
|
| 137 |
for f in available_font_files:
|
| 138 |
if "NotoEmoji-Bold" in f:
|
| 139 |
-
|
| 140 |
break
|
| 141 |
-
if
|
| 142 |
-
pdfmetrics.registerFont(TTFont("NotoEmoji-Bold",
|
| 143 |
-
|
| 144 |
-
pdfmetrics.registerFont(TTFont(emoji_font, available_font_files[0]))
|
| 145 |
except Exception as e:
|
| 146 |
st.error(f"Font registration error: {e}")
|
| 147 |
return
|
| 148 |
-
# Split content into columns.
|
| 149 |
columns = [[] for _ in range(num_columns)]
|
| 150 |
lines_per_column = total_lines / num_columns if num_columns > 0 else total_lines
|
| 151 |
current_line_count = 0
|
|
@@ -160,7 +127,6 @@ def create_pdf(markdown_text, base_font_size, render_with_bold, auto_bold_number
|
|
| 160 |
column_cells = [[] for _ in range(num_columns)]
|
| 161 |
for col_idx, column in enumerate(columns):
|
| 162 |
for item in column:
|
| 163 |
-
# If the item is bold (wrapped in <b> tags), use bold style; otherwise, use the selected emoji font.
|
| 164 |
if isinstance(item, str) and item.startswith("<b>") and item.endswith("</b>"):
|
| 165 |
content = item[3:-4].strip()
|
| 166 |
if number_pattern.match(content):
|
|
@@ -168,7 +134,7 @@ def create_pdf(markdown_text, base_font_size, render_with_bold, auto_bold_number
|
|
| 168 |
else:
|
| 169 |
column_cells[col_idx].append(Paragraph(apply_emoji_font(content, "NotoEmoji-Bold"), section_style))
|
| 170 |
else:
|
| 171 |
-
column_cells[col_idx].append(Paragraph(apply_emoji_font(item,
|
| 172 |
max_cells = max(len(cells) for cells in column_cells) if column_cells else 0
|
| 173 |
for cells in column_cells:
|
| 174 |
cells.extend([Paragraph("", item_style)] * (max_cells - len(cells)))
|
|
@@ -205,82 +171,221 @@ def pdf_to_image(pdf_bytes):
|
|
| 205 |
st.error(f"Failed to render PDF preview: {e}")
|
| 206 |
return None
|
| 207 |
|
| 208 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 209 |
md_files = [f for f in glob.glob("*.md") if os.path.basename(f) != "README.md"]
|
| 210 |
md_options = [os.path.splitext(os.path.basename(f))[0] for f in md_files]
|
| 211 |
-
if md_options:
|
| 212 |
-
if 'markdown_content' not in st.session_state or not st.session_state.markdown_content:
|
| 213 |
-
with open(f"{md_options[0]}.md", "r", encoding="utf-8") as f:
|
| 214 |
-
st.session_state.markdown_content = f.read()
|
| 215 |
-
else:
|
| 216 |
-
st.session_state.markdown_content = ""
|
| 217 |
|
| 218 |
with st.sidebar:
|
| 219 |
st.markdown("### PDF Options")
|
| 220 |
selected_md = st.selectbox("Select Markdown File", options=md_options, index=0 if md_options else -1)
|
| 221 |
available_font_files = {os.path.splitext(os.path.basename(f))[0]: f for f in glob.glob("*.ttf")}
|
| 222 |
-
|
| 223 |
-
selected_font_name = st.selectbox("Select Emoji Font", options=list(available_font_files.keys()),
|
| 224 |
-
index=list(available_font_files.keys()).index("NotoEmoji-Bold") if "NotoEmoji-Bold" in available_font_files else 0)
|
| 225 |
base_font_size = st.slider("Font Size (points)", min_value=6, max_value=16, value=8, step=1)
|
| 226 |
render_with_bold = st.checkbox("Render with Bold Formatting (remove ** markers)", value=True, key="render_with_bold")
|
| 227 |
auto_bold_numbers = st.checkbox("Auto Bold Numbered Lines", value=True, key="auto_bold_numbers")
|
| 228 |
enlarge_numbered = st.checkbox("Enlarge Font Size for Numbered Lines", value=True, key="enlarge_numbered")
|
| 229 |
num_columns = st.selectbox("Number of Columns", options=[1, 2, 3, 4, 5, 6], index=3)
|
|
|
|
|
|
|
| 230 |
if md_options and selected_md:
|
| 231 |
with open(f"{selected_md}.md", "r", encoding="utf-8") as f:
|
| 232 |
st.session_state.markdown_content = f.read()
|
| 233 |
-
edited_markdown = st.text_area("Modify the markdown content below:",
|
| 234 |
-
value=st.session_state.markdown_content,
|
| 235 |
-
height=300, key=f"markdown_{selected_md}_{selected_font_name}_{num_columns}")
|
| 236 |
if st.button("Update PDF"):
|
| 237 |
st.session_state.markdown_content = edited_markdown
|
| 238 |
if md_options and selected_md:
|
| 239 |
with open(f"{selected_md}.md", "w", encoding="utf-8") as f:
|
| 240 |
f.write(edited_markdown)
|
| 241 |
st.experimental_rerun()
|
| 242 |
-
st.download_button(label="Save Markdown", data=st.session_state.markdown_content,
|
| 243 |
-
file_name=f"{selected_md}.md" if selected_md else "default.md", mime="text/markdown")
|
| 244 |
st.markdown("### Text-to-Speech")
|
| 245 |
VOICES = ["en-US-AriaNeural", "en-US-JennyNeural", "en-GB-SoniaNeural", "en-US-GuyNeural", "en-US-AnaNeural"]
|
| 246 |
selected_voice = st.selectbox("Select Voice for TTS", options=VOICES, index=0)
|
| 247 |
if st.button("Generate Audio"):
|
| 248 |
-
|
| 249 |
-
audio_file = asyncio.run(generate_audio(st.session_state.markdown_content, selected_voice, st.session_state.markdown_content))
|
| 250 |
st.audio(audio_file)
|
| 251 |
with open(audio_file, "rb") as f:
|
| 252 |
audio_bytes = f.read()
|
| 253 |
-
st.download_button("Download Audio", data=audio_bytes,
|
| 254 |
-
file_name=os.path.basename(audio_file), mime="audio/mpeg")
|
| 255 |
-
if st.button("Save PDF"):
|
| 256 |
-
title = get_file_title_from_markdown(st.session_state.markdown_content)
|
| 257 |
-
pdf_filename = f"{title}.pdf"
|
| 258 |
-
with open(pdf_filename, "wb") as f:
|
| 259 |
-
f.write(pdf_bytes)
|
| 260 |
-
st.success(f"Saved PDF as {pdf_filename}")
|
| 261 |
-
st.experimental_rerun()
|
| 262 |
-
|
| 263 |
-
st.markdown("### Saved Audio Files")
|
| 264 |
-
mp3_files = glob.glob("*.mp3")
|
| 265 |
-
for mp3 in mp3_files:
|
| 266 |
-
st.audio(mp3)
|
| 267 |
-
st.markdown(get_download_link(mp3, "mp3"), unsafe_allow_html=True)
|
| 268 |
-
if st.button("Delete All MP3"):
|
| 269 |
-
for mp3 in mp3_files:
|
| 270 |
-
try:
|
| 271 |
-
os.remove(mp3)
|
| 272 |
-
except Exception as e:
|
| 273 |
-
st.error(f"Error deleting {mp3}: {e}")
|
| 274 |
-
st.experimental_rerun()
|
| 275 |
-
|
| 276 |
-
st.markdown("### Saved PDF Files")
|
| 277 |
-
pdf_files = glob.glob("*.pdf")
|
| 278 |
-
for pdf in pdf_files:
|
| 279 |
-
st.markdown(get_download_link(pdf, "pdf"), unsafe_allow_html=True)
|
| 280 |
|
| 281 |
with st.spinner("Generating PDF..."):
|
| 282 |
-
pdf_bytes = create_pdf(st.session_state.markdown_content, base_font_size, render_with_bold,
|
| 283 |
-
auto_bold_numbers, enlarge_numbered, num_columns, selected_font_name)
|
| 284 |
|
| 285 |
with st.container():
|
| 286 |
pdf_images = pdf_to_image(pdf_bytes)
|
|
|
|
| 4 |
import glob
|
| 5 |
import asyncio
|
| 6 |
import hashlib
|
|
|
|
| 7 |
import unicodedata
|
| 8 |
import streamlit as st
|
| 9 |
from PIL import Image
|
|
|
|
| 18 |
|
| 19 |
st.set_page_config(layout="wide", initial_sidebar_state="collapsed")
|
| 20 |
|
| 21 |
+
async def generate_audio(text, voice):
|
| 22 |
+
filename = f"{hashlib.md5(text.encode()).hexdigest()}_{voice}.mp3"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
communicate = edge_tts.Communicate(text, voice)
|
| 24 |
await communicate.save(filename)
|
| 25 |
return filename
|
| 26 |
|
| 27 |
+
def apply_emoji_font(text, emoji_font):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 28 |
emoji_pattern = re.compile(
|
| 29 |
r"([\U0001F300-\U0001F5FF"
|
| 30 |
r"\U0001F600-\U0001F64F"
|
|
|
|
| 33 |
r"\U0001F780-\U0001F7FF"
|
| 34 |
r"\U0001F800-\U0001F8FF"
|
| 35 |
r"\U0001F900-\U0001F9FF"
|
| 36 |
+
r"\U0001FA00-\U0001FA6F"
|
| 37 |
+
r"\U0001FA70-\U0001FAFF"
|
| 38 |
r"\u2600-\u26FF"
|
| 39 |
r"\u2700-\u27BF]+)"
|
| 40 |
)
|
| 41 |
def replace_emoji(match):
|
| 42 |
emoji = match.group(1)
|
| 43 |
emoji = unicodedata.normalize('NFC', emoji)
|
| 44 |
+
return f'<font face="{emoji_font}">{emoji}</font>'
|
| 45 |
segments = []
|
| 46 |
last_pos = 0
|
| 47 |
for match in emoji_pattern.finditer(text):
|
| 48 |
start, end = match.span()
|
| 49 |
if last_pos < start:
|
| 50 |
+
segments.append(f'<font face="DejaVuSans">{text[last_pos:start]}</font>')
|
| 51 |
segments.append(replace_emoji(match))
|
| 52 |
last_pos = end
|
| 53 |
if last_pos < len(text):
|
| 54 |
+
segments.append(f'<font face="DejaVuSans">{text[last_pos:]}</font>')
|
| 55 |
return ''.join(segments)
|
| 56 |
|
| 57 |
def markdown_to_pdf_content(markdown_text, render_with_bold, auto_bold_numbers):
|
|
|
|
| 63 |
if not line or line.startswith('# '):
|
| 64 |
continue
|
| 65 |
if render_with_bold:
|
|
|
|
| 66 |
line = re.sub(r'\*\*(.*?)\*\*', r'<b>\1</b>', line)
|
| 67 |
if auto_bold_numbers and number_pattern.match(line):
|
| 68 |
if not (line.startswith("<b>") and line.endswith("</b>")):
|
|
|
|
| 71 |
total_lines = len(pdf_content)
|
| 72 |
return pdf_content, total_lines
|
| 73 |
|
| 74 |
+
def create_pdf(markdown_text, base_font_size, render_with_bold, auto_bold_numbers, enlarge_numbered, num_columns):
|
| 75 |
buffer = io.BytesIO()
|
| 76 |
page_width = A4[0] * 2
|
| 77 |
page_height = A4[1]
|
| 78 |
+
doc = SimpleDocTemplate(buffer, pagesize=(page_width, page_height), leftMargin=36, rightMargin=36, topMargin=36, bottomMargin=36)
|
|
|
|
| 79 |
styles = getSampleStyleSheet()
|
| 80 |
spacer_height = 10
|
| 81 |
+
section_spacer_height = 15
|
| 82 |
pdf_content, total_lines = markdown_to_pdf_content(markdown_text, render_with_bold, auto_bold_numbers)
|
|
|
|
| 83 |
item_style = ParagraphStyle(
|
| 84 |
+
'ItemStyle', parent=styles['Normal'], fontName="DejaVuSans",
|
| 85 |
fontSize=base_font_size, leading=base_font_size * 1.15, spaceAfter=1
|
| 86 |
)
|
| 87 |
bold_style = ParagraphStyle(
|
|
|
|
| 91 |
numbered_bold_style = ParagraphStyle(
|
| 92 |
'NumberedBoldStyle', parent=styles['Normal'], fontName="NotoEmoji-Bold",
|
| 93 |
fontSize=base_font_size + 1 if enlarge_numbered else base_font_size,
|
| 94 |
+
leading=(base_font_size + 1) * 1.15 if enlarge_numbered else base_font_size * 1.15, spaceAfter=1
|
|
|
|
| 95 |
)
|
| 96 |
section_style = ParagraphStyle(
|
| 97 |
+
'SectionStyle', parent=styles['Heading2'], fontName="DejaVuSans",
|
| 98 |
+
textColor=colors.darkblue, fontSize=base_font_size * 1.1, leading=base_font_size * 1.32, spaceAfter=2
|
|
|
|
| 99 |
)
|
| 100 |
try:
|
| 101 |
available_font_files = glob.glob("*.ttf")
|
| 102 |
if not available_font_files:
|
| 103 |
st.error("No .ttf font files found in the current directory.")
|
| 104 |
return
|
| 105 |
+
selected_font_path = None
|
|
|
|
| 106 |
for f in available_font_files:
|
| 107 |
if "NotoEmoji-Bold" in f:
|
| 108 |
+
selected_font_path = f
|
| 109 |
break
|
| 110 |
+
if selected_font_path:
|
| 111 |
+
pdfmetrics.registerFont(TTFont("NotoEmoji-Bold", selected_font_path))
|
| 112 |
+
pdfmetrics.registerFont(TTFont("DejaVuSans", "DejaVuSans.ttf"))
|
|
|
|
| 113 |
except Exception as e:
|
| 114 |
st.error(f"Font registration error: {e}")
|
| 115 |
return
|
|
|
|
| 116 |
columns = [[] for _ in range(num_columns)]
|
| 117 |
lines_per_column = total_lines / num_columns if num_columns > 0 else total_lines
|
| 118 |
current_line_count = 0
|
|
|
|
| 127 |
column_cells = [[] for _ in range(num_columns)]
|
| 128 |
for col_idx, column in enumerate(columns):
|
| 129 |
for item in column:
|
|
|
|
| 130 |
if isinstance(item, str) and item.startswith("<b>") and item.endswith("</b>"):
|
| 131 |
content = item[3:-4].strip()
|
| 132 |
if number_pattern.match(content):
|
|
|
|
| 134 |
else:
|
| 135 |
column_cells[col_idx].append(Paragraph(apply_emoji_font(content, "NotoEmoji-Bold"), section_style))
|
| 136 |
else:
|
| 137 |
+
column_cells[col_idx].append(Paragraph(apply_emoji_font(item, "DejaVuSans"), item_style))
|
| 138 |
max_cells = max(len(cells) for cells in column_cells) if column_cells else 0
|
| 139 |
for cells in column_cells:
|
| 140 |
cells.extend([Paragraph("", item_style)] * (max_cells - len(cells)))
|
|
|
|
| 171 |
st.error(f"Failed to render PDF preview: {e}")
|
| 172 |
return None
|
| 173 |
|
| 174 |
+
default_markdown = """# Deities Guide: Mythology and Moral Lessons ๐โจ
|
| 175 |
+
|
| 176 |
+
1. ๐ **Introduction**
|
| 177 |
+
- **Purpose**: Explore deities, spirits, saints, and beings with their epic stories and morals! ๐๐
|
| 178 |
+
- **Usage**: A guide for learning and storytelling across traditions. ๐ญโ๏ธ
|
| 179 |
+
- **Themes**: Justice โ๏ธ, faith ๐, hubris ๐ค, redemption ๐, cosmic order ๐.
|
| 180 |
+
|
| 181 |
+
2. ๐ ๏ธ **Core Concepts of Divinity**
|
| 182 |
+
- **Powers**: Creation ๐, omniscience ๐๏ธโ๐จ๏ธ, shapeshifting ๐ฆ across entities.
|
| 183 |
+
- **Life Cycle**: Mortality ๐, immortality โจ, transitions like saints and avatars ๐.
|
| 184 |
+
- **Communication**: Omens ๐ฉ๏ธ, visions ๐๏ธ, miracles โจ from gods and spirits.
|
| 185 |
+
|
| 186 |
+
3. โก **Standard Abilities**
|
| 187 |
+
- **Creation**: Gods and spirits shape worlds, e.g., Allah ๐ and Vishnu ๐.
|
| 188 |
+
- **Influence**: Saints and prophets intercede, like Muhammad ๐ and Paul โ๏ธ.
|
| 189 |
+
- **Transformation**: Angels and avatars shift forms, e.g., Gabriel ๐ and Krishna ๐ฆ.
|
| 190 |
+
- **Knowledge**: Foresight ๐ฎ or revelation ๐, as with the Holy Spirit ๐๏ธ and Brahma ๐ง .
|
| 191 |
+
- **Judgment**: Divine authority ๐, e.g., Yahweh โ๏ธ and Yama ๐.
|
| 192 |
+
|
| 193 |
+
4. โณ **Mortality and Immortality**
|
| 194 |
+
- **Gods**: Eternal โฐ, like Allah ๐ and Shiva ๐๏ธ.
|
| 195 |
+
- **Spirits**: Realm-bound ๐ , e.g., jinn ๐ฅ and devas โจ.
|
| 196 |
+
- **Saints/Prophets**: Mortal to divine ๐โก๏ธ๐, e.g., Moses ๐ and Rama ๐น.
|
| 197 |
+
- **Beings**: Limbo states โ, like cherubim ๐ and rakshasas ๐น.
|
| 198 |
+
- **Lessons**: Faith ๐ and duty โ๏ธ define transitions.
|
| 199 |
+
|
| 200 |
+
5. ๐ **Ascension and Signs**
|
| 201 |
+
- **Paths**: Birth ๐ถ, deeds ๐ก๏ธ, revelation ๐, as with Jesus โ๏ธ and Arjuna ๐น.
|
| 202 |
+
- **Signs**: Miracles โจ and prophecies ๐ฎ, like those in the Quran ๐ and Gita ๐.
|
| 203 |
+
- **Morals**: Obedience ๐ง and devotion โค๏ธ shape destiny ๐.
|
| 204 |
+
|
| 205 |
+
6. ๐ฒ **Storytelling and Games**
|
| 206 |
+
- **Portrayal**: Gods, spirits, and saints in narratives or RPGs ๐ฎ๐.
|
| 207 |
+
- **Dynamics**: Clerics โช, imams ๐, and sadhus ๐ง serve higher powers.
|
| 208 |
+
- **Balance**: Power ๐ช vs. personality ๐ for depth.
|
| 209 |
+
|
| 210 |
+
7. ๐ฎ **Dungeon Mastering Beings**
|
| 211 |
+
- **Gods**: Epic scope ๐, e.g., Allah โจ and Vishnu ๐.
|
| 212 |
+
- **Spirits**: Local influence ๐๏ธ, like jinn ๐ฅ and apsaras ๐.
|
| 213 |
+
- **Saints**: Moral anchors โ, e.g., St. Francis ๐พ and Ali โ๏ธ.
|
| 214 |
+
|
| 215 |
+
8. ๐ **Devotee Relationships**
|
| 216 |
+
- **Clerics**: Serve gods, e.g., Krishnaโs priests ๐ฆ.
|
| 217 |
+
- **Mediums**: Channel spirits, like jinn whisperers ๐ฅ๐๏ธ.
|
| 218 |
+
- **Faithful**: Venerate saints and prophets, e.g., Fatimaโs followers ๐น.
|
| 219 |
+
|
| 220 |
+
9. ๐ฆ
**American Indian Traditions**
|
| 221 |
+
- **Coyote, Raven, White Buffalo Woman**: Trickster kin ๐ฆ๐ฆ and wise mother ๐.
|
| 222 |
+
- **Relation**: Siblings and guide teach balance โ๏ธ.
|
| 223 |
+
- **Lesson**: Chaos ๐ช๏ธ breeds wisdom ๐ง .
|
| 224 |
+
|
| 225 |
+
10. โ๏ธ **Arthurian Legends**
|
| 226 |
+
- **Merlin, Morgan le Fay, Arthur**: Mentor ๐ง, rival ๐งโโ๏ธ, son ๐.
|
| 227 |
+
- **Relation**: Family tests loyalty ๐ค.
|
| 228 |
+
- **Lesson**: Honor ๐ก๏ธ vs. betrayal ๐ก๏ธ.
|
| 229 |
+
|
| 230 |
+
11. ๐๏ธ **Babylonian Mythology**
|
| 231 |
+
- **Marduk, Tiamat, Ishtar**: Son โ๏ธ, mother ๐, lover โค๏ธ.
|
| 232 |
+
- **Relation**: Kinship drives order ๐ฐ.
|
| 233 |
+
- **Lesson**: Power ๐ช reshapes chaos ๐ช๏ธ.
|
| 234 |
+
|
| 235 |
+
12. โ๏ธ **Christian Trinity**
|
| 236 |
+
- **God (Yahweh), Jesus, Holy Spirit**: Father ๐, Son โ๏ธ, Spirit ๐๏ธ.
|
| 237 |
+
- **Relation**: Divine family redeems ๐.
|
| 238 |
+
- **Lesson**: Faith ๐ restores grace โจ.
|
| 239 |
+
|
| 240 |
+
13. ๐ **Christian Saints & Angels**
|
| 241 |
+
- **St. Michael, Gabriel, Mary**: Warrior โ๏ธ, messenger ๐, mother ๐น.
|
| 242 |
+
- **Relation**: Heavenly kin serve God ๐.
|
| 243 |
+
- **Lesson**: Duty โ๏ธ upholds divine will ๐.
|
| 244 |
+
|
| 245 |
+
14. ๐ **Celtic Mythology**
|
| 246 |
+
- **Lugh, Morrigan, Cernunnos**: Son โ๏ธ, mother ๐ฆ, father ๐ฆ.
|
| 247 |
+
- **Relation**: Family governs cycles ๐.
|
| 248 |
+
- **Lesson**: Courage ๐ช in fate ๐ฒ.
|
| 249 |
+
|
| 250 |
+
15. ๐ **Central American Traditions**
|
| 251 |
+
- **Quetzalcoatl, Tezcatlipoca, Huitzilopochtli**: Brothers ๐๐ and war son โ๏ธ.
|
| 252 |
+
- **Relation**: Sibling rivalry creates ๐.
|
| 253 |
+
- **Lesson**: Sacrifice ๐ฉธ builds worlds ๐ฐ.
|
| 254 |
+
|
| 255 |
+
16. ๐ **Chinese Mythology**
|
| 256 |
+
- **Jade Emperor, Nuwa, Sun Wukong**: Father ๐, mother ๐, rebel son ๐.
|
| 257 |
+
- **Relation**: Family enforces harmony ๐ถ.
|
| 258 |
+
- **Lesson**: Duty โ๏ธ curbs chaos ๐ช๏ธ.
|
| 259 |
+
|
| 260 |
+
17. ๐ **Cthulhu Mythos**
|
| 261 |
+
- **Cthulhu, Nyarlathotep, Yog-Sothoth**: Elder kin ๐๐๏ธโ๐จ๏ธ๐.
|
| 262 |
+
- **Relation**: Cosmic trio overwhelms ๐ฑ.
|
| 263 |
+
- **Lesson**: Insignificance ๐ humbles ๐.
|
| 264 |
+
|
| 265 |
+
18. โฅ **Egyptian Mythology**
|
| 266 |
+
- **Ra, Osiris, Isis**: Father โ๏ธ, son โฐ๏ธ, mother ๐.
|
| 267 |
+
- **Relation**: Family ensures renewal ๐.
|
| 268 |
+
- **Lesson**: Justice โ๏ธ prevails.
|
| 269 |
+
|
| 270 |
+
19. โ๏ธ **Finnish Mythology**
|
| 271 |
+
- **Vรคinรคmรถinen, Louhi, Ukko**: Son ๐ถ, mother โ๏ธ, father โก.
|
| 272 |
+
- **Relation**: Kinship tests wisdom ๐ง .
|
| 273 |
+
- **Lesson**: Perseverance ๐๏ธ wins.
|
| 274 |
+
|
| 275 |
+
20. ๐๏ธ **Greek Mythology**
|
| 276 |
+
- **Zeus, Hera, Athena**: Father โก, mother ๐, daughter ๐ฆ.
|
| 277 |
+
- **Relation**: Family rules with tension โ๏ธ.
|
| 278 |
+
- **Lesson**: Hubris ๐ค meets wisdom ๐ง .
|
| 279 |
+
|
| 280 |
+
21. ๐๏ธ **Hindu Trimurti**
|
| 281 |
+
- **Brahma, Vishnu, Shiva**: Creator ๐, preserver ๐ก๏ธ, destroyer ๐ฅ.
|
| 282 |
+
- **Relation**: Divine trio cycles existence ๐.
|
| 283 |
+
- **Lesson**: Balance โ๏ธ sustains life ๐.
|
| 284 |
+
|
| 285 |
+
22. ๐บ **Hindu Avatars & Devis**
|
| 286 |
+
- **Krishna, Rama, Durga**: Sons ๐ฆ๐น and fierce mother ๐ก๏ธ.
|
| 287 |
+
- **Relation**: Avatars and goddess protect dharma โ๏ธ.
|
| 288 |
+
- **Lesson**: Duty โ๏ธ defeats evil ๐น.
|
| 289 |
+
|
| 290 |
+
23. ๐ธ **Japanese Mythology**
|
| 291 |
+
- **Amaterasu, Susanoo, Tsukuyomi**: Sister โ๏ธ, brothers ๐๐.
|
| 292 |
+
- **Relation**: Siblings balance cosmos ๐.
|
| 293 |
+
- **Lesson**: Harmony ๐ถ vs. chaos ๐ช๏ธ.
|
| 294 |
+
|
| 295 |
+
24. ๐ก๏ธ **Melnibonean Legends**
|
| 296 |
+
- **Arioch, Xiombarg, Elric**: Lords ๐ and mortal son โ๏ธ.
|
| 297 |
+
- **Relation**: Pact binds chaos ๐ช๏ธ.
|
| 298 |
+
- **Lesson**: Power ๐ช corrupts ๐.
|
| 299 |
+
|
| 300 |
+
25. โช๏ธ **Muslim Divine & Messengers**
|
| 301 |
+
- **Allah, Muhammad, Gabriel**: God ๐, prophet ๐, angel ๐.
|
| 302 |
+
- **Relation**: Messenger reveals divine will ๐.
|
| 303 |
+
- **Lesson**: Submission ๐ brings peace โฎ๏ธ.
|
| 304 |
+
|
| 305 |
+
26. ๐ป **Muslim Spirits & Kin**
|
| 306 |
+
- **Jinn, Iblis, Khidr**: Spirits ๐ฅ๐ and guide ๐ฟ defy or aid.
|
| 307 |
+
- **Relation**: Supernatural kin test faith ๐.
|
| 308 |
+
- **Lesson**: Obedience ๐ง vs. rebellion ๐ก.
|
| 309 |
+
|
| 310 |
+
27. ๐ฐ **Nehwon Legends**
|
| 311 |
+
- **Death, Ningauble, Sheelba**: Fateful trio ๐๐๏ธโ๐จ๏ธ๐ฟ.
|
| 312 |
+
- **Relation**: Guides shape destiny ๐ฒ.
|
| 313 |
+
- **Lesson**: Cunning ๐ง defies fate โฐ๏ธ.
|
| 314 |
+
|
| 315 |
+
28. ๐ง **Nonhuman Traditions**
|
| 316 |
+
- **Corellon, Moradin, Gruumsh**: Elf ๐ง, dwarf โ๏ธ, orc ๐ก๏ธ fathers.
|
| 317 |
+
- **Relation**: Rivals define purpose โ๏ธ.
|
| 318 |
+
- **Lesson**: Community ๐ค endures.
|
| 319 |
+
|
| 320 |
+
29. แฑ **Norse Mythology**
|
| 321 |
+
- **Odin, Frigg, Loki**: Father ๐๏ธ, mother ๐, trickster son ๐ฆ.
|
| 322 |
+
- **Relation**: Family faces doom โก.
|
| 323 |
+
- **Lesson**: Sacrifice ๐ฉธ costs.
|
| 324 |
+
|
| 325 |
+
30. ๐ฟ **Sumerian Mythology**
|
| 326 |
+
- **Enki, Inanna, Anu**: Son ๐, daughter โค๏ธ, father ๐.
|
| 327 |
+
- **Relation**: Kin wield knowledge ๐ง .
|
| 328 |
+
- **Lesson**: Ambition ๐ shapes.
|
| 329 |
+
|
| 330 |
+
31. ๐ **Appendices**
|
| 331 |
+
- **Planes**: Realms of gods, spirits, saints, e.g., Paradise ๐ and Svarga โจ.
|
| 332 |
+
- **Symbols**: Rituals ๐๏ธ and artifacts ๐ฟ of faith.
|
| 333 |
+
- **Charts**: Domains and duties for devotees ๐.
|
| 334 |
+
|
| 335 |
+
32. ๐ **Planes of Existence**
|
| 336 |
+
- **Heaven/Paradise**: Christian/Muslim abode ๐.
|
| 337 |
+
- **Svarga**: Hindu divine realm โจ.
|
| 338 |
+
- **Underworld**: Spirits linger, e.g., Sheol โฐ๏ธ and Naraka ๐ฅ.
|
| 339 |
+
|
| 340 |
+
33. ๐ **Temple Trappings**
|
| 341 |
+
- **Cross/Crescent**: Christian/Muslim faith โ๏ธโช๏ธ.
|
| 342 |
+
- **Mandalas**: Hindu devotion ๐.
|
| 343 |
+
- **Relics**: Saintsโ and prophetsโ legacy ๐๏ธ.
|
| 344 |
+
|
| 345 |
+
34. ๐ **Clerical Chart**
|
| 346 |
+
- **Gods**: Domains, e.g., creation ๐ and mercy โค๏ธ.
|
| 347 |
+
- **Spirits**: Influence, like guidance ๐ฟ and mischief ๐.
|
| 348 |
+
- **Saints/Prophets**: Virtues, e.g., justice โ๏ธ and prophecy ๐ฎ.
|
| 349 |
+
"""
|
| 350 |
+
|
| 351 |
md_files = [f for f in glob.glob("*.md") if os.path.basename(f) != "README.md"]
|
| 352 |
md_options = [os.path.splitext(os.path.basename(f))[0] for f in md_files]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 353 |
|
| 354 |
with st.sidebar:
|
| 355 |
st.markdown("### PDF Options")
|
| 356 |
selected_md = st.selectbox("Select Markdown File", options=md_options, index=0 if md_options else -1)
|
| 357 |
available_font_files = {os.path.splitext(os.path.basename(f))[0]: f for f in glob.glob("*.ttf")}
|
| 358 |
+
selected_font_name = st.selectbox("Select Emoji Font", options=list(available_font_files.keys()), index=list(available_font_files.keys()).index("NotoEmoji-Bold") if "NotoEmoji-Bold" in available_font_files else 0)
|
|
|
|
|
|
|
| 359 |
base_font_size = st.slider("Font Size (points)", min_value=6, max_value=16, value=8, step=1)
|
| 360 |
render_with_bold = st.checkbox("Render with Bold Formatting (remove ** markers)", value=True, key="render_with_bold")
|
| 361 |
auto_bold_numbers = st.checkbox("Auto Bold Numbered Lines", value=True, key="auto_bold_numbers")
|
| 362 |
enlarge_numbered = st.checkbox("Enlarge Font Size for Numbered Lines", value=True, key="enlarge_numbered")
|
| 363 |
num_columns = st.selectbox("Number of Columns", options=[1, 2, 3, 4, 5, 6], index=3)
|
| 364 |
+
if 'markdown_content' not in st.session_state or not md_options:
|
| 365 |
+
st.session_state.markdown_content = default_markdown
|
| 366 |
if md_options and selected_md:
|
| 367 |
with open(f"{selected_md}.md", "r", encoding="utf-8") as f:
|
| 368 |
st.session_state.markdown_content = f.read()
|
| 369 |
+
edited_markdown = st.text_area("Modify the markdown content below:", value=st.session_state.markdown_content, height=300, key=f"markdown_{selected_md}_{selected_font_name}_{num_columns}")
|
|
|
|
|
|
|
| 370 |
if st.button("Update PDF"):
|
| 371 |
st.session_state.markdown_content = edited_markdown
|
| 372 |
if md_options and selected_md:
|
| 373 |
with open(f"{selected_md}.md", "w", encoding="utf-8") as f:
|
| 374 |
f.write(edited_markdown)
|
| 375 |
st.experimental_rerun()
|
| 376 |
+
st.download_button(label="Save Markdown", data=st.session_state.markdown_content, file_name=f"{selected_md}.md" if selected_md else "default.md", mime="text/markdown")
|
|
|
|
| 377 |
st.markdown("### Text-to-Speech")
|
| 378 |
VOICES = ["en-US-AriaNeural", "en-US-JennyNeural", "en-GB-SoniaNeural", "en-US-GuyNeural", "en-US-AnaNeural"]
|
| 379 |
selected_voice = st.selectbox("Select Voice for TTS", options=VOICES, index=0)
|
| 380 |
if st.button("Generate Audio"):
|
| 381 |
+
audio_file = asyncio.run(generate_audio(st.session_state.markdown_content, selected_voice))
|
|
|
|
| 382 |
st.audio(audio_file)
|
| 383 |
with open(audio_file, "rb") as f:
|
| 384 |
audio_bytes = f.read()
|
| 385 |
+
st.download_button("Download Audio", data=audio_bytes, file_name=os.path.basename(audio_file), mime="audio/mpeg")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 386 |
|
| 387 |
with st.spinner("Generating PDF..."):
|
| 388 |
+
pdf_bytes = create_pdf(st.session_state.markdown_content, base_font_size, render_with_bold, auto_bold_numbers, enlarge_numbered, num_columns)
|
|
|
|
| 389 |
|
| 390 |
with st.container():
|
| 391 |
pdf_images = pdf_to_image(pdf_bytes)
|