afaaaak commited on
Commit
9ad4148
·
verified ·
1 Parent(s): 28db5c0

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +51 -79
app.py CHANGED
@@ -1,8 +1,6 @@
1
  import streamlit as st
2
- from transformers import MarianMTModel, MarianTokenizer
3
- import torch
4
  from deep_translator import GoogleTranslator
5
- import re
6
 
7
  # ================================
8
  # PAGE CONFIGURATION
@@ -166,12 +164,12 @@ def load_css():
166
  }
167
 
168
  /* Clear Button */
169
- .clear-button button {
170
  background: linear-gradient(145deg, #f093fb, #f5576c);
171
  box-shadow: 0 10px 25px rgba(245, 87, 108, 0.4);
172
  }
173
 
174
- .clear-button button:hover {
175
  box-shadow: 0 15px 35px rgba(245, 87, 108, 0.6);
176
  }
177
 
@@ -202,18 +200,13 @@ def load_css():
202
  border-top-color: #ffffff !important;
203
  }
204
 
205
- /* Columns */
206
- .element-container {
207
- margin-bottom: 20px;
208
- }
209
-
210
- /* Success Message */
211
- .stSuccess {
212
- background: rgba(72, 187, 120, 0.2);
213
  backdrop-filter: blur(10px);
214
  border-radius: 10px;
215
- border: 1px solid rgba(72, 187, 120, 0.4);
216
- color: white;
217
  }
218
 
219
  /* Footer */
@@ -246,19 +239,6 @@ def load_css():
246
  # TRANSLATION FUNCTIONS
247
  # ================================
248
 
249
- @st.cache_resource
250
- def load_translation_model():
251
- """Load the translation model (cached for performance)"""
252
- try:
253
- # Using a multilingual model that supports Urdu and Pashto
254
- model_name = "Helsinki-NLP/opus-mt-mul-en"
255
- tokenizer = MarianTokenizer.from_pretrained(model_name)
256
- model = MarianMTModel.from_pretrained(model_name)
257
- return tokenizer, model
258
- except Exception as e:
259
- st.error(f"Error loading model: {e}")
260
- return None, None
261
-
262
  def urdu_to_pashto_roman(urdu_text):
263
  """
264
  Translate Urdu to Pashto and romanize it
@@ -271,35 +251,44 @@ def urdu_to_pashto_roman(urdu_text):
271
  translator_to_pashto = GoogleTranslator(source='ur', target='ps')
272
  pashto_text = translator_to_pashto.translate(urdu_text)
273
 
274
- # Step 2: Transliterate Pashto to Roman script
275
- # Using English as intermediate for romanization
276
- translator_to_roman = GoogleTranslator(source='ps', target='en')
277
- temp_english = translator_to_roman.translate(pashto_text)
278
-
279
- # Step 3: Create phonetic Roman Urdu representation
280
- # This gives us a romanized version
281
- romanized = create_roman_representation(pashto_text, temp_english)
282
 
283
  return romanized
284
 
285
  except Exception as e:
286
- st.error(f"Translation error: {e}")
287
- return "Translation failed. Please try again."
288
 
289
- def create_roman_representation(pashto_text, english_reference):
290
  """
291
  Create a Roman Urdu representation of Pashto text
292
  """
293
- # Pashto to Roman Urdu character mapping (phonetic)
294
  pashto_to_roman = {
295
- 'ا': 'a', 'ب': 'b', 'پ': 'p', 'ت': 't', 'ټ': 't', 'ث': 's',
296
- 'ج': 'j', 'چ': 'ch', 'ح': 'h', 'خ': 'kh', 'د': 'd', 'ډ': 'd',
297
- 'ذ': 'z', 'ر': 'r', 'ړ': 'r', 'ز': 'z', 'ژ': 'zh', 'ږ': 'g',
298
- 'س': 's', 'ش': 'sh', 'ښ': 'kh', 'ص': 's', 'ض': 'z', 'ط': 't',
299
- 'ظ': 'z', 'ع': 'a', 'غ': 'gh', 'ف': 'f', 'ق': 'q', 'ک': 'k',
300
- 'ګ': 'g', 'ل': 'l', 'م': 'm', 'ن': 'n', 'ڼ': 'n', 'و': 'o',
301
- 'ه': 'h', 'ۀ': 'a', 'ي': 'i', 'ې': 'e', 'ی': 'i', 'ئ': 'y',
302
- ' ': ' ', '،': ',', '؟': '?', '۔': '.'
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
303
  }
304
 
305
  # Transliterate Pashto to Roman
@@ -307,17 +296,22 @@ def create_roman_representation(pashto_text, english_reference):
307
  for char in pashto_text:
308
  roman_text += pashto_to_roman.get(char, char)
309
 
310
- # If transliteration is too rough, use English reference with Urdu touch
311
- if len(roman_text.strip()) < 3:
312
- return english_reference
313
 
314
- return roman_text
315
 
316
  # ================================
317
  # MAIN APPLICATION
318
  # ================================
319
 
320
  def main():
 
 
 
 
 
 
321
  # Load custom CSS
322
  load_css()
323
 
@@ -335,41 +329,19 @@ def main():
335
  st.markdown("### 📝 Input (Urdu)")
336
  urdu_input = st.text_area(
337
  "Enter Urdu text",
 
338
  height=250,
339
- placeholder="یہاں اردو متن درج کریں...",
340
  label_visibility="collapsed",
341
- key="urdu_input"
342
  )
343
 
344
  with col2:
345
  st.markdown("### 🔤 Output (Pashto in Roman)")
346
- if 'translation_output' not in st.session_state:
347
- st.session_state.translation_output = ""
348
-
349
- translation_output = st.text_area(
350
  "Pashto translation in Roman script",
351
  value=st.session_state.translation_output,
352
  height=250,
353
  placeholder="Translation will appear here...",
354
  label_visibility="collapsed",
355
- key="output_display",
356
- disabled=True
357
- )
358
-
359
- # Buttons
360
- col_btn1, col_btn2, col_btn3 = st.columns([1, 1, 1])
361
-
362
- with col_btn2:
363
- if st.button("🔄 Translate", use_container_width=True):
364
- if urdu_input.strip():
365
- with st.spinner("✨ Translating..."):
366
- result = urdu_to_pashto_roman(urdu_input)
367
- st.session_state.translation_output = result
368
- st.rerun()
369
- else:
370
- st.warning("⚠️ Please enter some Urdu text first!")
371
-
372
- with col_btn3:
373
- st.markdown('<div class="clear-button">', unsafe_allow_html=True)
374
- if st.button("🗑️ Clear", use_container_width=True):
375
- st.session_state.translation_output = ""
 
1
  import streamlit as st
 
 
2
  from deep_translator import GoogleTranslator
3
+ import time
4
 
5
  # ================================
6
  # PAGE CONFIGURATION
 
164
  }
165
 
166
  /* Clear Button */
167
+ div[data-testid="column"]:last-child .stButton button {
168
  background: linear-gradient(145deg, #f093fb, #f5576c);
169
  box-shadow: 0 10px 25px rgba(245, 87, 108, 0.4);
170
  }
171
 
172
+ div[data-testid="column"]:last-child .stButton button:hover {
173
  box-shadow: 0 15px 35px rgba(245, 87, 108, 0.6);
174
  }
175
 
 
200
  border-top-color: #ffffff !important;
201
  }
202
 
203
+ /* Success/Warning Messages */
204
+ .stSuccess, .stWarning, .stError {
205
+ background: rgba(255, 255, 255, 0.2);
 
 
 
 
 
206
  backdrop-filter: blur(10px);
207
  border-radius: 10px;
208
+ border: 1px solid rgba(255, 255, 255, 0.4);
209
+ color: white !important;
210
  }
211
 
212
  /* Footer */
 
239
  # TRANSLATION FUNCTIONS
240
  # ================================
241
 
 
 
 
 
 
 
 
 
 
 
 
 
 
242
  def urdu_to_pashto_roman(urdu_text):
243
  """
244
  Translate Urdu to Pashto and romanize it
 
251
  translator_to_pashto = GoogleTranslator(source='ur', target='ps')
252
  pashto_text = translator_to_pashto.translate(urdu_text)
253
 
254
+ # Step 2: Create Roman representation
255
+ romanized = create_roman_representation(pashto_text)
 
 
 
 
 
 
256
 
257
  return romanized
258
 
259
  except Exception as e:
260
+ return f"Translation error: {str(e)}"
 
261
 
262
+ def create_roman_representation(pashto_text):
263
  """
264
  Create a Roman Urdu representation of Pashto text
265
  """
266
+ # Comprehensive Pashto to Roman Urdu character mapping (phonetic)
267
  pashto_to_roman = {
268
+ # Vowels
269
+ 'ا': 'a', 'آ': 'aa', 'ع': 'a', 'ء': '',
270
+ # Consonants
271
+ 'ب': 'b', 'پ': 'p', 'ت': 't', 'ټ': 't', 'ث': 's',
272
+ 'ج': 'j', 'چ': 'ch', 'ح': 'h', 'خ': 'kh',
273
+ 'د': 'd', 'ډ': 'd', 'ذ': 'z', 'ر': 'r', 'ړ': 'r',
274
+ 'ز': 'z', 'ژ': 'zh', 'ږ': 'g', 'س': 's', 'ش': 'sh',
275
+ 'ښ': 'x', 'ص': 's', 'ض': 'z', 'ط': 't', 'ظ': 'z',
276
+ 'غ': 'gh', 'ف': 'f', 'ق': 'q', 'ک': 'k', 'ګ': 'g',
277
+ 'ل': 'l', 'م': 'm', 'ن': 'n', 'ڼ': 'n',
278
+ 'و': 'w', 'ؤ': 'o', 'ه': 'h', 'ۀ': 'a', 'ة': 'a',
279
+ 'ي': 'y', 'ې': 'e', 'ی': 'i', 'ئ': 'y', 'ى': 'a',
280
+ # Diacritics and special marks
281
+ 'َ': 'a', 'ُ': 'u', 'ِ': 'i', 'ً': 'an', 'ٌ': 'un', 'ٍ': 'in',
282
+ 'ّ': '', 'ْ': '', 'ٔ': '', 'ٰ': 'a',
283
+ # Punctuation
284
+ ' ': ' ', '،': ',', '؛': ';', '؟': '?', '۔': '.', '!': '!',
285
+ '(': '(', ')': ')', '[': '[', ']': ']', '{': '{', '}': '}',
286
+ '-': '-', '_': '_', '«': '"', '»': '"', '"': '"', '"': '"',
287
+ # Numbers (Pashto/Arabic numerals to Roman)
288
+ '۰': '0', '۱': '1', '۲': '2', '۳': '3', '۴': '4',
289
+ '۵': '5', '۶': '6', '۷': '7', '۸': '8', '۹': '9',
290
+ '٠': '0', '١': '1', '٢': '2', '٣': '3', '٤': '4',
291
+ '٥': '5', '٦': '6', '٧': '7', '٨': '8', '٩': '9',
292
  }
293
 
294
  # Transliterate Pashto to Roman
 
296
  for char in pashto_text:
297
  roman_text += pashto_to_roman.get(char, char)
298
 
299
+ # Clean up extra spaces and formatting
300
+ roman_text = ' '.join(roman_text.split())
 
301
 
302
+ return roman_text if roman_text.strip() else pashto_text
303
 
304
  # ================================
305
  # MAIN APPLICATION
306
  # ================================
307
 
308
  def main():
309
+ # Initialize session state
310
+ if 'translation_output' not in st.session_state:
311
+ st.session_state.translation_output = ""
312
+ if 'urdu_input' not in st.session_state:
313
+ st.session_state.urdu_input = ""
314
+
315
  # Load custom CSS
316
  load_css()
317
 
 
329
  st.markdown("### 📝 Input (Urdu)")
330
  urdu_input = st.text_area(
331
  "Enter Urdu text",
332
+ value=st.session_state.urdu_input,
333
  height=250,
334
+ placeholder="یہاں اردو متن درج کریں... (Enter Urdu text here...)",
335
  label_visibility="collapsed",
336
+ key="urdu_input_area"
337
  )
338
 
339
  with col2:
340
  st.markdown("### 🔤 Output (Pashto in Roman)")
341
+ st.text_area(
 
 
 
342
  "Pashto translation in Roman script",
343
  value=st.session_state.translation_output,
344
  height=250,
345
  placeholder="Translation will appear here...",
346
  label_visibility="collapsed",
347
+ key="output_