File size: 3,807 Bytes
03069df
83b48b4
 
03069df
83b48b4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
03069df
83b48b4
 
 
 
03069df
83b48b4
 
03069df
83b48b4
 
03069df
83b48b4
03069df
83b48b4
 
 
 
 
 
 
 
03069df
83b48b4
 
 
03069df
83b48b4
03069df
83b48b4
03069df
83b48b4
 
03069df
83b48b4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
03069df
83b48b4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
import gradio as gr
from transformers import pipeline
import torch

# ----------------------------------------
# Supported Languages
# ----------------------------------------
languages = [
    "English",
    "Hindi",
    "Tamil",
    "Telugu",
    "Kannada",
    "Malayalam",
    "Bengali",
    "Marathi",
    "Gujarati",
    "Punjabi",
    "Urdu"
]

# ----------------------------------------
# Translation Function
# ----------------------------------------
def translate_text(hf_token, source_lang, target_lang, input_text):

    if not hf_token.strip():
        return "Please enter Hugging Face Access Token."

    if not input_text.strip():
        return "Please enter text."

    try:

        # Load IBM Granite model
        pipe = pipeline(
            "text-generation",
            model="ibm-granite/granite-3.3-2b-base",
            token=hf_token,
            device_map="auto",
            torch_dtype=torch.float16
        )

        # Translation Prompt
        prompt = f"""
You are an expert multilingual translator.

Translate the below text from {source_lang} to {target_lang}.

Only provide translated text.

Text:
{input_text}
"""

        # Generate Translation
        result = pipe(
            prompt,
            max_new_tokens=200,
            temperature=0.2
        )

        translated_text = result[0]["generated_text"]

        # Remove original prompt if generated
        translated_text = translated_text.replace(prompt, "").strip()

        return translated_text

    except Exception as e:
        return f"Error: {str(e)}"


# ----------------------------------------
# Professional UI Styling
# ----------------------------------------
custom_css = """
body {
    background: linear-gradient(to right, #0f2027, #203a43, #2c5364);
}

.gradio-container {
    font-family: 'Segoe UI', sans-serif;
}

.main-title {
    text-align: center;
    font-size: 42px;
    font-weight: bold;
    color: white;
}

.sub-title {
    text-align: center;
    color: #dfe6e9;
    font-size: 18px;
    margin-bottom: 20px;
}

textarea {
    border-radius: 12px !important;
}

footer {
    visibility: hidden;
}
"""

# ----------------------------------------
# Gradio Interface
# ----------------------------------------
with gr.Blocks(
    theme=gr.themes.Soft(),
    css=custom_css
) as demo:

    gr.Markdown(
        """
        <div class="main-title">
            🌍 AI Multilingual Translator
        </div>

        <div class="sub-title">
            IBM Granite + Hugging Face + Gradio
        </div>
        """
    )

    with gr.Row():

        with gr.Column(scale=1):

            hf_token = gr.Textbox(
                label="πŸ”‘ Hugging Face Access Token",
                placeholder="Paste your HF token here",
                type="password"
            )

            source_lang = gr.Dropdown(
                choices=languages,
                value="English",
                label="🌐 Source Language"
            )

            target_lang = gr.Dropdown(
                choices=languages,
                value="Hindi",
                label="🎯 Target Language"
            )

        with gr.Column(scale=2):

            input_text = gr.Textbox(
                label="πŸ“ Enter Text",
                lines=8,
                placeholder="Type text to translate..."
            )

            output_text = gr.Textbox(
                label="βœ… Translated Output",
                lines=8
            )

    translate_btn = gr.Button(
        "πŸš€ Translate",
        variant="primary"
    )

    translate_btn.click(
        fn=translate_text,
        inputs=[
            hf_token,
            source_lang,
            target_lang,
            input_text
        ],
        outputs=output_text
    )

# Launch App
demo.launch(share=True)