File size: 7,152 Bytes
aa84e52
 
 
 
 
 
 
 
 
 
 
 
a31db9e
 
4320235
a31db9e
 
aa84e52
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a31db9e
aa84e52
4320235
 
 
a31db9e
4320235
 
 
 
 
 
 
 
 
 
 
 
 
 
 
845f6f3
4320235
 
 
 
 
 
845f6f3
aa84e52
4320235
845f6f3
4320235
 
 
 
 
 
 
 
 
845f6f3
4320235
 
845f6f3
 
4320235
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
aa84e52
 
a31db9e
aa84e52
a31db9e
aa84e52
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a31db9e
 
 
aa84e52
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a31db9e
4320235
a31db9e
aa84e52
 
 
 
845f6f3
aa84e52
 
4320235
 
 
 
 
 
 
 
 
 
aa84e52
a31db9e
 
 
 
4320235
 
 
 
 
 
 
 
 
a31db9e
aa84e52
 
 
 
 
a31db9e
 
 
 
 
4320235
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
845f6f3
aa84e52
de2970a
 
4320235
845f6f3
 
de2970a
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
import gradio as gr
import torch
from transformers import AutoModelForSeq2SeqLM, AutoTokenizer

# Model configuration
model_name = "ai4bharat/IndicBART"

# Load tokenizer and model on CPU
print("Loading IndicBART tokenizer...")
tokenizer = AutoTokenizer.from_pretrained(model_name, do_lower_case=False, use_fast=False, keep_accents=True)

print("Loading IndicBART model on CPU...")
model = AutoModelForSeq2SeqLM.from_pretrained(
    model_name, 
    torch_dtype=torch.float32,
    device_map="cpu"
)

# Language mapping
LANGUAGE_CODES = {
    "Assamese": "<2as>",
    "Bengali": "<2bn>", 
    "English": "<2en>",
    "Gujarati": "<2gu>",
    "Hindi": "<2hi>",
    "Kannada": "<2kn>",
    "Malayalam": "<2ml>",
    "Marathi": "<2mr>",
    "Oriya": "<2or>",
    "Punjabi": "<2pa>",
    "Tamil": "<2ta>",
    "Telugu": "<2te>"
}

def generate_response(input_text, source_lang, target_lang, task_type, max_length):
    """Generate response using IndicBART on CPU"""
    
    if not input_text.strip():
        return "Please enter some text to process."
    
    try:
        # Get language codes
        src_code = LANGUAGE_CODES[source_lang]
        tgt_code = LANGUAGE_CODES[target_lang]
        
        # Format input based on task type
        if task_type == "Translation":
            formatted_input = f"{input_text} </s> {src_code}"
            decoder_start_token = tgt_code
        elif task_type == "Text Completion":
            formatted_input = f"{input_text} </s> {tgt_code}"
            decoder_start_token = tgt_code
        else:  # Text Generation
            formatted_input = f"{input_text} </s> {src_code}"
            decoder_start_token = tgt_code
        
        # FIX 1: Tokenize with explicit token_type_ids=False
        inputs = tokenizer(
            formatted_input, 
            return_tensors="pt", 
            padding=True, 
            truncation=True, 
            max_length=512,
            return_token_type_ids=False  # KEY FIX: Prevent token_type_ids
        )
        
        # FIX 2: Alternative approach - manually remove if present
        if 'token_type_ids' in inputs:
            del inputs['token_type_ids']
        
        # Get decoder start token id
        try:
            decoder_start_token_id = tokenizer._convert_token_to_id_with_added_voc(decoder_start_token)
        except:
            decoder_start_token_id = tokenizer.convert_tokens_to_ids(decoder_start_token)
        
        # FIX 3: Use explicit parameters instead of **inputs (most reliable)
        with torch.no_grad():
            outputs = model.generate(
                input_ids=inputs['input_ids'],  # Explicit parameter
                attention_mask=inputs['attention_mask'],  # Explicit parameter
                decoder_start_token_id=decoder_start_token_id,
                max_length=max_length,
                num_beams=2,
                early_stopping=True,
                pad_token_id=tokenizer.pad_token_id,
                eos_token_id=tokenizer.eos_token_id,
                use_cache=True,
                do_sample=False
            )
        
        # Decode output
        generated_text = tokenizer.decode(outputs[0], skip_special_tokens=True, clean_up_tokenization_spaces=False)
        return generated_text
        
    except Exception as e:
        return f"Error generating response: {str(e)}"

# Create Gradio interface
with gr.Blocks(title="IndicBART CPU Multilingual Assistant", theme=gr.themes.Soft()) as demo:
    gr.Markdown("""
    # ๐Ÿ‡ฎ๐Ÿ‡ณ IndicBART Multilingual Assistant (CPU Version)
    
    Experience IndicBART - trained on **11 Indian languages**! Perfect for translation, text completion, and multilingual generation.
    
    **Supported Languages**: Assamese, Bengali, Gujarati, Hindi, Kannada, Malayalam, Marathi, Oriya, Punjabi, Tamil, Telugu, English
    """)
    
    with gr.Row():
        with gr.Column(scale=3):
            input_text = gr.Textbox(
                label="Input Text",
                placeholder="Enter text in any supported language...",
                lines=3
            )
            
            output_text = gr.Textbox(
                label="Generated Output",
                lines=5,
                interactive=False
            )
            
            with gr.Row():
                generate_btn = gr.Button("Generate", variant="primary", size="lg")
                clear_btn = gr.Button("Clear", variant="secondary")
        
        with gr.Column(scale=1):
            task_type = gr.Dropdown(
                choices=["Translation", "Text Completion", "Text Generation"],
                value="Translation",
                label="Task Type"
            )
            
            source_lang = gr.Dropdown(
                choices=list(LANGUAGE_CODES.keys()),
                value="English",
                label="Source Language"
            )
            
            target_lang = gr.Dropdown(
                choices=list(LANGUAGE_CODES.keys()),
                value="Hindi",
                label="Target Language"
            )
            
            max_length = gr.Slider(
                minimum=20,
                maximum=200,
                value=80,
                step=10,
                label="Max Length"
            )
    
    # Simple examples without caching
    gr.Markdown("### ๐Ÿ’ก Try these examples:")
    
    with gr.Row():
        with gr.Column():
            gr.Markdown("**English to Hindi**")
            example1_btn = gr.Button("Hello, how are you?")
        with gr.Column():
            gr.Markdown("**Hindi to English**") 
            example2_btn = gr.Button("เคฎเฅˆเค‚ เคเค• เค›เคพเคคเฅเคฐ เคนเฅ‚เค‚")
        with gr.Column():
            gr.Markdown("**Bengali to English**")
            example3_btn = gr.Button("เฆ†เฆฎเฆฟ เฆญเฆพเฆค เฆ–เฆพเฆ‡")
    
    # Event handlers
    def clear_fields():
        return "", ""
    
    def set_example1():
        return "Hello, how are you?", "English", "Hindi", "Translation"
    
    def set_example2():
        return "เคฎเฅˆเค‚ เคเค• เค›เคพเคคเฅเคฐ เคนเฅ‚เค‚", "Hindi", "English", "Translation"
    
    def set_example3():
        return "เฆ†เฆฎเฆฟ เฆญเฆพเฆค เฆ–เฆพเฆ‡", "Bengali", "English", "Translation"
    
    # Connect buttons
    generate_btn.click(
        generate_response,
        inputs=[input_text, source_lang, target_lang, task_type, max_length],
        outputs=output_text
    )
    
    clear_btn.click(
        clear_fields,
        outputs=[input_text, output_text]
    )
    
    example1_btn.click(
        set_example1,
        outputs=[input_text, source_lang, target_lang, task_type]
    )
    
    example2_btn.click(
        set_example2,
        outputs=[input_text, source_lang, target_lang, task_type]
    )
    
    example3_btn.click(
        set_example3,
        outputs=[input_text, source_lang, target_lang, task_type]
    )

# FIX 4: Updated launch parameters (removed cache_examples)
if __name__ == "__main__":
    demo.launch(
        share=True,
        show_error=True,
        enable_queue=False,
        # Removed cache_examples parameter - not supported in newer Gradio versions
    )