Spaces:
Sleeping
Sleeping
dreamerdeo
commited on
Commit
•
89dd80e
1
Parent(s):
49c7d6f
Update app.py
Browse files
app.py
CHANGED
@@ -1,63 +1,103 @@
|
|
|
|
1 |
import gradio as gr
|
2 |
-
|
|
|
|
|
3 |
|
4 |
-
|
5 |
-
|
6 |
-
|
7 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
8 |
|
9 |
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
system_message,
|
14 |
-
max_tokens,
|
15 |
-
temperature,
|
16 |
-
top_p,
|
17 |
-
):
|
18 |
-
messages = [{"role": "system", "content": system_message}]
|
19 |
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
if val[1]:
|
24 |
-
messages.append({"role": "assistant", "content": val[1]})
|
25 |
|
26 |
-
|
|
|
|
|
|
|
|
|
27 |
|
28 |
-
|
29 |
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
top_p=top_p,
|
36 |
-
):
|
37 |
-
token = message.choices[0].delta.content
|
38 |
|
39 |
-
|
40 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
41 |
|
|
|
|
|
|
|
|
|
|
|
42 |
"""
|
43 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
44 |
"""
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
maximum=1.0,
|
54 |
-
value=0.95,
|
55 |
-
step=0.05,
|
56 |
-
label="Top-p (nucleus sampling)",
|
57 |
-
),
|
58 |
-
],
|
59 |
-
)
|
60 |
-
|
61 |
-
|
62 |
-
if __name__ == "__main__":
|
63 |
-
demo.launch()
|
|
|
1 |
+
import spaces
|
2 |
import gradio as gr
|
3 |
+
import torch
|
4 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer, StoppingCriteria, StoppingCriteriaList, TextIteratorStreamer
|
5 |
+
from threading import Thread
|
6 |
|
7 |
+
model_path = 'sail/Sailor-14B-Chat'
|
8 |
+
|
9 |
+
# Loading the tokenizer and model from Hugging Face's model hub.
|
10 |
+
tokenizer = AutoTokenizer.from_pretrained(model_path, trust_remote_code=True)
|
11 |
+
model = AutoModelForCausalLM.from_pretrained(model_path, trust_remote_code=True)
|
12 |
+
|
13 |
+
# using CUDA for an optimal experience
|
14 |
+
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
|
15 |
+
model = model.to(device)
|
16 |
+
|
17 |
+
# Defining a custom stopping criteria class for the model's text generation.
|
18 |
+
class StopOnTokens(StoppingCriteria):
|
19 |
+
def __call__(self, input_ids: torch.LongTensor, scores: torch.FloatTensor, **kwargs) -> bool:
|
20 |
+
stop_ids = [151645] # IDs of tokens where the generation should stop.
|
21 |
+
for stop_id in stop_ids:
|
22 |
+
if input_ids[0][-1] == stop_id: # Checking if the last generated token is a stop token.
|
23 |
+
return True
|
24 |
+
return False
|
25 |
|
26 |
|
27 |
+
system_role= 'system'
|
28 |
+
user_role = 'assistant'
|
29 |
+
assistant_role = "user"
|
|
|
|
|
|
|
|
|
|
|
|
|
30 |
|
31 |
+
sft_start_token = "<|im_start|>"
|
32 |
+
sft_end_token = "<|im_end|>"
|
33 |
+
ct_end_token = "<|endoftext|>"
|
|
|
|
|
34 |
|
35 |
+
system_prompt= \
|
36 |
+
'You are an AI assistant named Sailor created by Sea AI Lab. \
|
37 |
+
As an AI assistant, you need to answer a series of questions next, which may include languages such as English, Chinese, Thai, Vietnamese, Indonesian, Malay, and so on. \
|
38 |
+
Your answer should be friendly, unbiased, faithful, informative and detailed.'
|
39 |
+
system_prompt = f"<|im_start|>{system_role}\n{system_prompt}<|im_end|>"
|
40 |
|
41 |
+
# Function to generate model predictions.
|
42 |
|
43 |
+
@spaces.GPU()
|
44 |
+
def predict(message, history):
|
45 |
+
# history = []
|
46 |
+
history_transformer_format = history + [[message, ""]]
|
47 |
+
stop = StopOnTokens()
|
|
|
|
|
|
|
48 |
|
49 |
+
# Formatting the input for the model.
|
50 |
+
messages = system_prompt + sft_end_token.join([sft_end_token.join([f"\n{sft_start_token}{user_role}\n" + item[0], f"\n{sft_start_token}{assistant_role}\n" + item[1]])
|
51 |
+
for item in history_transformer_format])
|
52 |
+
model_inputs = tokenizer([messages], return_tensors="pt").to(device)
|
53 |
+
streamer = TextIteratorStreamer(tokenizer, timeout=10., skip_prompt=True, skip_special_tokens=True)
|
54 |
+
generate_kwargs = dict(
|
55 |
+
model_inputs,
|
56 |
+
streamer=streamer,
|
57 |
+
max_new_tokens=256,
|
58 |
+
do_sample=True,
|
59 |
+
top_p= 0.75,
|
60 |
+
top_k= 60,
|
61 |
+
temperature=0.2,
|
62 |
+
num_beams=1,
|
63 |
+
stopping_criteria=StoppingCriteriaList([stop]),
|
64 |
+
repetition_penalty=1.1,
|
65 |
+
)
|
66 |
+
t = Thread(target=model.generate, kwargs=generate_kwargs)
|
67 |
+
t.start() # Starting the generation in a separate thread.
|
68 |
+
partial_message = ""
|
69 |
+
for new_token in streamer:
|
70 |
+
partial_message += new_token
|
71 |
+
if sft_end_token in partial_message: # Breaking the loop if the stop token is generated.
|
72 |
+
break
|
73 |
+
yield partial_message
|
74 |
|
75 |
+
|
76 |
+
css = """
|
77 |
+
full-height {
|
78 |
+
height: 100%;
|
79 |
+
}
|
80 |
"""
|
81 |
+
|
82 |
+
prompt_examples = [
|
83 |
+
'How to cook a fish?',
|
84 |
+
'Cara memanggang ikan',
|
85 |
+
'วิธีย่างปลา',
|
86 |
+
'Cách nướng cá'
|
87 |
+
]
|
88 |
+
|
89 |
+
placeholder = """
|
90 |
+
<div style="opacity: 0.5;">
|
91 |
+
<img src="https://raw.githubusercontent.com/sail-sg/sailor-llm/main/misc/banner.jpg" style="width:30%;">
|
92 |
+
<br>Sailor models are designed to understand and generate text across diverse linguistic landscapes of these SEA regions:
|
93 |
+
<br>🇮🇩Indonesian, 🇹🇭Thai, 🇻🇳Vietnamese, 🇲🇾Malay, and 🇱🇦Lao.
|
94 |
+
</div>
|
95 |
"""
|
96 |
+
|
97 |
+
chatbot = gr.Chatbot(label='Sailor', placeholder=placeholder)
|
98 |
+
with gr.Blocks(theme=gr.themes.Soft(), fill_height=True) as demo:
|
99 |
+
# gr.Markdown("""<center><font size=8>Sailor-Chat Bot⚓</center>""")
|
100 |
+
gr.Markdown("""<p align="center"><img src="https://github.com/sail-sg/sailor-llm/raw/main/misc/wide_sailor_banner.jpg" style="height: 110px"/><p>""")
|
101 |
+
gr.ChatInterface(predict, chatbot=chatbot, fill_height=True, examples=prompt_examples, css=css)
|
102 |
+
|
103 |
+
demo.launch() # Launching the web interface.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|