Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,64 +1,117 @@
|
|
|
|
|
|
|
|
|
|
1 |
import gradio as gr
|
2 |
-
|
|
|
|
|
3 |
|
|
|
|
|
|
|
|
|
|
|
|
|
4 |
"""
|
5 |
-
For more information on `huggingface_hub` Inference API support, please check the docs: https://huggingface.co/docs/huggingface_hub/v0.22.2/en/guides/inference
|
6 |
-
"""
|
7 |
-
client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
|
8 |
|
|
|
|
|
|
|
9 |
|
10 |
-
|
11 |
-
message,
|
12 |
-
history: list[tuple[str, str]],
|
13 |
-
system_message,
|
14 |
-
max_tokens,
|
15 |
-
temperature,
|
16 |
-
top_p,
|
17 |
-
):
|
18 |
-
messages = [{"role": "system", "content": system_message}]
|
19 |
|
20 |
-
|
21 |
-
|
22 |
-
messages.append({"role": "user", "content": val[0]})
|
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 |
-
|
|
|
|
|
|
|
|
|
|
|
38 |
|
39 |
-
|
40 |
-
|
|
|
|
|
41 |
|
42 |
|
43 |
-
"""
|
44 |
-
For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
|
45 |
-
"""
|
46 |
demo = gr.ChatInterface(
|
47 |
-
|
48 |
additional_inputs=[
|
49 |
-
gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
|
50 |
-
gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
|
51 |
-
gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
|
52 |
gr.Slider(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
53 |
minimum=0.1,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
54 |
maximum=1.0,
|
55 |
-
value=0.95,
|
56 |
step=0.05,
|
57 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
58 |
),
|
59 |
],
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
60 |
)
|
61 |
|
62 |
-
|
63 |
if __name__ == "__main__":
|
64 |
-
demo.launch()
|
|
|
1 |
+
import os
|
2 |
+
from collections.abc import Iterator
|
3 |
+
from threading import Thread
|
4 |
+
|
5 |
import gradio as gr
|
6 |
+
import spaces
|
7 |
+
import torch
|
8 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer, TextIteratorStreamer
|
9 |
|
10 |
+
DESCRIPTION = """\
|
11 |
+
# Llama 3.2 3B Instruct
|
12 |
+
|
13 |
+
Llama 3.2 3B is Meta's latest iteration of open LLMs.
|
14 |
+
This is a demo of [`meta-llama/Llama-3.2-3B-Instruct`](https://huggingface.co/meta-llama/Llama-3.2-3B-Instruct), fine-tuned for instruction following.
|
15 |
+
For more details, please check [our post](https://huggingface.co/blog/llama32).
|
16 |
"""
|
|
|
|
|
|
|
17 |
|
18 |
+
MAX_MAX_NEW_TOKENS = 2048
|
19 |
+
DEFAULT_MAX_NEW_TOKENS = 1024
|
20 |
+
MAX_INPUT_TOKEN_LENGTH = 32000
|
21 |
|
22 |
+
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
23 |
|
24 |
+
tokenizer = AutoTokenizer.from_pretrained("evabyte/EvaByte-SFT", trust_remote_code=True)
|
25 |
+
model = AutoModelForCausalLM.from_pretrained("evabyte/EvaByte-SFT", torch_dtype=torch.bfloat16, trust_remote_code=True).eval().to("cuda")
|
|
|
|
|
|
|
26 |
|
27 |
+
@spaces.GPU(duration=120)
|
28 |
+
def generate(
|
29 |
+
message: str,
|
30 |
+
chat_history: list[dict],
|
31 |
+
max_new_tokens: int = 1024,
|
32 |
+
temperature: float = 0.6,
|
33 |
+
top_p: float = 0.9,
|
34 |
+
top_k: int = 50,
|
35 |
+
repetition_penalty: float = 1.2,
|
36 |
+
) -> Iterator[str]:
|
37 |
+
conversation = [*chat_history, {"role": "user", "content": message}]
|
38 |
|
39 |
+
input_ids = tokenizer.apply_chat_template(conversation, add_generation_prompt=True, return_tensors="pt")
|
40 |
+
if input_ids.shape[1] > MAX_INPUT_TOKEN_LENGTH:
|
41 |
+
input_ids = input_ids[:, -MAX_INPUT_TOKEN_LENGTH:]
|
42 |
+
gr.Warning(f"Trimmed input from conversation as it was longer than {MAX_INPUT_TOKEN_LENGTH} tokens.")
|
43 |
+
input_ids = input_ids.to(model.device)
|
44 |
|
45 |
+
streamer = TextIteratorStreamer(tokenizer, timeout=20.0, skip_prompt=True, skip_special_tokens=True)
|
46 |
+
generate_kwargs = dict(
|
47 |
+
{"input_ids": input_ids},
|
48 |
+
streamer=streamer,
|
49 |
+
max_new_tokens=max_new_tokens,
|
50 |
+
do_sample=True,
|
51 |
top_p=top_p,
|
52 |
+
top_k=top_k,
|
53 |
+
temperature=temperature,
|
54 |
+
num_beams=1,
|
55 |
+
repetition_penalty=repetition_penalty,
|
56 |
+
)
|
57 |
+
t = Thread(target=model.generate, kwargs=generate_kwargs)
|
58 |
+
t.start()
|
59 |
|
60 |
+
outputs = []
|
61 |
+
for text in streamer:
|
62 |
+
outputs.append(text)
|
63 |
+
yield "".join(outputs)
|
64 |
|
65 |
|
|
|
|
|
|
|
66 |
demo = gr.ChatInterface(
|
67 |
+
fn=generate,
|
68 |
additional_inputs=[
|
|
|
|
|
|
|
69 |
gr.Slider(
|
70 |
+
label="Max new tokens",
|
71 |
+
minimum=1,
|
72 |
+
maximum=MAX_MAX_NEW_TOKENS,
|
73 |
+
step=1,
|
74 |
+
value=DEFAULT_MAX_NEW_TOKENS,
|
75 |
+
),
|
76 |
+
gr.Slider(
|
77 |
+
label="Temperature",
|
78 |
minimum=0.1,
|
79 |
+
maximum=4.0,
|
80 |
+
step=0.1,
|
81 |
+
value=0.6,
|
82 |
+
),
|
83 |
+
gr.Slider(
|
84 |
+
label="Top-p (nucleus sampling)",
|
85 |
+
minimum=0.05,
|
86 |
maximum=1.0,
|
|
|
87 |
step=0.05,
|
88 |
+
value=0.9,
|
89 |
+
),
|
90 |
+
gr.Slider(
|
91 |
+
label="Top-k",
|
92 |
+
minimum=1,
|
93 |
+
maximum=1000,
|
94 |
+
step=1,
|
95 |
+
value=50,
|
96 |
+
),
|
97 |
+
gr.Slider(
|
98 |
+
label="Repetition penalty",
|
99 |
+
minimum=1.0,
|
100 |
+
maximum=2.0,
|
101 |
+
step=0.05,
|
102 |
+
value=1.2,
|
103 |
),
|
104 |
],
|
105 |
+
stop_btn=None,
|
106 |
+
examples=[
|
107 |
+
["Write me an English pangram."],
|
108 |
+
],
|
109 |
+
cache_examples=False,
|
110 |
+
type="messages",
|
111 |
+
description=DESCRIPTION,
|
112 |
+
css_paths="style.css",
|
113 |
+
fill_height=True,
|
114 |
)
|
115 |
|
|
|
116 |
if __name__ == "__main__":
|
117 |
+
demo.queue(max_size=20).launch()
|