File size: 6,112 Bytes
51a7d9e
 
c4c656e
65c31d6
51a7d9e
65c31d6
c4c656e
51a7d9e
 
c4c656e
ba2710b
c4c656e
 
 
 
 
0a38613
c4c656e
2fec857
c4c656e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b42e029
c4c656e
51a7d9e
c4c656e
 
 
3b9cb87
c4c656e
 
 
 
 
 
bbc7f7f
 
 
c4c656e
 
 
 
2fec857
fca0518
edb9e8a
0a38613
c4c656e
51a7d9e
 
 
 
4e2bf05
9ad1f27
4e2bf05
9ad1f27
c9792bc
51a7d9e
c4c656e
 
 
 
 
 
 
 
 
 
 
e7fb870
65c31d6
e7fb870
6033503
 
a983a99
6033503
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
51a7d9e
c4c656e
 
 
 
 
 
 
 
 
 
6033503
c4c656e
 
6033503
c4c656e
 
bbc7f7f
c4c656e
 
 
 
 
 
 
51a7d9e
 
c4c656e
51a7d9e
c4c656e
bbc7f7f
 
 
 
 
 
 
 
 
 
 
 
 
 
c4c656e
fca0518
c4c656e
 
 
 
 
 
 
 
 
 
 
 
 
 
b16d982
 
bbc7f7f
51a7d9e
 
 
 
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
import torch
import gradio as gr
import spaces
from transformers import AutoModelForCausalLM, AutoTokenizer, BitsAndBytesConfig
import os
import re
from polyglot.detect import Detector

HF_TOKEN = os.environ.get("HF_TOKEN", None)
MODEL = "LLaMAX/LLaMAX3-8B-Alpaca"
RELATIVE_MODEL="LLaMAX/LLaMAX3-8B"

TITLE = "<h1><center>LLaMAX3-8B-Translation</center></h1>"

quantization_config = BitsAndBytesConfig(load_in_8bit=True)

model = AutoModelForCausalLM.from_pretrained(
        MODEL,
        torch_dtype=torch.float16,
        device_map="auto",
        quantization_config=quantization_config)
tokenizer = AutoTokenizer.from_pretrained(MODEL)


def lang_detector(text):
    min_chars = 5
    if len(text) < min_chars:
        return "Input text too short"
    try:
        detector = Detector(text).language
        lang_info = str(detector)
        code = re.search(r"name: (\w+)", lang_info).group(1)
        return code
    except Exception as e:
        return f"ERROR:{str(e)}"

def Prompt_template(query, src_language, trg_language):
    instruction = f'Translate the following sentences from {src_language} to {trg_language}.'
    prompt = (
        'Below is an instruction that describes a task, paired with an input that provides further context. '
        'Write a response that appropriately completes the request.\n'
        f'### Instruction:\n{instruction}\n'
        f'### Input:\n{query}\n### Response:'
    )
    return prompt

# Unfinished
def chunk_text():
    pass
    
@spaces.GPU()
def translate(
    source_text: str, 
    source_lang: str,
    target_lang: str, 
    max_length: int,
    temperature: float,
    top_p: float,
    rp: float):
    
    print(f'Text is - {source_text}')
    
    prompt = Prompt_template(source_text, source_lang, target_lang)
    input_ids = tokenizer(prompt, return_tensors="pt").input_ids.to(model.device)
        
    generate_kwargs = dict(
        input_ids=input_ids,
        max_length=max_length, 
        do_sample=True, 
        temperature=temperature,
    )

    outputs = model.generate(**generate_kwargs)
    
    resp = tokenizer.decode(outputs[0], skip_special_tokens=True, clean_up_tokenization_spaces=False)
    
    yield resp[len(prompt):]

CSS = """
    h1 {
        text-align: center;
        display: block;
        height: 10vh;
        align-content: center;
    }
    footer {
        visibility: hidden;
    }
"""
DESCRIPTION = """
LLaMAX is a language model with powerful multilingual capabilities without loss instruction-following capabilities.
"""
LANG_LIST = ['Akrikaans', 'Amharic', 'Arabic', 'Armenian', 'Assamese', 'Asturian', 'Azerbaijani', \
             'Belarusian', 'Bengali', 'Bosnian', 'Bulgarian', 'Burmese', \
             'Catalan', 'Cebuano', 'Simplified Chinese', 'Traditional Chinese', 'Croatian', 'Czech', \
             'Danish', 'Dutch', 'English', 'Estonian', 'Filipino', 'Finnish', 'French', 'Fulah', \
             'Galician', 'Ganda', 'Georgian', 'German', 'Greek', 'Gujarati', \
             'Hausa', 'Hebrew', 'Hindi', 'Hungarian', \
             'Icelandic', 'Igbo', 'Indonesian', 'Irish', 'Italian', \
             'Japanese', 'Javanese', \
             'Kabuverdianu', 'Kamba', 'Kannada', 'Kazakh', 'Khmer', 'Korean', 'Kyrgyz', \
             'Lao', 'Latvian', 'Lingala', 'Lithuanian', 'Luo', 'Luxembourgish', \
             'Macedonian', 'Malay', 'Malayalam', 'Maltese', 'Maori', 'Marathi', 'Mongolian', \
             'Nepali', 'Northern', 'Norwegian', 'Nyanja', \
             'Occitan', 'Oriya', 'Oromo', \
             'Pashto', 'Persian', 'Polish', 'Portuguese', 'Punjabi', \
             'Romanian', 'Russian', \
             'Serbian', 'Shona', 'Sindhi', 'Slovak', 'Slovenian', 'Somali', 'Sorani', 'Spanish', 'Swahili', 'Swedish', \
             'Tajik', 'Tamil', 'Telugu', 'Thai', 'Turkish', \
             'Ukrainian', 'Umbundu', 'Urdu', 'Uzbek', \
             'Vietnamese', 'Welsh', 'Wolof', 'Xhosa', 'Yoruba', 'Zulu']

chatbot = gr.Chatbot(height=600)

with gr.Blocks(theme="soft", css=CSS) as demo:
    gr.Markdown(TITLE)
    with gr.Row():
        with gr.Column(scale=1):
            source_lang = gr.Textbox(
                label="Source Lang(Auto-Detect)",
                value="English",
            )
            target_lang = gr.Dropdown(
                label="Target Lang",
                value="Spanish",
                choices=LANG_LIST,
            )
            max_length = gr.Slider(
                label="Max Length",
                minimum=512,
                maximum=8192,
                value=4096,
                step=8,
            )
            temperature = gr.Slider(
                label="Temperature",
                minimum=0,
                maximum=1,
                value=0.3,
                step=0.1,
            )
            top_p = gr.Slider(
                minimum=0.0,
                maximum=1.0,
                step=0.1,
                value=1.0,
                label="top_p",
            )
            rp = gr.Slider(
                minimum=0.0,
                maximum=2.0,
                step=0.1,
                value=1.2,
                label="Repetition penalty",
            )
            gr.Markdown(DESCRIPTION)
        with gr.Column(scale=4):
            source_text = gr.Textbox(
                label="Source Text",
                value="How we live is so different from how we ought to live that he who studies "+\
                "what ought to be done rather than what is done will learn the way to his downfall "+\
                "rather than to his preservation.",
                lines=10,
            )
            output_text = gr.Textbox(
                label="Output Text",
                lines=10,
            )
    with gr.Row():
        submit = gr.Button(value="Submit")
        clear = gr.ClearButton([source_text, output_text])
        
    source_text.change(lang_detector, source_text, source_lang)
    submit.click(fn=translate, inputs=[source_text, source_lang, target_lang, max_length, temperature, top_p, rp], outputs=[output_text])


if __name__ == "__main__":
    demo.launch()