awacke1 commited on
Commit
b51b5a6
·
verified ·
1 Parent(s): 45c2a49

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +47 -192
app.py CHANGED
@@ -15,11 +15,18 @@ from reportlab.lib.styles import getSampleStyleSheet, ParagraphStyle
15
  from reportlab.lib import colors
16
  from reportlab.pdfbase import pdfmetrics
17
  from reportlab.pdfbase.ttfonts import TTFont
 
 
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
@@ -78,7 +85,6 @@ def create_pdf(markdown_text, base_font_size, render_with_bold, auto_bold_number
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",
@@ -171,218 +177,61 @@ def pdf_to_image(pdf_bytes):
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)
@@ -395,5 +244,11 @@ with st.container():
395
  else:
396
  st.info("Download the PDF to view it locally.")
397
 
 
398
  with st.sidebar:
399
- st.download_button(label="Download PDF", data=pdf_bytes, file_name="output.pdf", mime="application/pdf")
 
 
 
 
 
 
15
  from reportlab.lib import colors
16
  from reportlab.pdfbase import pdfmetrics
17
  from reportlab.pdfbase.ttfonts import TTFont
18
+ from datetime import datetime
19
+ import pytz
20
 
21
  st.set_page_config(layout="wide", initial_sidebar_state="collapsed")
22
 
23
+ def get_timestamp_prefix():
24
+ central = pytz.timezone("US/Central")
25
+ now = datetime.now(central)
26
+ # Format: three-letter day, MMDD, HHmm + AM/PM (all uppercase)
27
+ return now.strftime("%a %m%d %I%M%p").upper()
28
+
29
+ async def generate_audio(text, voice, filename):
30
  communicate = edge_tts.Communicate(text, voice)
31
  await communicate.save(filename)
32
  return filename
 
85
  doc = SimpleDocTemplate(buffer, pagesize=(page_width, page_height), leftMargin=36, rightMargin=36, topMargin=36, bottomMargin=36)
86
  styles = getSampleStyleSheet()
87
  spacer_height = 10
 
88
  pdf_content, total_lines = markdown_to_pdf_content(markdown_text, render_with_bold, auto_bold_numbers)
89
  item_style = ParagraphStyle(
90
  'ItemStyle', parent=styles['Normal'], fontName="DejaVuSans",
 
177
  st.error(f"Failed to render PDF preview: {e}")
178
  return None
179
 
180
+ # -- Markdown File Selection --
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
181
  md_files = [f for f in glob.glob("*.md") if os.path.basename(f) != "README.md"]
182
  md_options = [os.path.splitext(os.path.basename(f))[0] for f in md_files]
183
 
184
  with st.sidebar:
185
  st.markdown("### PDF Options")
186
+ if md_options:
187
+ selected_md = st.selectbox("Select Markdown File", options=md_options, index=0)
188
+ with open(f"{selected_md}.md", "r", encoding="utf-8") as f:
189
+ st.session_state.markdown_content = f.read()
190
+ else:
191
+ st.warning("No markdown file found. Please add one to your folder.")
192
+ selected_md = None
193
+ st.session_state.markdown_content = ""
194
+
195
  available_font_files = {os.path.splitext(os.path.basename(f))[0]: f for f in glob.glob("*.ttf")}
196
+ selected_font_name = st.selectbox("Select Emoji Font", options=list(available_font_files.keys()),
197
+ index=list(available_font_files.keys()).index("NotoEmoji-Bold") if "NotoEmoji-Bold" in available_font_files else 0)
198
  base_font_size = st.slider("Font Size (points)", min_value=6, max_value=16, value=8, step=1)
199
  render_with_bold = st.checkbox("Render with Bold Formatting (remove ** markers)", value=True, key="render_with_bold")
200
  auto_bold_numbers = st.checkbox("Auto Bold Numbered Lines", value=True, key="auto_bold_numbers")
201
  enlarge_numbered = st.checkbox("Enlarge Font Size for Numbered Lines", value=True, key="enlarge_numbered")
202
  num_columns = st.selectbox("Number of Columns", options=[1, 2, 3, 4, 5, 6], index=3)
203
+
204
+ # Use the file's content for editing
 
 
 
205
  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}")
206
  if st.button("Update PDF"):
207
  st.session_state.markdown_content = edited_markdown
208
+ if selected_md:
209
  with open(f"{selected_md}.md", "w", encoding="utf-8") as f:
210
  f.write(edited_markdown)
211
  st.experimental_rerun()
212
+
213
+ # Create a timestamp prefix for file naming
214
+ prefix = get_timestamp_prefix()
215
+
216
+ # Download button for Markdown with new naming convention
217
+ st.download_button(
218
+ label="Save Markdown",
219
+ data=st.session_state.markdown_content,
220
+ file_name=f"{prefix} {selected_md}.md" if selected_md else f"{prefix} default.md",
221
+ mime="text/markdown"
222
+ )
223
+
224
  st.markdown("### Text-to-Speech")
225
  VOICES = ["en-US-AriaNeural", "en-US-JennyNeural", "en-GB-SoniaNeural", "en-US-GuyNeural", "en-US-AnaNeural"]
226
  selected_voice = st.selectbox("Select Voice for TTS", options=VOICES, index=0)
227
  if st.button("Generate Audio"):
228
+ # Create a filename for the audio file using the timestamp, markdown name, and selected voice
229
+ audio_filename = f"{prefix} {selected_md} {selected_voice}.mp3" if selected_md else f"{prefix} default {selected_voice}.mp3"
230
+ audio_file = asyncio.run(generate_audio(st.session_state.markdown_content, selected_voice, audio_filename))
231
  st.audio(audio_file)
232
  with open(audio_file, "rb") as f:
233
  audio_bytes = f.read()
234
+ st.download_button("Download Audio", data=audio_bytes, file_name=audio_filename, mime="audio/mpeg")
235
 
236
  with st.spinner("Generating PDF..."):
237
  pdf_bytes = create_pdf(st.session_state.markdown_content, base_font_size, render_with_bold, auto_bold_numbers, enlarge_numbered, num_columns)
 
244
  else:
245
  st.info("Download the PDF to view it locally.")
246
 
247
+ # Download button for PDF with new naming convention
248
  with st.sidebar:
249
+ st.download_button(
250
+ label="Download PDF",
251
+ data=pdf_bytes,
252
+ file_name=f"{prefix} {selected_md}.pdf" if selected_md else f"{prefix} output.pdf",
253
+ mime="application/pdf"
254
+ )