import os import gradio as gr from groq import Groq import re # Initialize Groq client (API key from HF Secrets) client = Groq(api_key=os.environ.get("GROQ_API_KEY")) def translate_to_urdu(text): """ Translate English text to Urdu using Groq's Qwen 3 32B model. Returns Urdu, Roman Urdu transliteration, and pronunciation guide. """ if not text.strip(): return "براہ کرم متن درج کریں", "Please enter some text", "" # System prompt optimized for translation with cultural context system_prompt = """You are an expert English to Urdu translator specializing in Pakistani and Indian cultural context. Rules: 1. Translate the English text to natural, conversational Urdu (Nastaliq script) 2. Provide Roman Urdu transliteration (English alphabets representing Urdu pronunciation) 3. Add a brief pronunciation guide for difficult words 4. Maintain cultural nuances and idioms appropriately Format your response EXACTLY as: URDU: [Urdu translation in nastaliq script] ROMAN: [Roman Urdu transliteration] PRONUNCIATION: [Key pronunciation tips]""" try: completion = client.chat.completions.create( model="qwen/qwen3-32b", # REPLACED: mistral-saba-24b → qwen/qwen3-32b (Recommended by Groq) messages=[ {"role": "system", "content": system_prompt}, {"role": "user", "content": f"Translate: {text}"} ], temperature=0.3, max_tokens=1024 ) response = completion.choices[0].message.content # Parse the structured response urdu_match = re.search(r'URDU:\s*(.+?)(?=ROMAN:|PRONUNCIATION:|$)', response, re.DOTALL) roman_match = re.search(r'ROMAN:\s*(.+?)(?=URDU:|PRONUNCIATION:|$)', response, re.DOTALL) pron_match = re.search(r'PRONUNCIATION:\s*(.+?)(?=URDU:|ROMAN:|$)', response, re.DOTALL) urdu = urdu_match.group(1).strip() if urdu_match else "ترجمہ دستیاب نہیں" roman = roman_match.group(1).strip() if roman_match else "Transliteration not available" pronunciation = pron_match.group(1).strip() if pron_match else "" return urdu, roman, pronunciation except Exception as e: return f"Error: {str(e)}", "Translation service unavailable", "" # Custom CSS for beautiful UI with animations custom_css = """ @import url('https://fonts.googleapis.com/css2?family=Noto+Nastaliq+Urdu:wght@400;700&family=Inter:wght@300;400;600&display=swap'); .urdu-text { font-family: 'Noto Nastaliq Urdu', serif; font-size: 1.5rem; line-height: 2.5; direction: rtl; text-align: right; background: linear-gradient(135deg, #667eea 0%, #764ba2 100%); -webkit-background-clip: text; -webkit-text-fill-color: transparent; background-clip: text; animation: fadeIn 0.8s ease-in; } .roman-text { font-family: 'Inter', sans-serif; font-size: 1.2rem; color: #4a5568; font-style: italic; margin-top: 0.5rem; animation: slideUp 0.6s ease-out; } .pronunciation-box { background: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%); border-left: 4px solid #667eea; padding: 1rem; border-radius: 0 8px 8px 0; margin-top: 1rem; animation: slideUp 0.8s ease-out; } @keyframes fadeIn { from { opacity: 0; transform: translateY(-10px); } to { opacity: 1; transform: translateY(0); } } @keyframes slideUp { from { opacity: 0; transform: translateY(20px); } to { opacity: 1; transform: translateY(0); } } .translate-btn { background: linear-gradient(135deg, #667eea 0%, #764ba2 100%) !important; border: none !important; color: white !important; font-weight: 600 !important; transition: transform 0.2s, box-shadow 0.2s !important; } .translate-btn:hover { transform: translateY(-2px) !important; box-shadow: 0 10px 20px rgba(102, 126, 234, 0.3) !important; } .input-box { border: 2px solid #e2e8f0 !important; border-radius: 12px !important; transition: border-color 0.3s !important; } .input-box:focus { border-color: #667eea !important; box-shadow: 0 0 0 3px rgba(102, 126, 234, 0.1) !important; } .header-text { text-align: center; background: linear-gradient(135deg, #667eea 0%, #764ba2 100%); -webkit-background-clip: text; -webkit-text-fill-color: transparent; font-size: 2.5rem; font-weight: 700; margin-bottom: 0.5rem; } .subheader-text { text-align: center; color: #718096; font-size: 1.1rem; margin-bottom: 2rem; } """ # Create Gradio interface with gr.Blocks(css=custom_css, theme=gr.themes.Soft()) as demo: gr.HTML("""