import os import gradio as gr from groq import Groq # Load API key securely client = Groq(api_key=os.environ.get("GROQ_API_KEY")) SYSTEM_PROMPT = """ You are a professional English to Urdu translator. Rules: - Translate accurately and naturally - Use proper Urdu grammar - Do not add explanations - Output ONLY Urdu text """ def translate_to_urdu(english_text): if not english_text.strip(): return "" response = client.chat.completions.create( model="llama-3.3-70b-versatile", messages=[ {"role": "system", "content": SYSTEM_PROMPT}, {"role": "user", "content": english_text} ], temperature=0.2, max_tokens=500 ) return response.choices[0].message.content.strip() # Demo click function def demo_translate(text): return text, translate_to_urdu(text) # UI with gr.Blocks(theme=gr.themes.Soft(), title="English → Urdu Translator") as demo: gr.Markdown( """ # 🌍 English → Urdu Translator Chatbot **Powered by Groq LLM** """ ) input_text = gr.Textbox( label="English Text", placeholder="Enter English sentence here...", lines=4 ) output_text = gr.Textbox( label="Urdu Translation", lines=4 ) translate_btn = gr.Button("Translate 🇵🇰") translate_btn.click(translate_to_urdu, input_text, output_text) gr.Markdown("### 🔹 Try Demo Examples (Click & Translate)") demo_sentences = [ "Education is the key to success.", "Artificial intelligence is changing the world.", "Please submit your assignment before Friday.", "Healthcare systems need modern technology.", "Pakistan is a beautiful country with rich culture.", "I am learning machine learning and data science." ] with gr.Row(): for sentence in demo_sentences[:3]: gr.Button(sentence).click( demo_translate, outputs=[input_text, output_text], inputs=[] ) with gr.Row(): for sentence in demo_sentences[3:]: gr.Button(sentence).click( demo_translate, outputs=[input_text, output_text], inputs=[] ) demo.launch()