File size: 2,311 Bytes
ddd80b9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1eff71c
ddd80b9
 
 
 
 
 
 
 
 
 
0f4418f
 
 
 
 
ddd80b9
 
 
0f4418f
 
ddd80b9
 
 
0f4418f
 
 
 
 
 
 
 
 
 
ddd80b9
 
 
 
0f4418f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import os
import gradio as gr
from groq import Groq

# Load API key securely
client = Groq(api_key=os.environ.get("GROQ_API_KEY"))

SYSTEM_PROMPT = """
You are a professional English to Urdu translator.
Rules:
- Translate accurately and naturally
- Use proper Urdu grammar
- Do not add explanations
- Output ONLY Urdu text
"""

def translate_to_urdu(english_text):
    if not english_text.strip():
        return ""

    response = client.chat.completions.create(
        model="llama-3.3-70b-versatile",
        messages=[
            {"role": "system", "content": SYSTEM_PROMPT},
            {"role": "user", "content": english_text}
        ],
        temperature=0.2,
        max_tokens=500
    )

    return response.choices[0].message.content.strip()

# Demo click function
def demo_translate(text):
    return text, translate_to_urdu(text)

# UI
with gr.Blocks(theme=gr.themes.Soft(), title="English β†’ Urdu Translator") as demo:
    gr.Markdown(
        """
        # 🌍 English β†’ Urdu Translator Chatbot  
        **Powered by Groq LLM**
        """
    )

    input_text = gr.Textbox(
        label="English Text",
        placeholder="Enter English sentence here...",
        lines=4
    )

    output_text = gr.Textbox(
        label="Urdu Translation",
        lines=4
    )

    translate_btn = gr.Button("Translate πŸ‡΅πŸ‡°")
    translate_btn.click(translate_to_urdu, input_text, output_text)

    gr.Markdown("### πŸ”Ή Try Demo Examples (Click & Translate)")

    demo_sentences = [
        "Education is the key to success.",
        "Artificial intelligence is changing the world.",
        "Please submit your assignment before Friday.",
        "Healthcare systems need modern technology.",
        "Pakistan is a beautiful country with rich culture.",
        "I am learning machine learning and data science."
    ]

    with gr.Row():
        for sentence in demo_sentences[:3]:
            gr.Button(sentence).click(
                demo_translate,
                outputs=[input_text, output_text],
                inputs=[]
            )

    with gr.Row():
        for sentence in demo_sentences[3:]:
            gr.Button(sentence).click(
                demo_translate,
                outputs=[input_text, output_text],
                inputs=[]
            )

demo.launch()