Spaces:
Runtime error
Runtime error
| 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() | |