Update app.py
Browse files
app.py
CHANGED
@@ -1,50 +1,57 @@
|
|
1 |
import gradio as gr
|
2 |
-
from
|
|
|
|
|
3 |
|
4 |
-
""
|
5 |
-
|
6 |
-
"""
|
7 |
-
client = InferenceClient("HODACHI/EZO-Common-9B-gemma-2-it")
|
8 |
|
|
|
|
|
|
|
|
|
|
|
|
|
9 |
|
10 |
def respond(
|
11 |
message,
|
12 |
history: list[tuple[str, str]],
|
13 |
-
system_message,
|
14 |
max_tokens,
|
15 |
temperature,
|
16 |
top_p,
|
17 |
):
|
18 |
-
|
19 |
-
for
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
messages.append({"role": "assistant", "content": val[1]})
|
24 |
-
|
25 |
-
messages.append({"role": "user", "content": message})
|
26 |
|
27 |
-
|
|
|
28 |
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
|
|
33 |
temperature=temperature,
|
34 |
top_p=top_p,
|
35 |
-
|
36 |
-
|
|
|
|
|
|
|
|
|
37 |
|
38 |
-
|
|
|
|
|
39 |
yield response
|
40 |
|
41 |
-
"""
|
42 |
-
For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
|
43 |
-
"""
|
44 |
demo = gr.ChatInterface(
|
45 |
respond,
|
46 |
additional_inputs=[
|
47 |
-
gr.Slider(minimum=1, maximum=2048, value=
|
48 |
gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
|
49 |
gr.Slider(
|
50 |
minimum=0.1,
|
@@ -56,6 +63,5 @@ demo = gr.ChatInterface(
|
|
56 |
],
|
57 |
)
|
58 |
|
59 |
-
|
60 |
if __name__ == "__main__":
|
61 |
demo.launch()
|
|
|
1 |
import gradio as gr
|
2 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM, TextIteratorStreamer
|
3 |
+
import torch
|
4 |
+
from threading import Thread
|
5 |
|
6 |
+
MODEL_ID = "HODACHI/EZO-Common-9B-gemma-2-it"
|
7 |
+
DTYPE = torch.bfloat16
|
|
|
|
|
8 |
|
9 |
+
tokenizer = AutoTokenizer.from_pretrained(MODEL_ID)
|
10 |
+
model = AutoModelForCausalLM.from_pretrained(
|
11 |
+
MODEL_ID,
|
12 |
+
device_map="cuda",
|
13 |
+
torch_dtype=DTYPE,
|
14 |
+
)
|
15 |
|
16 |
def respond(
|
17 |
message,
|
18 |
history: list[tuple[str, str]],
|
|
|
19 |
max_tokens,
|
20 |
temperature,
|
21 |
top_p,
|
22 |
):
|
23 |
+
chat = []
|
24 |
+
for user, assistant in history:
|
25 |
+
chat.append({"role": "user", "content": user})
|
26 |
+
chat.append({"role": "assistant", "content": assistant})
|
27 |
+
chat.append({"role": "user", "content": message})
|
|
|
|
|
|
|
28 |
|
29 |
+
prompt = tokenizer.apply_chat_template(chat, tokenize=False, add_generation_prompt=True)
|
30 |
+
inputs = tokenizer.encode(prompt, add_special_tokens=False, return_tensors="pt").to(model.device)
|
31 |
|
32 |
+
streamer = TextIteratorStreamer(tokenizer, skip_prompt=True, skip_special_tokens=True)
|
33 |
+
|
34 |
+
generation_kwargs = dict(
|
35 |
+
input_ids=inputs,
|
36 |
+
max_new_tokens=max_tokens,
|
37 |
temperature=temperature,
|
38 |
top_p=top_p,
|
39 |
+
do_sample=True,
|
40 |
+
streamer=streamer,
|
41 |
+
)
|
42 |
+
|
43 |
+
thread = Thread(target=model.generate, kwargs=generation_kwargs)
|
44 |
+
thread.start()
|
45 |
|
46 |
+
response = ""
|
47 |
+
for new_text in streamer:
|
48 |
+
response += new_text
|
49 |
yield response
|
50 |
|
|
|
|
|
|
|
51 |
demo = gr.ChatInterface(
|
52 |
respond,
|
53 |
additional_inputs=[
|
54 |
+
gr.Slider(minimum=1, maximum=2048, value=150, step=1, label="Max new tokens"),
|
55 |
gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
|
56 |
gr.Slider(
|
57 |
minimum=0.1,
|
|
|
63 |
],
|
64 |
)
|
65 |
|
|
|
66 |
if __name__ == "__main__":
|
67 |
demo.launch()
|