Update app.py
Browse files
app.py
CHANGED
@@ -1,90 +1,124 @@
|
|
1 |
-
|
|
|
|
|
|
|
2 |
import gradio as gr
|
|
|
|
|
|
|
3 |
|
4 |
-
|
5 |
-
|
6 |
-
)
|
7 |
|
8 |
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
prompt += f"[INST] {message} [/INST]"
|
15 |
-
return prompt
|
16 |
|
|
|
|
|
17 |
def generate(
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
top_p =
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
24 |
|
|
|
25 |
generate_kwargs = dict(
|
26 |
-
|
|
|
27 |
max_new_tokens=max_new_tokens,
|
|
|
28 |
top_p=top_p,
|
|
|
|
|
|
|
29 |
repetition_penalty=repetition_penalty,
|
30 |
-
do_sample=True,
|
31 |
-
seed=42,
|
32 |
)
|
|
|
|
|
33 |
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
|
39 |
-
for response in stream:
|
40 |
-
output += response.token.text
|
41 |
-
yield output
|
42 |
-
return output
|
43 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
44 |
|
45 |
-
|
46 |
-
gr.
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
maximum=1.0,
|
51 |
-
step=0.05,
|
52 |
-
interactive=True,
|
53 |
-
info="Higher values produce more diverse outputs",
|
54 |
-
),
|
55 |
-
gr.Slider(
|
56 |
-
label="Max new tokens",
|
57 |
-
value=1000,
|
58 |
-
minimum=0,
|
59 |
-
maximum=1048,
|
60 |
-
step=64,
|
61 |
-
interactive=True,
|
62 |
-
info="The maximum numbers of new tokens",
|
63 |
-
),
|
64 |
-
gr.Slider(
|
65 |
-
label="Top-p (nucleus sampling)",
|
66 |
-
value=0.90,
|
67 |
-
minimum=0.0,
|
68 |
-
maximum=1,
|
69 |
-
step=0.05,
|
70 |
-
interactive=True,
|
71 |
-
info="Higher values sample more low-probability tokens",
|
72 |
-
),
|
73 |
-
gr.Slider(
|
74 |
-
label="Repetition penalty",
|
75 |
-
value=1.2,
|
76 |
-
minimum=1.0,
|
77 |
-
maximum=2.0,
|
78 |
-
step=0.05,
|
79 |
-
interactive=True,
|
80 |
-
info="Penalize repeated tokens",
|
81 |
-
)
|
82 |
-
]
|
83 |
-
|
84 |
|
85 |
-
|
86 |
-
|
87 |
-
chatbot=gr.Chatbot(show_label=False, show_share_button=False, show_copy_button=True, likeable=True, layout="panel"),
|
88 |
-
additional_inputs=additional_inputs,
|
89 |
-
title="""Mistral 7B"""
|
90 |
-
).launch(show_api=False)
|
|
|
1 |
+
import os
|
2 |
+
from threading import Thread
|
3 |
+
from typing import Iterator
|
4 |
+
|
5 |
import gradio as gr
|
6 |
+
import spaces
|
7 |
+
import torch
|
8 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer, TextIteratorStreamer
|
9 |
|
10 |
+
MAX_MAX_NEW_TOKENS = 2048
|
11 |
+
DEFAULT_MAX_NEW_TOKENS = 1024
|
12 |
+
MAX_INPUT_TOKEN_LENGTH = int(os.getenv("MAX_INPUT_TOKEN_LENGTH", "4096"))
|
13 |
|
14 |
|
15 |
+
#if torch.cuda.is_available():
|
16 |
+
model_id = "meta-llama/Llama-2-7b-chat-hf"
|
17 |
+
model = AutoModelForCausalLM.from_pretrained(model_id, torch_dtype=torch.float16, device_map="auto")
|
18 |
+
tokenizer = AutoTokenizer.from_pretrained(model_id)
|
19 |
+
tokenizer.use_default_system_prompt = False
|
|
|
|
|
20 |
|
21 |
+
|
22 |
+
@spaces.GPU
|
23 |
def generate(
|
24 |
+
message: str,
|
25 |
+
chat_history: list[tuple[str, str]],
|
26 |
+
system_prompt: str,
|
27 |
+
max_new_tokens: int = 1024,
|
28 |
+
temperature: float = 0.6,
|
29 |
+
top_p: float = 0.9,
|
30 |
+
top_k: int = 50,
|
31 |
+
repetition_penalty: float = 1.2,
|
32 |
+
) -> Iterator[str]:
|
33 |
+
conversation = []
|
34 |
+
if system_prompt:
|
35 |
+
conversation.append({"role": "system", "content": system_prompt})
|
36 |
+
for user, assistant in chat_history:
|
37 |
+
conversation.extend([{"role": "user", "content": user}, {"role": "assistant", "content": assistant}])
|
38 |
+
conversation.append({"role": "user", "content": message})
|
39 |
+
|
40 |
+
input_ids = tokenizer.apply_chat_template(conversation, return_tensors="pt")
|
41 |
+
if input_ids.shape[1] > MAX_INPUT_TOKEN_LENGTH:
|
42 |
+
input_ids = input_ids[:, -MAX_INPUT_TOKEN_LENGTH:]
|
43 |
+
gr.Warning(f"Trimmed input from conversation as it was longer than {MAX_INPUT_TOKEN_LENGTH} tokens.")
|
44 |
+
input_ids = input_ids.to(model.device)
|
45 |
|
46 |
+
streamer = TextIteratorStreamer(tokenizer, timeout=10.0, skip_prompt=True, skip_special_tokens=True)
|
47 |
generate_kwargs = dict(
|
48 |
+
{"input_ids": input_ids},
|
49 |
+
streamer=streamer,
|
50 |
max_new_tokens=max_new_tokens,
|
51 |
+
do_sample=True,
|
52 |
top_p=top_p,
|
53 |
+
top_k=top_k,
|
54 |
+
temperature=temperature,
|
55 |
+
num_beams=1,
|
56 |
repetition_penalty=repetition_penalty,
|
|
|
|
|
57 |
)
|
58 |
+
t = Thread(target=model.generate, kwargs=generate_kwargs)
|
59 |
+
t.start()
|
60 |
|
61 |
+
outputs = []
|
62 |
+
for text in streamer:
|
63 |
+
outputs.append(text)
|
64 |
+
yield "".join(outputs)
|
65 |
|
|
|
|
|
|
|
|
|
66 |
|
67 |
+
chat_interface = gr.ChatInterface(
|
68 |
+
fn=generate,
|
69 |
+
additional_inputs=[
|
70 |
+
gr.Textbox(label="System prompt", lines=6),
|
71 |
+
gr.Slider(
|
72 |
+
label="Max new tokens",
|
73 |
+
minimum=1,
|
74 |
+
maximum=MAX_MAX_NEW_TOKENS,
|
75 |
+
step=1,
|
76 |
+
value=DEFAULT_MAX_NEW_TOKENS,
|
77 |
+
),
|
78 |
+
gr.Slider(
|
79 |
+
label="Temperature",
|
80 |
+
minimum=0.1,
|
81 |
+
maximum=4.0,
|
82 |
+
step=0.1,
|
83 |
+
value=0.6,
|
84 |
+
),
|
85 |
+
gr.Slider(
|
86 |
+
label="Top-p (nucleus sampling)",
|
87 |
+
minimum=0.05,
|
88 |
+
maximum=1.0,
|
89 |
+
step=0.05,
|
90 |
+
value=0.9,
|
91 |
+
),
|
92 |
+
gr.Slider(
|
93 |
+
label="Top-k",
|
94 |
+
minimum=1,
|
95 |
+
maximum=1000,
|
96 |
+
step=1,
|
97 |
+
value=50,
|
98 |
+
),
|
99 |
+
gr.Slider(
|
100 |
+
label="Repetition penalty",
|
101 |
+
minimum=1.0,
|
102 |
+
maximum=2.0,
|
103 |
+
step=0.05,
|
104 |
+
value=1.2,
|
105 |
+
),
|
106 |
+
],
|
107 |
+
stop_btn=None,
|
108 |
+
examples=[
|
109 |
+
["Hello there! How are you doing?"],
|
110 |
+
["Can you explain briefly to me what is the Python programming language?"],
|
111 |
+
["Explain the plot of Cinderella in a sentence."],
|
112 |
+
["How many hours does it take a man to eat a Helicopter?"],
|
113 |
+
["Write a 100-word article on 'Benefits of Open-Source in AI research'"],
|
114 |
+
],
|
115 |
+
)
|
116 |
|
117 |
+
with gr.Blocks(css="style.css") as demo:
|
118 |
+
gr.Markdown(DESCRIPTION)
|
119 |
+
gr.DuplicateButton(value="Duplicate Space for private use", elem_id="duplicate-button")
|
120 |
+
chat_interface.render()
|
121 |
+
gr.Markdown(LICENSE)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
122 |
|
123 |
+
if __name__ == "__main__":
|
124 |
+
demo.queue(max_size=20).launch()
|
|
|
|
|
|
|
|