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()