File size: 2,585 Bytes
7150449
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
import os
import gradio as gr
from groq import Groq

# -------------------------------------------------
# Groq API Client (HF Secret)
# -------------------------------------------------
api_key = os.environ.get("GROQ_API_KEY")
if not api_key:
    raise RuntimeError(
        "GROQ_API_KEY is not set. Add it in Hugging Face Spaces → Settings → Secrets."
    )

client = Groq(api_key=api_key)

# -------------------------------------------------
# Translation Function
# -------------------------------------------------
def translate_english_to_urdu(text):
    if not text or not text.strip():
        return ""

    messages = [
        {
            "role": "system",
            "content": (
                "You are a professional English to Urdu translator. "
                "Translate accurately, clearly, and naturally into Urdu. "
                "Do NOT add explanations."
            )
        },
        {
            "role": "user",
            "content": text
        }
    ]

    try:
        completion = client.chat.completions.create(
            model="llama-3.3-70b-versatile",
            messages=messages
        )
        return completion.choices[0].message.content
    except Exception as e:
        return f"⚠️ Error: {str(e)}"

# -------------------------------------------------
# Theme & Custom CSS
# -------------------------------------------------
theme = gr.themes.Soft(
    primary_hue="blue",
    secondary_hue="gray",
    neutral_hue="gray",
    font=["Inter", "sans-serif"]
)

custom_css = """
#container {
    max-width: 850px;
    margin: auto;
}
textarea {
    font-size: 16px;
}
footer {display: none;}
"""

# -------------------------------------------------
# UI
# -------------------------------------------------
with gr.Blocks() as demo:
    with gr.Column(elem_id="container"):
        gr.Markdown(
            """
            # 🌍 English → Urdu Translator  
            **Powered by Groq LLaMA 3.3**  
            Fast • Accurate • User-Friendly
            """
        )

        english_input = gr.Textbox(
            label="English Text",
            placeholder="Enter English text here...",
            lines=6
        )

        translate_btn = gr.Button("Translate", variant="primary")

        urdu_output = gr.Textbox(
            label="Urdu Translation",
            lines=6,
            interactive=False
        )

        translate_btn.click(
            fn=translate_english_to_urdu,
            inputs=english_input,
            outputs=urdu_output
        )

demo.launch(theme=theme, css=custom_css)