Update app.py
Browse files
app.py
CHANGED
|
@@ -17,9 +17,15 @@ translit_map = {
|
|
| 17 |
"l": "Ω", "m": "Ω
", "n": "Ω", "h": "Ω",
|
| 18 |
"w": "Ω", "y": "Ω", "x": "Ψ‘", "o": "Ψ£Ω",
|
| 19 |
|
| 20 |
-
#
|
| 21 |
"i": "Ψ₯Ω", "u": "Ψ£Ω", "e": "Ψ£",
|
| 22 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
# Space and punctuation
|
| 24 |
" ": " ", ".": ".", ",": "Ψ", "?": "Ψ", "!": "!"
|
| 25 |
}
|
|
@@ -31,7 +37,7 @@ def transliterate(text):
|
|
| 31 |
|
| 32 |
output = ""
|
| 33 |
i = 0
|
| 34 |
-
|
| 35 |
|
| 36 |
while i < len(text):
|
| 37 |
# Check for two-character combinations first
|
|
@@ -49,21 +55,13 @@ def transliterate(text):
|
|
| 49 |
|
| 50 |
return output
|
| 51 |
|
| 52 |
-
def arabic_tts(arabic_text
|
| 53 |
-
"""Generate TTS audio for Arabic text
|
| 54 |
if not arabic_text or not arabic_text.strip():
|
| 55 |
return None
|
| 56 |
|
| 57 |
try:
|
| 58 |
-
|
| 59 |
-
if tts_option == "neutral":
|
| 60 |
-
# Remove common Arabic diacritics that add inflection
|
| 61 |
-
diacritics = "ΩΩΩΩΩΩΩΩ"
|
| 62 |
-
cleaned_text = ''.join(char for char in arabic_text if char not in diacritics)
|
| 63 |
-
tts = gTTS(text=cleaned_text, lang='ar', slow=True)
|
| 64 |
-
else:
|
| 65 |
-
tts = gTTS(text=arabic_text, lang='ar', slow=False)
|
| 66 |
-
|
| 67 |
# Create temporary file
|
| 68 |
tmp_file = tempfile.NamedTemporaryFile(delete=False, suffix=".mp3")
|
| 69 |
tts.save(tmp_file.name)
|
|
@@ -104,14 +102,23 @@ with gr.Blocks(title="Arabic Transliterator") as demo:
|
|
| 104 |
`S` β Ψ΅, `D` β ΨΆ, `T` β Ψ·, `Z` β ΨΈ, `'` β ΨΉ, `f` β Ω, `q` β Ω, `k` β Ω
|
| 105 |
`l` β Ω, `m` β Ω
, `n` β Ω, `h` β Ω, `w` β Ω, `y` β Ω, `x` β Ψ‘
|
| 106 |
|
| 107 |
-
**Vowels:**
|
| 108 |
`o` β Ψ£Ω, `i` β Ψ₯Ω, `u` β Ψ£Ω, `e` β Ψ£
|
| 109 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 110 |
**Tips:**
|
| 111 |
- Use capital letters for emphatic consonants (S, D, T, Z, H)
|
|
|
|
|
|
|
| 112 |
- Combinations like 'th', 'kh' are processed before single letters
|
| 113 |
- Use apostrophe (') for the letter ΨΉ (ayn)
|
| 114 |
- Use 'x' for hamza Ψ‘ when standalone
|
|
|
|
| 115 |
""")
|
| 116 |
|
| 117 |
with gr.Column():
|
|
@@ -124,15 +131,10 @@ with gr.Blocks(title="Arabic Transliterator") as demo:
|
|
| 124 |
with gr.Row():
|
| 125 |
convert_btn = gr.Button("π Transliterate", variant="primary")
|
| 126 |
tts_btn = gr.Button("π Speak Arabic", variant="secondary")
|
| 127 |
-
neutral_tts_btn = gr.Button("π Neutral Pronunciation", variant="secondary")
|
| 128 |
combined_btn = gr.Button("ππ Transliterate & Speak", variant="secondary")
|
| 129 |
|
| 130 |
tts_audio = gr.Audio(label="Audio Output", type="filepath")
|
| 131 |
|
| 132 |
-
# Options
|
| 133 |
-
with gr.Row():
|
| 134 |
-
gr.Markdown("**TTS Options:** Standard includes natural inflections, Neutral removes diacritics for clearer pronunciation")
|
| 135 |
-
|
| 136 |
# Event handlers
|
| 137 |
convert_btn.click(
|
| 138 |
fn=transliterate,
|
|
@@ -141,13 +143,7 @@ with gr.Blocks(title="Arabic Transliterator") as demo:
|
|
| 141 |
)
|
| 142 |
|
| 143 |
tts_btn.click(
|
| 144 |
-
fn=
|
| 145 |
-
inputs=arabic_output,
|
| 146 |
-
outputs=tts_audio
|
| 147 |
-
)
|
| 148 |
-
|
| 149 |
-
neutral_tts_btn.click(
|
| 150 |
-
fn=lambda text: arabic_tts(text, "neutral"),
|
| 151 |
inputs=arabic_output,
|
| 152 |
outputs=tts_audio
|
| 153 |
)
|
|
@@ -157,6 +153,8 @@ with gr.Blocks(title="Arabic Transliterator") as demo:
|
|
| 157 |
inputs=latin_input,
|
| 158 |
outputs=[arabic_output, tts_audio]
|
| 159 |
)
|
|
|
|
|
|
|
| 160 |
|
| 161 |
if __name__ == "__main__":
|
| 162 |
demo.launch()
|
|
|
|
| 17 |
"l": "Ω", "m": "Ω
", "n": "Ω", "h": "Ω",
|
| 18 |
"w": "Ω", "y": "Ω", "x": "Ψ‘", "o": "Ψ£Ω",
|
| 19 |
|
| 20 |
+
# Initial vowels (hamza with vowel for word beginnings)
|
| 21 |
"i": "Ψ₯Ω", "u": "Ψ£Ω", "e": "Ψ£",
|
| 22 |
|
| 23 |
+
# Short vowels (diacritics)
|
| 24 |
+
"I": "Ω", "U": "Ω", "A": "Ω", "~": "Ω", "^": "Ω",
|
| 25 |
+
|
| 26 |
+
# Tanween (double diacritics)
|
| 27 |
+
"an": "Ω", "un": "Ω", "in": "Ω",
|
| 28 |
+
|
| 29 |
# Space and punctuation
|
| 30 |
" ": " ", ".": ".", ",": "Ψ", "?": "Ψ", "!": "!"
|
| 31 |
}
|
|
|
|
| 37 |
|
| 38 |
output = ""
|
| 39 |
i = 0
|
| 40 |
+
# Don't convert to lowercase to preserve capital letter mappings
|
| 41 |
|
| 42 |
while i < len(text):
|
| 43 |
# Check for two-character combinations first
|
|
|
|
| 55 |
|
| 56 |
return output
|
| 57 |
|
| 58 |
+
def arabic_tts(arabic_text):
|
| 59 |
+
"""Generate TTS audio for Arabic text."""
|
| 60 |
if not arabic_text or not arabic_text.strip():
|
| 61 |
return None
|
| 62 |
|
| 63 |
try:
|
| 64 |
+
tts = gTTS(text=arabic_text, lang='ar', slow=False)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 65 |
# Create temporary file
|
| 66 |
tmp_file = tempfile.NamedTemporaryFile(delete=False, suffix=".mp3")
|
| 67 |
tts.save(tmp_file.name)
|
|
|
|
| 102 |
`S` β Ψ΅, `D` β ΨΆ, `T` β Ψ·, `Z` β ΨΈ, `'` β ΨΉ, `f` β Ω, `q` β Ω, `k` β Ω
|
| 103 |
`l` β Ω, `m` β Ω
, `n` β Ω, `h` β Ω, `w` β Ω, `y` β Ω, `x` β Ψ‘
|
| 104 |
|
| 105 |
+
**Initial Vowels (Word Beginnings):**
|
| 106 |
`o` β Ψ£Ω, `i` β Ψ₯Ω, `u` β Ψ£Ω, `e` β Ψ£
|
| 107 |
|
| 108 |
+
**Short Vowels (Diacritics):**
|
| 109 |
+
`I` β Ω (Kasra), `U` β Ω (Damma), `A` β Ω (Fatha), `~` β Ω (Shadda), `^` β Ω (Sukun)
|
| 110 |
+
|
| 111 |
+
**Tanween (Double Diacritics):**
|
| 112 |
+
`an` β Ω (Fathatan), `un` β Ω (Dammatan), `in` β Ω (Kasratan)
|
| 113 |
+
|
| 114 |
**Tips:**
|
| 115 |
- Use capital letters for emphatic consonants (S, D, T, Z, H)
|
| 116 |
+
- Use capital vowels (I, U, A) for diacritics/short vowels
|
| 117 |
+
- Use 'an', 'un', 'in' for tanween (double diacritics)
|
| 118 |
- Combinations like 'th', 'kh' are processed before single letters
|
| 119 |
- Use apostrophe (') for the letter ΨΉ (ayn)
|
| 120 |
- Use 'x' for hamza Ψ‘ when standalone
|
| 121 |
+
- Use ~ for shadda (gemination) and ^ for sukun (no vowel)
|
| 122 |
""")
|
| 123 |
|
| 124 |
with gr.Column():
|
|
|
|
| 131 |
with gr.Row():
|
| 132 |
convert_btn = gr.Button("π Transliterate", variant="primary")
|
| 133 |
tts_btn = gr.Button("π Speak Arabic", variant="secondary")
|
|
|
|
| 134 |
combined_btn = gr.Button("ππ Transliterate & Speak", variant="secondary")
|
| 135 |
|
| 136 |
tts_audio = gr.Audio(label="Audio Output", type="filepath")
|
| 137 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 138 |
# Event handlers
|
| 139 |
convert_btn.click(
|
| 140 |
fn=transliterate,
|
|
|
|
| 143 |
)
|
| 144 |
|
| 145 |
tts_btn.click(
|
| 146 |
+
fn=arabic_tts,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 147 |
inputs=arabic_output,
|
| 148 |
outputs=tts_audio
|
| 149 |
)
|
|
|
|
| 153 |
inputs=latin_input,
|
| 154 |
outputs=[arabic_output, tts_audio]
|
| 155 |
)
|
| 156 |
+
|
| 157 |
+
|
| 158 |
|
| 159 |
if __name__ == "__main__":
|
| 160 |
demo.launch()
|