Spaces:
Runtime error
Runtime error
File size: 1,978 Bytes
97ba343 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 | 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()
|