Translator / app.py
muhammadrazapathan's picture
Create app.py
97ba343 verified
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()