Spaces:
Running
on
Zero
Running
on
Zero
Update app.py
Browse files
app.py
CHANGED
@@ -8,30 +8,26 @@ import torch
|
|
8 |
from transformers import (
|
9 |
AutoModelForCausalLM,
|
10 |
BitsAndBytesConfig,
|
11 |
-
|
12 |
TextIteratorStreamer,
|
13 |
)
|
14 |
|
15 |
DESCRIPTION = """\
|
16 |
-
#
|
17 |
-
|
|
|
|
|
|
|
18 |
"""
|
19 |
|
20 |
-
|
21 |
-
|
|
|
22 |
|
23 |
-
|
24 |
-
{Question}
|
25 |
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
MAX_MAX_NEW_TOKENS = 4096
|
30 |
-
DEFAULT_MAX_NEW_TOKENS = 4096
|
31 |
-
MAX_INPUT_TOKEN_LENGTH = 2048
|
32 |
-
|
33 |
-
model_id = "CardinalOperations/ORLM-LLaMA-3-8B"
|
34 |
-
tokenizer = AutoTokenizer.from_pretrained(model_id, use_fast=True)
|
35 |
model = AutoModelForCausalLM.from_pretrained(
|
36 |
model_id,
|
37 |
device_map="auto",
|
@@ -41,7 +37,7 @@ model.config.sliding_window = 4096
|
|
41 |
model.eval()
|
42 |
|
43 |
|
44 |
-
@spaces.GPU(duration=
|
45 |
def generate(
|
46 |
message: str,
|
47 |
chat_history: list[tuple[str, str]],
|
@@ -51,12 +47,20 @@ def generate(
|
|
51 |
top_k: int = 50,
|
52 |
repetition_penalty: float = 1.2,
|
53 |
) -> Iterator[str]:
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
60 |
input_ids = input_ids.to(model.device)
|
61 |
|
62 |
streamer = TextIteratorStreamer(tokenizer, timeout=20.0, skip_prompt=True, skip_special_tokens=True)
|
@@ -64,13 +68,12 @@ def generate(
|
|
64 |
{"input_ids": input_ids},
|
65 |
streamer=streamer,
|
66 |
max_new_tokens=max_new_tokens,
|
67 |
-
do_sample=
|
68 |
top_p=top_p,
|
69 |
top_k=top_k,
|
70 |
temperature=temperature,
|
71 |
num_beams=1,
|
72 |
repetition_penalty=repetition_penalty,
|
73 |
-
eos_token_id=[tok.eos_token_id],
|
74 |
)
|
75 |
t = Thread(target=model.generate, kwargs=generate_kwargs)
|
76 |
t.start()
|
@@ -79,9 +82,6 @@ def generate(
|
|
79 |
for text in streamer:
|
80 |
outputs.append(text)
|
81 |
yield "".join(outputs)
|
82 |
-
|
83 |
-
# outputs.append("\n\nI have now attempted to solve the optimization modeling task! Please try executing the code in your environment, making sure it is equipped with `coptpy`.")
|
84 |
-
# yield "".join(outputs)
|
85 |
|
86 |
|
87 |
chat_interface = gr.ChatInterface(
|
@@ -96,44 +96,46 @@ chat_interface = gr.ChatInterface(
|
|
96 |
),
|
97 |
gr.Slider(
|
98 |
label="Temperature",
|
99 |
-
minimum=0.
|
100 |
maximum=4.0,
|
101 |
step=0.1,
|
102 |
-
value=0.
|
103 |
),
|
104 |
gr.Slider(
|
105 |
label="Top-p (nucleus sampling)",
|
106 |
minimum=0.05,
|
107 |
maximum=1.0,
|
108 |
step=0.05,
|
109 |
-
value=
|
110 |
),
|
111 |
gr.Slider(
|
112 |
label="Top-k",
|
113 |
minimum=1,
|
114 |
maximum=1000,
|
115 |
step=1,
|
116 |
-
value=
|
117 |
),
|
118 |
gr.Slider(
|
119 |
label="Repetition penalty",
|
120 |
minimum=1.0,
|
121 |
maximum=2.0,
|
122 |
step=0.05,
|
123 |
-
value=1.
|
124 |
),
|
125 |
],
|
126 |
stop_btn=None,
|
127 |
examples=[
|
128 |
-
["
|
129 |
-
["
|
130 |
-
["
|
|
|
|
|
131 |
],
|
132 |
)
|
133 |
|
134 |
with gr.Blocks(css="style.css", fill_height=True) as demo:
|
135 |
gr.Markdown(DESCRIPTION)
|
136 |
-
|
137 |
chat_interface.render()
|
138 |
|
139 |
if __name__ == "__main__":
|
|
|
8 |
from transformers import (
|
9 |
AutoModelForCausalLM,
|
10 |
BitsAndBytesConfig,
|
11 |
+
GemmaTokenizerFast,
|
12 |
TextIteratorStreamer,
|
13 |
)
|
14 |
|
15 |
DESCRIPTION = """\
|
16 |
+
# Gemma 2 9B IT
|
17 |
+
Gemma 2 is Google's latest iteration of open LLMs.
|
18 |
+
This is a demo of [`google/gemma-2-9b-it`](https://huggingface.co/google/gemma-2-9b-it), fine-tuned for instruction following.
|
19 |
+
For more details, please check [our post](https://huggingface.co/blog/gemma2).
|
20 |
+
👉 Looking for a larger and more powerful version? Try the 27B version in [HuggingChat](https://huggingface.co/chat/models/google/gemma-2-27b-it).
|
21 |
"""
|
22 |
|
23 |
+
MAX_MAX_NEW_TOKENS = 2048
|
24 |
+
DEFAULT_MAX_NEW_TOKENS = 1024
|
25 |
+
MAX_INPUT_TOKEN_LENGTH = int(os.getenv("MAX_INPUT_TOKEN_LENGTH", "4096"))
|
26 |
|
27 |
+
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
|
|
|
28 |
|
29 |
+
model_id = "google/gemma-2-9b-it"
|
30 |
+
tokenizer = GemmaTokenizerFast.from_pretrained(model_id)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
31 |
model = AutoModelForCausalLM.from_pretrained(
|
32 |
model_id,
|
33 |
device_map="auto",
|
|
|
37 |
model.eval()
|
38 |
|
39 |
|
40 |
+
@spaces.GPU(duration=90)
|
41 |
def generate(
|
42 |
message: str,
|
43 |
chat_history: list[tuple[str, str]],
|
|
|
47 |
top_k: int = 50,
|
48 |
repetition_penalty: float = 1.2,
|
49 |
) -> Iterator[str]:
|
50 |
+
conversation = []
|
51 |
+
for user, assistant in chat_history:
|
52 |
+
conversation.extend(
|
53 |
+
[
|
54 |
+
{"role": "user", "content": user},
|
55 |
+
{"role": "assistant", "content": assistant},
|
56 |
+
]
|
57 |
+
)
|
58 |
+
conversation.append({"role": "user", "content": message})
|
59 |
+
|
60 |
+
input_ids = tokenizer.apply_chat_template(conversation, add_generation_prompt=True, return_tensors="pt")
|
61 |
+
if input_ids.shape[1] > MAX_INPUT_TOKEN_LENGTH:
|
62 |
+
input_ids = input_ids[:, -MAX_INPUT_TOKEN_LENGTH:]
|
63 |
+
gr.Warning(f"Trimmed input from conversation as it was longer than {MAX_INPUT_TOKEN_LENGTH} tokens.")
|
64 |
input_ids = input_ids.to(model.device)
|
65 |
|
66 |
streamer = TextIteratorStreamer(tokenizer, timeout=20.0, skip_prompt=True, skip_special_tokens=True)
|
|
|
68 |
{"input_ids": input_ids},
|
69 |
streamer=streamer,
|
70 |
max_new_tokens=max_new_tokens,
|
71 |
+
do_sample=True,
|
72 |
top_p=top_p,
|
73 |
top_k=top_k,
|
74 |
temperature=temperature,
|
75 |
num_beams=1,
|
76 |
repetition_penalty=repetition_penalty,
|
|
|
77 |
)
|
78 |
t = Thread(target=model.generate, kwargs=generate_kwargs)
|
79 |
t.start()
|
|
|
82 |
for text in streamer:
|
83 |
outputs.append(text)
|
84 |
yield "".join(outputs)
|
|
|
|
|
|
|
85 |
|
86 |
|
87 |
chat_interface = gr.ChatInterface(
|
|
|
96 |
),
|
97 |
gr.Slider(
|
98 |
label="Temperature",
|
99 |
+
minimum=0.1,
|
100 |
maximum=4.0,
|
101 |
step=0.1,
|
102 |
+
value=0.6,
|
103 |
),
|
104 |
gr.Slider(
|
105 |
label="Top-p (nucleus sampling)",
|
106 |
minimum=0.05,
|
107 |
maximum=1.0,
|
108 |
step=0.05,
|
109 |
+
value=0.9,
|
110 |
),
|
111 |
gr.Slider(
|
112 |
label="Top-k",
|
113 |
minimum=1,
|
114 |
maximum=1000,
|
115 |
step=1,
|
116 |
+
value=50,
|
117 |
),
|
118 |
gr.Slider(
|
119 |
label="Repetition penalty",
|
120 |
minimum=1.0,
|
121 |
maximum=2.0,
|
122 |
step=0.05,
|
123 |
+
value=1.2,
|
124 |
),
|
125 |
],
|
126 |
stop_btn=None,
|
127 |
examples=[
|
128 |
+
["Hello there! How are you doing?"],
|
129 |
+
["Can you explain briefly to me what is the Python programming language?"],
|
130 |
+
["Explain the plot of Cinderella in a sentence."],
|
131 |
+
["How many hours does it take a man to eat a Helicopter?"],
|
132 |
+
["Write a 100-word article on 'Benefits of Open-Source in AI research'"],
|
133 |
],
|
134 |
)
|
135 |
|
136 |
with gr.Blocks(css="style.css", fill_height=True) as demo:
|
137 |
gr.Markdown(DESCRIPTION)
|
138 |
+
gr.DuplicateButton(value="Duplicate Space for private use", elem_id="duplicate-button")
|
139 |
chat_interface.render()
|
140 |
|
141 |
if __name__ == "__main__":
|