Jesus del Carmen Valdiviezo commited on
Commit
cc568ab
·
1 Parent(s): 0160120

clean text for audio

Browse files
Files changed (2) hide show
  1. app.py +89 -18
  2. requirements.txt +1 -0
app.py CHANGED
@@ -21,7 +21,9 @@ from audio_recorder_streamlit import audio_recorder
21
  import asyncio
22
  import edge_tts
23
  from io import BytesIO
24
-
 
 
25
 
26
  # Define your knowledge content at the top level of your script
27
  knowledge_content = """
@@ -521,33 +523,102 @@ def convert_audio_to_text(path):
521
  # os.remove(tmp.name)
522
 
523
 
524
- # pip install edge-tts
525
- import asyncio
526
- import edge_tts
527
- from io import BytesIO
528
- import streamlit as st
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
529
 
530
  def convert_text_to_audio(text: str) -> bytes:
531
  """
532
- Usa edge-tts para generar MP3 en memoria.
533
  """
534
- #voice = "es-MX-JorgeNeural"
 
 
 
 
 
 
535
  voice = "es-MX-DaliaNeural"
536
- communicate = edge_tts.Communicate(text, voice)
537
-
538
- # Guardar en disco y luego leer, porque save() no admite BytesIO directo
539
  tmp_path = "temp_edge.mp3"
540
  loop = asyncio.new_event_loop()
541
  loop.run_until_complete(communicate.save(tmp_path))
542
-
543
  with open(tmp_path, "rb") as f:
544
  data = f.read()
545
- # cleanup
546
- try:
547
- import os; os.remove(tmp_path)
548
- except:
549
- pass
550
-
551
  return data
552
 
553
 
 
21
  import asyncio
22
  import edge_tts
23
  from io import BytesIO
24
+ #
25
+ import re
26
+ import emoji
27
 
28
  # Define your knowledge content at the top level of your script
29
  knowledge_content = """
 
523
  # os.remove(tmp.name)
524
 
525
 
526
+
527
+
528
+
529
+ def remove_emojis(text: str) -> str:
530
+ # usa emoji.replace_emoji para borrar cualquier icono
531
+ return emoji.replace_emoji(text, replace="")
532
+
533
+
534
+ #def remove_emojis(text: str) -> str:
535
+ # """
536
+ # Elimina emojis basándose en rangos Unicode.
537
+ # """
538
+ # emoji_pattern = re.compile(
539
+ # "["
540
+ # "\U0001F600-\U0001F64F" # emoticons
541
+ # "\U0001F300-\U0001F5FF" # symbols & pictographs
542
+ # "\U0001F680-\U0001F6FF" # transport & map symbols
543
+ # "\U0001F1E0-\U0001F1FF" # flags
544
+ # "]",
545
+ # flags=re.UNICODE
546
+ # )
547
+ # return emoji_pattern.sub("", text)
548
+
549
+ def remove_urls(text: str) -> str:
550
+ """
551
+ Elimina cualquier substring que empiece con http:// o https://
552
+ """
553
+ return re.sub(r"https?://\S+", "", text)
554
+
555
+ def remove_bullets(text: str) -> str:
556
+ # elimina los marcadores de lista (•, –, *, etc.) al inicio de cada línea
557
+ text = re.sub(r'(?m)^[\s]*[•\-\*]\s*', '', text)
558
+ # quita cualquier • suelto en el resto del texto
559
+ return text.replace('•', '')
560
+
561
+
562
+ def remove_markdown(text: str) -> str:
563
+ """
564
+ Quita **bold**, *italic*, `code`, y enlaces [texto](url).
565
+ """
566
+ # Bold: **algo** → algo
567
+ text = re.sub(r'\*\*(.*?)\*\*', r'\1', text)
568
+ # Italic or single *: *algo* → algo
569
+ text = re.sub(r'\*(.*?)\*', r'\1', text)
570
+ # Inline code: `algo` → algo
571
+ text = re.sub(r'`([^`]*)`', r'\1', text)
572
+ # Links: [texto](url) → texto
573
+ text = re.sub(r'\[([^\]]+)\]\([^\)]+\)', r'\1', text)
574
+ return text
575
+
576
+
577
+ #def convert_text_to_audio(text: str) -> bytes:
578
+ # """
579
+ # Usa edge-tts para generar MP3 en memoria.
580
+ # """
581
+ # #voice = "es-MX-JorgeNeural"
582
+ # voice = "es-MX-DaliaNeural"
583
+ # communicate = edge_tts.Communicate(text, voice)
584
+ #
585
+ # # Guardar en disco y luego leer, porque save() no admite BytesIO directo
586
+ # tmp_path = "temp_edge.mp3"
587
+ # loop = asyncio.new_event_loop()
588
+ # loop.run_until_complete(communicate.save(tmp_path))
589
+ #
590
+ # with open(tmp_path, "rb") as f:
591
+ # data = f.read()
592
+ # # cleanup
593
+ # try:
594
+ # import os; os.remove(tmp_path)
595
+ # except:
596
+ # pass
597
+ #
598
+ # return data
599
+
600
 
601
  def convert_text_to_audio(text: str) -> bytes:
602
  """
603
+ Usa edge-tts para generar MP3 en memoria, tras sanear emojis y URLs.
604
  """
605
+ # 1) Limpieza en serie
606
+ clean = remove_urls(text)
607
+ clean = remove_emojis(clean)
608
+ clean = remove_markdown(clean)
609
+ clean = remove_bullets(clean)
610
+
611
+ # 2) Selección de voz y síntesis
612
  voice = "es-MX-DaliaNeural"
613
+ communicate = edge_tts.Communicate(clean, voice)
614
+
615
+ # 3) Guardar y leer en memoria
616
  tmp_path = "temp_edge.mp3"
617
  loop = asyncio.new_event_loop()
618
  loop.run_until_complete(communicate.save(tmp_path))
 
619
  with open(tmp_path, "rb") as f:
620
  data = f.read()
621
+ os.remove(tmp_path)
 
 
 
 
 
622
  return data
623
 
624
 
requirements.txt CHANGED
@@ -18,3 +18,4 @@ SpeechRecognition
18
  gtts
19
  audio_recorder_streamlit
20
  edge-tts
 
 
18
  gtts
19
  audio_recorder_streamlit
20
  edge-tts
21
+ emoji