kambris commited on
Commit
dfb3fcb
·
verified ·
1 Parent(s): 16ea61a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +121 -9
app.py CHANGED
@@ -30,6 +30,33 @@ translit_map = {
30
  " ": " ", ".": ".", ",": "،", "?": "؟", "!": "!"
31
  }
32
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
33
  def transliterate(text):
34
  """Convert Latin text to Arabic script using transliteration mapping."""
35
  if not text:
@@ -37,7 +64,6 @@ def transliterate(text):
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,6 +81,30 @@ def transliterate(text):
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():
@@ -70,16 +120,41 @@ def arabic_tts(arabic_text):
70
  print(f"TTS Error: {e}")
71
  return None
72
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
73
  def transliterate_and_speak(latin_text):
74
  """Combined function to transliterate and generate audio."""
75
  arabic_text = transliterate(latin_text)
76
- audio_file = arabic_tts(arabic_text)
77
- return arabic_text, audio_file
 
 
 
 
 
 
 
 
 
78
 
79
  # Create Gradio interface
80
  with gr.Blocks(title="Arabic Transliterator") as demo:
81
- gr.Markdown("## 📝 Latin-to-Arabic Transliterator with Text-to-Speech")
82
- gr.Markdown("Enter Latin characters to convert to Arabic script. Use combinations like 'th', 'kh', 'sh', 'gh' for special sounds.")
83
 
84
  # Full-width transliteration guide
85
  with gr.Accordion("📖 Transliteration Guide", open=False):
@@ -128,12 +203,31 @@ with gr.Blocks(title="Arabic Transliterator") as demo:
128
  rtl=True # Right-to-left text direction for Arabic
129
  )
130
 
 
 
 
 
 
 
 
 
 
131
  with gr.Row():
132
  convert_btn = gr.Button("🔄 Transliterate", variant="primary")
133
- tts_btn = gr.Button("🔊 Speak", 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(
@@ -142,16 +236,34 @@ with gr.Blocks(title="Arabic Transliterator") as demo:
142
  outputs=arabic_output
143
  )
144
 
 
 
 
 
 
 
145
  tts_btn.click(
146
  fn=arabic_tts,
147
  inputs=arabic_output,
148
- outputs=tts_audio
 
 
 
 
 
 
149
  )
150
 
151
  combined_btn.click(
152
  fn=transliterate_and_speak,
153
  inputs=latin_input,
154
- outputs=[arabic_output, tts_audio]
 
 
 
 
 
 
155
  )
156
 
157
 
 
30
  " ": " ", ".": ".", ",": "،", "?": "؟", "!": "!"
31
  }
32
 
33
+ # Direct IPA mapping from English/Latin transliteration to IPA
34
+ latin_to_ipa = {
35
+ # Two-character combinations (must be checked first)
36
+ "th": "θ", "kh": "x", "dh": "ð", "sh": "ʃ", "gh": "ɣ",
37
+ "aa": "aː", "ee": "iː", "oo": "uː", "uu": "uː", "ii": "iː",
38
+
39
+ # Single characters
40
+ "a": "a", "b": "b", "t": "t", "j": "dʒ", "H": "ħ",
41
+ "d": "d", "r": "r", "z": "z", "s": "s",
42
+ "S": "sˤ", "D": "dˤ", "T": "tˤ", "Z": "ðˤ",
43
+ "'": "ʕ", "f": "f", "q": "q", "k": "k",
44
+ "l": "l", "m": "m", "n": "n", "h": "h",
45
+ "w": "w", "y": "j", "x": "ʔ", "o": "ʔu",
46
+
47
+ # Initial vowels (hamza with vowel for word beginnings)
48
+ "i": "ʔi", "u": "ʔu", "e": "ʔa",
49
+
50
+ # Short vowels (diacritics)
51
+ "I": "i", "U": "u", "A": "a", "~": "ː", "^": "",
52
+
53
+ # Tanween (double diacritics)
54
+ "an": "an", "un": "un", "in": "in",
55
+
56
+ # Space and punctuation
57
+ " ": " ", ".": ".", ",": ",", "?": "?", "!": "!"
58
+ }
59
+
60
  def transliterate(text):
61
  """Convert Latin text to Arabic script using transliteration mapping."""
62
  if not text:
 
64
 
65
  output = ""
66
  i = 0
 
67
 
68
  while i < len(text):
69
  # Check for two-character combinations first
 
81
 
82
  return output
83
 
84
+ def latin_to_ipa_conversion(latin_text):
85
+ """Convert Latin text directly to IPA phonetic transcription."""
86
+ if not latin_text:
87
+ return ""
88
+
89
+ output = ""
90
+ i = 0
91
+
92
+ while i < len(latin_text):
93
+ # Check for two-character combinations first
94
+ if i + 1 < len(latin_text) and latin_text[i:i+2] in latin_to_ipa:
95
+ output += latin_to_ipa[latin_text[i:i+2]]
96
+ i += 2
97
+ # Then check for single characters
98
+ elif latin_text[i] in latin_to_ipa:
99
+ output += latin_to_ipa[latin_text[i]]
100
+ i += 1
101
+ else:
102
+ # Keep unknown characters as-is
103
+ output += latin_text[i]
104
+ i += 1
105
+
106
+ return output
107
+
108
  def arabic_tts(arabic_text):
109
  """Generate TTS audio for Arabic text."""
110
  if not arabic_text or not arabic_text.strip():
 
120
  print(f"TTS Error: {e}")
121
  return None
122
 
123
+ def ipa_tts(ipa_text):
124
+ """Generate TTS audio for IPA text using English TTS (approximation)."""
125
+ if not ipa_text or not ipa_text.strip():
126
+ return None
127
+
128
+ try:
129
+ # Use English TTS for IPA - this is an approximation
130
+ # For better IPA pronunciation, you'd need specialized TTS engines
131
+ tts = gTTS(text=ipa_text, lang='en', slow=True)
132
+ tmp_file = tempfile.NamedTemporaryFile(delete=False, suffix=".mp3")
133
+ tts.save(tmp_file.name)
134
+ return tmp_file.name
135
+ except Exception as e:
136
+ print(f"IPA TTS Error: {e}")
137
+ return None
138
+
139
  def transliterate_and_speak(latin_text):
140
  """Combined function to transliterate and generate audio."""
141
  arabic_text = transliterate(latin_text)
142
+ ipa_text = latin_to_ipa_conversion(latin_text)
143
+ arabic_audio = arabic_tts(arabic_text)
144
+ return arabic_text, ipa_text, arabic_audio
145
+
146
+ def full_process_with_ipa_audio(latin_text):
147
+ """Process text and generate both Arabic and IPA audio."""
148
+ arabic_text = transliterate(latin_text)
149
+ ipa_text = latin_to_ipa_conversion(latin_text)
150
+ arabic_audio = arabic_tts(arabic_text)
151
+ ipa_audio = ipa_tts(ipa_text)
152
+ return arabic_text, ipa_text, arabic_audio, ipa_audio
153
 
154
  # Create Gradio interface
155
  with gr.Blocks(title="Arabic Transliterator") as demo:
156
+ gr.Markdown("## 📝 Latin-to-Arabic Transliterator with Text-to-Speech & IPA")
157
+ gr.Markdown("Enter Latin characters to convert to Arabic script. Generate IPA phonetic transcription and audio pronunciation.")
158
 
159
  # Full-width transliteration guide
160
  with gr.Accordion("📖 Transliteration Guide", open=False):
 
203
  rtl=True # Right-to-left text direction for Arabic
204
  )
205
 
206
+ # IPA output row
207
+ with gr.Row():
208
+ ipa_output = gr.Textbox(
209
+ label="IPA Phonetic Transcription",
210
+ lines=2,
211
+ placeholder="International Phonetic Alphabet representation will appear here"
212
+ )
213
+
214
+ # Button controls
215
  with gr.Row():
216
  convert_btn = gr.Button("🔄 Transliterate", variant="primary")
217
+ tts_btn = gr.Button("🔊 Speak Arabic", variant="secondary")
218
+ ipa_btn = gr.Button("🔤 Generate IPA", variant="secondary")
219
+ ipa_tts_btn = gr.Button("🔊 Pronounce IPA", variant="secondary")
220
+
221
+ with gr.Row():
222
  combined_btn = gr.Button("🔄🔊 Transliterate & Speak", variant="secondary")
223
+ full_process_btn = gr.Button("🔄🔤🔊 Full Process + IPA Audio", variant="secondary")
224
 
225
+ # Audio outputs
226
+ with gr.Row():
227
+ with gr.Column():
228
+ arabic_audio = gr.Audio(label="Arabic Audio", type="filepath")
229
+ with gr.Column():
230
+ ipa_audio = gr.Audio(label="IPA Pronunciation Audio", type="filepath")
231
 
232
  # Event handlers
233
  convert_btn.click(
 
236
  outputs=arabic_output
237
  )
238
 
239
+ ipa_btn.click(
240
+ fn=latin_to_ipa_conversion,
241
+ inputs=latin_input,
242
+ outputs=ipa_output
243
+ )
244
+
245
  tts_btn.click(
246
  fn=arabic_tts,
247
  inputs=arabic_output,
248
+ outputs=arabic_audio
249
+ )
250
+
251
+ ipa_tts_btn.click(
252
+ fn=ipa_tts,
253
+ inputs=ipa_output,
254
+ outputs=ipa_audio
255
  )
256
 
257
  combined_btn.click(
258
  fn=transliterate_and_speak,
259
  inputs=latin_input,
260
+ outputs=[arabic_output, ipa_output, arabic_audio]
261
+ )
262
+
263
+ full_process_btn.click(
264
+ fn=full_process_with_ipa_audio,
265
+ inputs=latin_input,
266
+ outputs=[arabic_output, ipa_output, arabic_audio, ipa_audio]
267
  )
268
 
269