import streamlit as st from deep_translator import GoogleTranslator import time # ================================ # PAGE CONFIGURATION # ================================ st.set_page_config( page_title="Urdu to Pashto Translator", page_icon="🌐", layout="wide", initial_sidebar_state="collapsed" ) # ================================ # CUSTOM CSS - GLASSMORPHISM THEME # ================================ def load_css(): st.markdown(""" """, unsafe_allow_html=True) # ================================ # TRANSLATION FUNCTIONS # ================================ def urdu_to_pashto_roman(urdu_text): """ Translate Urdu to Pashto and romanize it """ if not urdu_text.strip(): return "" try: # Step 1: Translate Urdu to Pashto using Google Translator translator_to_pashto = GoogleTranslator(source='ur', target='ps') pashto_text = translator_to_pashto.translate(urdu_text) # Step 2: Create Roman representation romanized = create_roman_representation(pashto_text) return romanized except Exception as e: return f"Translation error: {str(e)}" def create_roman_representation(pashto_text): """ Create a Roman Urdu representation of Pashto text """ # Comprehensive Pashto to Roman Urdu character mapping (phonetic) pashto_to_roman = { # Vowels 'ا': 'a', 'آ': 'aa', 'ع': 'a', 'ء': '', # Consonants 'ب': 'b', 'پ': 'p', 'ت': 't', 'ټ': 't', 'ث': 's', 'ج': 'j', 'چ': 'ch', 'ح': 'h', 'خ': 'kh', 'د': 'd', 'ډ': 'd', 'ذ': 'z', 'ر': 'r', 'ړ': 'r', 'ز': 'z', 'ژ': 'zh', 'ږ': 'g', 'س': 's', 'ش': 'sh', 'ښ': 'x', 'ص': 's', 'ض': 'z', 'ط': 't', 'ظ': 'z', 'غ': 'gh', 'ف': 'f', 'ق': 'q', 'ک': 'k', 'ګ': 'g', 'ل': 'l', 'م': 'm', 'ن': 'n', 'ڼ': 'n', 'و': 'w', 'ؤ': 'o', 'ه': 'h', 'ۀ': 'a', 'ة': 'a', 'ي': 'y', 'ې': 'e', 'ی': 'i', 'ئ': 'y', 'ى': 'a', # Diacritics and special marks 'َ': 'a', 'ُ': 'u', 'ِ': 'i', 'ً': 'an', 'ٌ': 'un', 'ٍ': 'in', 'ّ': '', 'ْ': '', 'ٔ': '', 'ٰ': 'a', # Punctuation ' ': ' ', '،': ',', '؛': ';', '؟': '?', '۔': '.', '!': '!', '(': '(', ')': ')', '[': '[', ']': ']', '{': '{', '}': '}', '-': '-', '_': '_', '«': '"', '»': '"', '"': '"', '"': '"', # Numbers (Pashto/Arabic numerals to Roman) '۰': '0', '۱': '1', '۲': '2', '۳': '3', '۴': '4', '۵': '5', '۶': '6', '۷': '7', '۸': '8', '۹': '9', '٠': '0', '١': '1', '٢': '2', '٣': '3', '٤': '4', '٥': '5', '٦': '6', '٧': '7', '٨': '8', '٩': '9', } # Transliterate Pashto to Roman roman_text = '' for char in pashto_text: roman_text += pashto_to_roman.get(char, char) # Clean up extra spaces and formatting roman_text = ' '.join(roman_text.split()) return roman_text if roman_text.strip() else pashto_text # ================================ # MAIN APPLICATION # ================================ def main(): # Initialize session state if 'translation_output' not in st.session_state: st.session_state.translation_output = "" if 'urdu_input' not in st.session_state: st.session_state.urdu_input = "" # Load custom CSS load_css() # Header st.markdown('

🌐 Urdu to Pashto Translator

', unsafe_allow_html=True) st.markdown('

Convert Urdu sentences to Pashto in Roman script

', unsafe_allow_html=True) # Main container st.markdown('
', unsafe_allow_html=True) # Create two columns for input and output col1, col2 = st.columns(2) with col1: st.markdown("### 📝 Input (Urdu)") urdu_input = st.text_area( "Enter Urdu text", value=st.session_state.urdu_input, height=250, placeholder="یہاں اردو متن درج کریں... (Enter Urdu text here...)", label_visibility="collapsed", key="urdu_input_area" ) with col2: st.markdown("### 🔤 Output (Pashto in Roman)") st.text_area( "Pashto translation in Roman script", value=st.session_state.translation_output, height=250, placeholder="Translation will appear here...", label_visibility="collapsed", key="output_