Update app.py
Browse files
app.py
CHANGED
@@ -1,62 +1,151 @@
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import gradio as gr
|
2 |
-
from
|
3 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
4 |
"""
|
5 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
6 |
"""
|
7 |
-
client = InferenceClient("AnatoliiPotapov/T-lite-instruct-0.1")
|
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 |
-
gr.
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
60 |
|
61 |
|
62 |
if __name__ == "__main__":
|
|
|
1 |
+
import os
|
2 |
+
import time
|
3 |
+
import spaces
|
4 |
+
import torch
|
5 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer, TextIteratorStreamer
|
6 |
import gradio as gr
|
7 |
+
from threading import Thread
|
8 |
|
9 |
+
MODEL_LIST = ["AnatoliiPotapov/T-lite-instruct-0.1"]
|
10 |
+
|
11 |
+
MODEL = MODEL_LIST[0]
|
12 |
+
TITLE = "<h1><center>T-lite</center></h1>"
|
13 |
+
|
14 |
+
PLACEHOLDER = """
|
15 |
+
<center>
|
16 |
+
<p>T-lite model by T-Bank.</p>
|
17 |
+
</center>
|
18 |
"""
|
19 |
+
|
20 |
+
|
21 |
+
CSS = """
|
22 |
+
.duplicate-button {
|
23 |
+
margin: auto !important;
|
24 |
+
color: white !important;
|
25 |
+
background: black !important;
|
26 |
+
border-radius: 100vh !important;
|
27 |
+
}
|
28 |
+
h3 {
|
29 |
+
text-align: center;
|
30 |
+
}
|
31 |
"""
|
|
|
32 |
|
33 |
+
device = "cuda" # for GPU usage or "cpu" for CPU usage
|
34 |
|
35 |
+
tokenizer = AutoTokenizer.from_pretrained(MODEL)
|
36 |
+
model = AutoModelForCausalLM.from_pretrained(
|
37 |
+
MODEL,
|
38 |
+
torch_dtype=torch.bfloat16,
|
39 |
+
device_map="auto",
|
40 |
+
ignore_mismatched_sizes=True)
|
41 |
+
|
42 |
+
@spaces.GPU()
|
43 |
+
def stream_chat(
|
44 |
+
message: str,
|
45 |
+
history: list,
|
46 |
+
temperature: float = 0.3,
|
47 |
+
max_new_tokens: int = 1024,
|
48 |
+
top_p: float = 1.0,
|
49 |
+
top_k: int = 20,
|
50 |
+
penalty: float = 1.2,
|
51 |
):
|
52 |
+
print(f'message: {message}')
|
53 |
+
print(f'history: {history}')
|
54 |
|
55 |
+
conversation = []
|
56 |
+
for prompt, answer in history:
|
57 |
+
conversation.extend([
|
58 |
+
{"role": "user", "content": prompt},
|
59 |
+
{"role": "assistant", "content": answer},
|
60 |
+
])
|
61 |
|
62 |
+
conversation.append({"role": "user", "content": message})
|
63 |
|
64 |
+
input_text=tokenizer.apply_chat_template(conversation, tokenize=False)
|
65 |
+
inputs = tokenizer.encode(input_text, return_tensors="pt").to(device)
|
66 |
+
streamer = TextIteratorStreamer(tokenizer, timeout=60.0, skip_prompt=True, skip_special_tokens=True)
|
67 |
+
|
68 |
+
generate_kwargs = dict(
|
69 |
+
input_ids=inputs,
|
70 |
+
max_new_tokens = max_new_tokens,
|
71 |
+
do_sample = False if temperature == 0 else True,
|
72 |
+
top_p = top_p,
|
73 |
+
top_k = top_k,
|
74 |
+
temperature = temperature,
|
75 |
+
streamer=streamer,
|
76 |
+
pad_token_id = 10,
|
77 |
+
)
|
78 |
|
79 |
+
with torch.no_grad():
|
80 |
+
thread = Thread(target=model.generate, kwargs=generate_kwargs)
|
81 |
+
thread.start()
|
82 |
+
|
83 |
+
buffer = ""
|
84 |
+
for new_text in streamer:
|
85 |
+
buffer += new_text
|
86 |
+
yield buffer
|
87 |
|
88 |
+
|
89 |
+
chatbot = gr.Chatbot(height=600, placeholder=PLACEHOLDER)
|
90 |
|
91 |
+
with gr.Blocks(css=CSS, theme="soft") as demo:
|
92 |
+
gr.HTML(TITLE)
|
93 |
+
gr.DuplicateButton(value="Duplicate Space for private use", elem_classes="duplicate-button")
|
94 |
+
gr.ChatInterface(
|
95 |
+
fn=stream_chat,
|
96 |
+
chatbot=chatbot,
|
97 |
+
fill_height=True,
|
98 |
+
additional_inputs_accordion=gr.Accordion(label="⚙️ Parameters", open=False, render=False),
|
99 |
+
additional_inputs=[
|
100 |
+
gr.Slider(
|
101 |
+
minimum=0,
|
102 |
+
maximum=1,
|
103 |
+
step=0.1,
|
104 |
+
value=0.3,
|
105 |
+
label="Temperature",
|
106 |
+
render=False,
|
107 |
+
),
|
108 |
+
gr.Slider(
|
109 |
+
minimum=128,
|
110 |
+
maximum=8192,
|
111 |
+
step=1,
|
112 |
+
value=1024,
|
113 |
+
label="Max new tokens",
|
114 |
+
render=False,
|
115 |
+
),
|
116 |
+
gr.Slider(
|
117 |
+
minimum=0.0,
|
118 |
+
maximum=1.0,
|
119 |
+
step=0.1,
|
120 |
+
value=1.0,
|
121 |
+
label="top_p",
|
122 |
+
render=False,
|
123 |
+
),
|
124 |
+
gr.Slider(
|
125 |
+
minimum=1,
|
126 |
+
maximum=20,
|
127 |
+
step=1,
|
128 |
+
value=20,
|
129 |
+
label="top_k",
|
130 |
+
render=False,
|
131 |
+
),
|
132 |
+
gr.Slider(
|
133 |
+
minimum=0.0,
|
134 |
+
maximum=2.0,
|
135 |
+
step=0.1,
|
136 |
+
value=1.2,
|
137 |
+
label="Repetition penalty",
|
138 |
+
render=False,
|
139 |
+
),
|
140 |
+
],
|
141 |
+
examples=[
|
142 |
+
["Help me study vocabulary: write a sentence for me to fill in the blank, and I'll try to pick the correct option."],
|
143 |
+
["What are 5 creative things I could do with my kids' art? I don't want to throw them away, but it's also so much clutter."],
|
144 |
+
["Tell me a random fun fact about the Roman Empire."],
|
145 |
+
["Show me a code snippet of a website's sticky header in CSS and JavaScript."],
|
146 |
+
],
|
147 |
+
cache_examples=False,
|
148 |
+
)
|
149 |
|
150 |
|
151 |
if __name__ == "__main__":
|