import gradio as gr import os from groq import Groq # Load Groq API key securely GROQ_API_KEY = os.getenv("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") if not GROQ_API_KEY: raise ValueError("❌ Please set your GROQ_API_KEY in environment variables.") client = Groq(api_key=GROQ_API_KEY) def translate_english_to_urdu(text): if not text.strip(): return "⚠️ Please enter English text to translate." try: completion = client.chat.completions.create( model="llama3-70b-8192", messages=[ {"role": "system", "content": "You are a professional translator. Translate English into pure, natural, and grammatically correct Urdu."}, {"role": "user", "content": text} ], temperature=0.2, max_tokens=1024 ) return completion.choices[0].message.content.strip() except Exception as e: return f"❌ Error: {str(e)}" with gr.Blocks(title="🌍 English to Urdu Translator (Powered by Groq)") as demo: gr.Markdown(""" # 🌍 English → Urdu Translator **Powered by Groq AI • Deployed on Hugging Face** Translate English text into accurate, natural, and fluent Urdu instantly. """) with gr.Row(): english_input = gr.Textbox(label="✍️ Enter English Text", placeholder="Type your English text here...", lines=6) urdu_output = gr.Textbox(label="📜 Urdu Translation", lines=6) translate_btn = gr.Button("🔄 Translate") translate_btn.click(fn=translate_english_to_urdu, inputs=english_input, outputs=urdu_output) gr.Markdown(""" --- ### ✨ Features ✔️ High-accuracy translation ✔️ Fast responses using Groq ✔️ Clean & modern UI ✔️ Ready for Hugging Face deployment ✔️ Production-grade code 👨‍💻 Built with ❤️ by Muhammad Raza """) if __name__ == "__main__": demo.launch()