Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,183 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import torch
|
2 |
+
import gradio as gr
|
3 |
+
import spaces
|
4 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer
|
5 |
+
import os
|
6 |
+
import re
|
7 |
+
from polyglot.detect import Detector
|
8 |
+
|
9 |
+
HF_TOKEN = os.environ.get("HF_TOKEN", None)
|
10 |
+
MODEL = "LLaMAX/LLaMAX3-8B-Alpaca"
|
11 |
+
RELATIVE_MODEL="LLaMAX/LLaMAX3-8B"
|
12 |
+
|
13 |
+
TITLE = "<h1><center>LLaMAX3-Translator</center></h1>"
|
14 |
+
|
15 |
+
|
16 |
+
model = AutoModelForCausalLM.from_pretrained(
|
17 |
+
MODEL,
|
18 |
+
torch_dtype=torch.float16,
|
19 |
+
device_map="auto")
|
20 |
+
tokenizer = AutoTokenizer.from_pretrained(MODEL)
|
21 |
+
|
22 |
+
|
23 |
+
def lang_detector(text):
|
24 |
+
min_chars = 5
|
25 |
+
if len(text) < min_chars:
|
26 |
+
return "Input text too short"
|
27 |
+
try:
|
28 |
+
detector = Detector(text).language
|
29 |
+
lang_info = str(detector)
|
30 |
+
code = re.search(r"name: (\w+)", lang_info).group(1)
|
31 |
+
return code
|
32 |
+
except Exception as e:
|
33 |
+
return f"ERROR:{str(e)}"
|
34 |
+
|
35 |
+
def Prompt_template(inst, prompt, query, src_language, trg_language):
|
36 |
+
inst = inst.format(src_language=src_language, trg_language=trg_language)
|
37 |
+
instruction = f"`{inst}`"
|
38 |
+
prompt = (
|
39 |
+
f'{prompt}'
|
40 |
+
f'### Instruction:\n{instruction}\n'
|
41 |
+
f'### Input:\n{query}\n### Response:'
|
42 |
+
)
|
43 |
+
return prompt
|
44 |
+
|
45 |
+
# Unfinished
|
46 |
+
def chunk_text():
|
47 |
+
pass
|
48 |
+
|
49 |
+
@spaces.GPU(duration=60)
|
50 |
+
def translate(
|
51 |
+
source_text: str,
|
52 |
+
source_lang: str,
|
53 |
+
target_lang: str,
|
54 |
+
inst: str,
|
55 |
+
prompt: str,
|
56 |
+
max_length: int,
|
57 |
+
temperature: float,
|
58 |
+
top_p: float,
|
59 |
+
rp: float):
|
60 |
+
|
61 |
+
print(f'Text is - {source_text}')
|
62 |
+
|
63 |
+
prompt = Prompt_template(inst, prompt, source_text, source_lang, target_lang)
|
64 |
+
input_ids = tokenizer(prompt, return_tensors="pt").input_ids.to(model.device)
|
65 |
+
|
66 |
+
generate_kwargs = dict(
|
67 |
+
input_ids=input_ids,
|
68 |
+
max_length=max_length,
|
69 |
+
do_sample=True,
|
70 |
+
temperature=temperature,
|
71 |
+
top_p=top_p,
|
72 |
+
repetition_penalty=rp,
|
73 |
+
)
|
74 |
+
|
75 |
+
outputs = model.generate(**generate_kwargs)
|
76 |
+
|
77 |
+
resp = tokenizer.decode(outputs[0], skip_special_tokens=True, clean_up_tokenization_spaces=False)
|
78 |
+
|
79 |
+
yield resp[len(prompt):]
|
80 |
+
|
81 |
+
CSS = """
|
82 |
+
h1 {
|
83 |
+
text-align: center;
|
84 |
+
display: block;
|
85 |
+
height: 10vh;
|
86 |
+
align-content: center;
|
87 |
+
}
|
88 |
+
footer {
|
89 |
+
visibility: hidden;
|
90 |
+
}
|
91 |
+
"""
|
92 |
+
|
93 |
+
LICENSE = """
|
94 |
+
Model: <a href="https://huggingface.co/LLaMAX/LLaMAX3-8B-Alpaca">LLaMAX3-8B-Alpaca</a>
|
95 |
+
"""
|
96 |
+
|
97 |
+
LANG_LIST = [
|
98 |
+
'Assamese', 'Bengali', 'Gujarati', 'Hindi', 'Kannada', 'Kashmiri', 'Konkani',
|
99 |
+
'Malayalam', 'Manipuri', 'Marathi', 'Nepali', 'Oriya', 'Punjabi',
|
100 |
+
'Sanskrit', 'Sindhi', 'Tamil', 'Telugu', 'Urdu', 'English'
|
101 |
+
]
|
102 |
+
|
103 |
+
|
104 |
+
chatbot = gr.Chatbot(height=600)
|
105 |
+
|
106 |
+
with gr.Blocks(theme="soft", css=CSS) as demo:
|
107 |
+
gr.Markdown(TITLE)
|
108 |
+
with gr.Row():
|
109 |
+
with gr.Column(scale=1):
|
110 |
+
source_lang = gr.Textbox(
|
111 |
+
label="Source Lang(Auto-Detect)",
|
112 |
+
value="English",
|
113 |
+
)
|
114 |
+
target_lang = gr.Dropdown(
|
115 |
+
label="Target Lang",
|
116 |
+
value="Spanish",
|
117 |
+
choices=LANG_LIST,
|
118 |
+
)
|
119 |
+
max_length = gr.Slider(
|
120 |
+
label="Max Length",
|
121 |
+
minimum=512,
|
122 |
+
maximum=8192,
|
123 |
+
value=4096,
|
124 |
+
step=8,
|
125 |
+
)
|
126 |
+
temperature = gr.Slider(
|
127 |
+
label="Temperature",
|
128 |
+
minimum=0,
|
129 |
+
maximum=1,
|
130 |
+
value=0.3,
|
131 |
+
step=0.1,
|
132 |
+
)
|
133 |
+
top_p = gr.Slider(
|
134 |
+
minimum=0.0,
|
135 |
+
maximum=1.0,
|
136 |
+
step=0.1,
|
137 |
+
value=1.0,
|
138 |
+
label="top_p",
|
139 |
+
)
|
140 |
+
rp = gr.Slider(
|
141 |
+
minimum=0.0,
|
142 |
+
maximum=2.0,
|
143 |
+
step=0.1,
|
144 |
+
value=1.2,
|
145 |
+
label="Repetition penalty",
|
146 |
+
)
|
147 |
+
with gr.Accordion("Advanced Options", open=False):
|
148 |
+
inst = gr.Textbox(
|
149 |
+
label="Instruction",
|
150 |
+
value="Translate the following sentences from {src_language} to {trg_language}.",
|
151 |
+
lines=3,
|
152 |
+
)
|
153 |
+
prompt = gr.Textbox(
|
154 |
+
label="Prompt",
|
155 |
+
value=""" 'Below is an instruction that describes a task, paired with an input that provides further context. '
|
156 |
+
'Write a response that appropriately completes the request.\n' """,
|
157 |
+
lines=8,
|
158 |
+
)
|
159 |
+
|
160 |
+
with gr.Column(scale=4):
|
161 |
+
source_text = gr.Textbox(
|
162 |
+
label="Source Text",
|
163 |
+
value="LLaMAX is a language model with powerful multilingual capabilities without loss instruction-following capabilities. "+\
|
164 |
+
"LLaMAX supports translation between more than 100 languages, "+\
|
165 |
+
"surpassing the performance of similarly scaled LLMs.",
|
166 |
+
lines=10,
|
167 |
+
)
|
168 |
+
output_text = gr.Textbox(
|
169 |
+
label="Output Text",
|
170 |
+
lines=10,
|
171 |
+
show_copy_button=True,
|
172 |
+
)
|
173 |
+
with gr.Row():
|
174 |
+
submit = gr.Button(value="Submit")
|
175 |
+
clear = gr.ClearButton([source_text, output_text])
|
176 |
+
gr.Markdown(LICENSE)
|
177 |
+
|
178 |
+
source_text.change(lang_detector, source_text, source_lang)
|
179 |
+
submit.click(fn=translate, inputs=[source_text, source_lang, target_lang, inst, prompt, max_length, temperature, top_p, rp], outputs=[output_text])
|
180 |
+
|
181 |
+
|
182 |
+
if __name__ == "__main__":
|
183 |
+
demo.launch()
|