Commit
·
543fe4a
1
Parent(s):
901bbd9
do the loop
Browse files
app.py
CHANGED
@@ -1,64 +1,19 @@
|
|
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 |
-
def respond(
|
11 |
-
|
12 |
-
|
13 |
-
system_message,
|
14 |
-
max_tokens,
|
15 |
-
temperature,
|
16 |
-
top_p,
|
17 |
-
):
|
18 |
-
messages = [{"role": "system", "content": system_message}]
|
19 |
|
20 |
-
|
21 |
-
if val[0]:
|
22 |
-
messages.append({"role": "user", "content": val[0]})
|
23 |
-
if val[1]:
|
24 |
-
messages.append({"role": "assistant", "content": val[1]})
|
25 |
-
|
26 |
-
messages.append({"role": "user", "content": message})
|
27 |
-
|
28 |
-
response = ""
|
29 |
-
|
30 |
-
for message in client.chat_completion(
|
31 |
-
messages,
|
32 |
-
max_tokens=max_tokens,
|
33 |
-
stream=True,
|
34 |
-
temperature=temperature,
|
35 |
-
top_p=top_p,
|
36 |
-
):
|
37 |
-
token = message.choices[0].delta.content
|
38 |
-
|
39 |
-
response += token
|
40 |
-
yield response
|
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 |
respond,
|
48 |
-
|
49 |
-
|
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 |
-
label="Top-p (nucleus sampling)",
|
58 |
-
),
|
59 |
-
],
|
60 |
)
|
61 |
|
62 |
-
|
63 |
if __name__ == "__main__":
|
64 |
demo.launch()
|
|
|
1 |
import gradio as gr
|
2 |
+
import loop
|
3 |
|
4 |
+
MODEL_ID = "TinyLlama/TinyLlama_v1.1_math_code"
|
|
|
|
|
|
|
5 |
|
6 |
+
handler = loop.EndpointHandler(MODEL_ID)
|
7 |
|
8 |
+
def respond(prompt, grammar):
|
9 |
+
args = { "inputs": prompt, "grammar": grammar }
|
10 |
+
return handler(prompt)
|
|
|
|
|
|
|
|
|
|
|
|
|
11 |
|
12 |
+
demo = gr.Interface(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
13 |
respond,
|
14 |
+
inputs=["text", "text"],
|
15 |
+
outputs=["text"]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
16 |
)
|
17 |
|
|
|
18 |
if __name__ == "__main__":
|
19 |
demo.launch()
|
loop.py
ADDED
@@ -0,0 +1,51 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from typing import Dict, List, Any
|
2 |
+
|
3 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM
|
4 |
+
from transformers.generation.logits_process import LogitsProcessorList, InfNanRemoveLogitsProcessor
|
5 |
+
from transformers_gad.grammar_utils import IncrementalGrammarConstraint
|
6 |
+
from transformers_gad.generation.logits_process import GrammarAlignedOracleLogitsProcessor
|
7 |
+
|
8 |
+
class EndpointHandler():
|
9 |
+
def __init__(self, path=""):
|
10 |
+
# Preload
|
11 |
+
self.tokenizer = AutoTokenizer.from_pretrained(path)
|
12 |
+
self.model = AutoModelForCausalLM.from_pretrained(path)
|
13 |
+
|
14 |
+
def __call__(self, data: Dict[str, Any]) -> List[Dict[str, Any]]:
|
15 |
+
# do it!
|
16 |
+
inputs = data.get("inputs",data)
|
17 |
+
grammar_str = data.get("grammar", "")
|
18 |
+
MAX_NEW_TOKENS=512
|
19 |
+
MAX_TIME=30
|
20 |
+
print(grammar_str)
|
21 |
+
grammar = IncrementalGrammarConstraint(grammar_str, "root", self.tokenizer)
|
22 |
+
|
23 |
+
# Initialize logits processor for the grammar
|
24 |
+
gad_oracle_processor = GrammarAlignedOracleLogitsProcessor(grammar)
|
25 |
+
inf_nan_remove_processor = InfNanRemoveLogitsProcessor()
|
26 |
+
logits_processors = LogitsProcessorList([
|
27 |
+
inf_nan_remove_processor,
|
28 |
+
gad_oracle_processor,
|
29 |
+
])
|
30 |
+
|
31 |
+
input_ids = self.tokenizer([inputs], add_special_tokens=False, return_tensors="pt")["input_ids"]
|
32 |
+
|
33 |
+
output = self.model.generate(
|
34 |
+
input_ids,
|
35 |
+
do_sample=True,
|
36 |
+
max_time=MAX_TIME,
|
37 |
+
max_new_tokens=MAX_NEW_TOKENS,
|
38 |
+
logits_processor=logits_processors
|
39 |
+
)
|
40 |
+
|
41 |
+
gad_oracle_processor.reset()
|
42 |
+
|
43 |
+
# Detokenize generated output
|
44 |
+
input_length = 1 if self.model.config.is_encoder_decoder else input_ids.shape[1]
|
45 |
+
if (hasattr(output, "sequences")):
|
46 |
+
generated_tokens = output.sequences[:, input_length:]
|
47 |
+
else:
|
48 |
+
generated_tokens = output[:, input_length:]
|
49 |
+
|
50 |
+
generations = self.tokenizer.batch_decode(generated_tokens, skip_special_tokens=True)
|
51 |
+
return generations
|