kambris commited on
Commit
d64cd8c
·
verified ·
1 Parent(s): e2ac22d

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +131 -0
app.py ADDED
@@ -0,0 +1,131 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from gtts import gTTS
3
+ import tempfile
4
+ import os
5
+
6
+ # Enhanced transliteration map with proper Arabic characters
7
+ translit_map = {
8
+ # Two-character combinations (must be checked first)
9
+ "th": "ث", "kh": "خ", "dh": "ذ", "sh": "ش", "gh": "غ",
10
+ "aa": "آ", "ee": "ي", "oo": "و", "uu": "و", "ii": "ي",
11
+
12
+ # Single characters
13
+ "a": "ا", "b": "ب", "t": "ت", "j": "ج", "H": "ح",
14
+ "d": "د", "r": "ر", "z": "ز", "s": "س",
15
+ "S": "ص", "D": "ض", "T": "ط", "Z": "ظ",
16
+ "'": "ع", "gh": "غ", "f": "ف", "q": "ق", "k": "ك",
17
+ "l": "ل", "m": "م", "n": "ن", "h": "ه",
18
+ "w": "و", "y": "ي", "x": "ء",
19
+
20
+ # Vowels (short)
21
+ "i": "ِ", "u": "ُ", "e": "َ",
22
+
23
+ # Space and punctuation
24
+ " ": " ", ".": ".", ",": "،", "?": "؟", "!": "!"
25
+ }
26
+
27
+ def transliterate(text):
28
+ """Convert Latin text to Arabic script using transliteration mapping."""
29
+ if not text:
30
+ return ""
31
+
32
+ output = ""
33
+ i = 0
34
+ text = text.lower() # Convert to lowercase for consistent mapping
35
+
36
+ while i < len(text):
37
+ # Check for two-character combinations first
38
+ if i + 1 < len(text) and text[i:i+2] in translit_map:
39
+ output += translit_map[text[i:i+2]]
40
+ i += 2
41
+ # Then check for single characters
42
+ elif text[i] in translit_map:
43
+ output += translit_map[text[i]]
44
+ i += 1
45
+ else:
46
+ # Keep unknown characters as-is
47
+ output += text[i]
48
+ i += 1
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
+ tts = gTTS(text=arabic_text, lang='ar', slow=False)
59
+ # Create temporary file
60
+ tmp_file = tempfile.NamedTemporaryFile(delete=False, suffix=".mp3")
61
+ tts.save(tmp_file.name)
62
+ return tmp_file.name
63
+ except Exception as e:
64
+ print(f"TTS Error: {e}")
65
+ return None
66
+
67
+ def transliterate_and_speak(latin_text):
68
+ """Combined function to transliterate and generate audio."""
69
+ arabic_text = transliterate(latin_text)
70
+ audio_file = arabic_tts(arabic_text)
71
+ return arabic_text, audio_file
72
+
73
+ # Create Gradio interface
74
+ with gr.Blocks(title="Arabic Transliterator") as demo:
75
+ gr.Markdown("## 📝 Latin-to-Arabic Transliterator with Text-to-Speech")
76
+ gr.Markdown("Enter Latin characters to convert to Arabic script. Use combinations like 'th', 'kh', 'sh', 'gh' for special sounds.")
77
+
78
+ with gr.Row():
79
+ with gr.Column():
80
+ latin_input = gr.Textbox(
81
+ label="Enter Latin-encoded Arabic",
82
+ lines=3,
83
+ placeholder="Example: ahlan wa sahlan (أهلا وسهلا)"
84
+ )
85
+ with gr.Column():
86
+ arabic_output = gr.Textbox(
87
+ label="Arabic Script",
88
+ lines=3,
89
+ rtl=True # Right-to-left text direction for Arabic
90
+ )
91
+
92
+ with gr.Row():
93
+ convert_btn = gr.Button("🔄 Transliterate", variant="primary")
94
+ tts_btn = gr.Button("🔊 Speak Arabic", variant="secondary")
95
+ combined_btn = gr.Button("🔄🔊 Transliterate & Speak", variant="secondary")
96
+
97
+ tts_audio = gr.Audio(label="Audio Output", type="filepath")
98
+
99
+ # Event handlers
100
+ convert_btn.click(
101
+ fn=transliterate,
102
+ inputs=latin_input,
103
+ outputs=arabic_output
104
+ )
105
+
106
+ tts_btn.click(
107
+ fn=arabic_tts,
108
+ inputs=arabic_output,
109
+ outputs=tts_audio
110
+ )
111
+
112
+ combined_btn.click(
113
+ fn=transliterate_and_speak,
114
+ inputs=latin_input,
115
+ outputs=[arabic_output, tts_audio]
116
+ )
117
+
118
+ # Examples
119
+ gr.Examples(
120
+ examples=[
121
+ ["ahlan wa sahlan"],
122
+ ["as-salamu alaykum"],
123
+ ["shukran jazeelan"],
124
+ ["kayf halak"],
125
+ ["ana min amreeka"]
126
+ ],
127
+ inputs=latin_input
128
+ )
129
+
130
+ if __name__ == "__main__":
131
+ demo.launch()