Spaces:
Sleeping
Sleeping
use a local transformers model with a grammar
Browse files- app.py +43 -25
- requirements.txt +5 -1
app.py
CHANGED
@@ -1,46 +1,63 @@
|
|
1 |
import gradio as gr
|
2 |
-
|
3 |
-
import
|
|
|
4 |
|
5 |
"""
|
6 |
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
|
7 |
"""
|
8 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
9 |
|
10 |
|
11 |
def respond(
|
12 |
message,
|
13 |
history: list[tuple[str, str]],
|
14 |
system_message,
|
|
|
15 |
max_tokens,
|
16 |
temperature,
|
17 |
top_p,
|
18 |
):
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
messages.append({"role": "user", "content": message})
|
28 |
|
29 |
-
|
30 |
-
grammar_regex = r"^- H[^\r\n\x0b\x0c\x85\u2028\u2029]+\n- E[^\r\n\x0b\x0c\x85\u2028\u2029]+\n- L[^\r\n\x0b\x0c\x85\u2028\u2029]+\n- P[^\r\n\x0b\x0c\x85\u2028\u2029]+\n- M[^\r\n\x0b\x0c\x85\u2028\u2029]+\n- E[^\r\n\x0b\x0c\x85\u2028\u2029]+\n$"
|
31 |
|
32 |
-
for
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
top_p=top_p,
|
38 |
-
response_format=grammar_regex,
|
39 |
-
):
|
40 |
-
token = message.choices[0].delta.content
|
41 |
|
42 |
-
|
43 |
-
|
44 |
|
45 |
|
46 |
"""
|
@@ -50,6 +67,7 @@ demo = gr.ChatInterface(
|
|
50 |
respond,
|
51 |
additional_inputs=[
|
52 |
gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
|
|
|
53 |
gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Maximum new tokens"),
|
54 |
gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
|
55 |
gr.Slider(
|
|
|
1 |
import gradio as gr
|
2 |
+
import outlines
|
3 |
+
import transformers
|
4 |
+
import torch
|
5 |
|
6 |
"""
|
7 |
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
|
8 |
"""
|
9 |
+
pipe = transformers.pipeline("text-generation", "HuggingFaceTB/SmolLM-135M-Instruct", torch_dtype=torch.float32)
|
10 |
+
outlines_tokenizer = outlines.models.TransformerTokenizer(pipe.tokenizer)
|
11 |
+
|
12 |
+
### TODO 1: use outliunes with a transformer model made directly
|
13 |
+
### TODO 2: use a cfg
|
14 |
+
|
15 |
+
def string_to_acrostic_grammar(s, dash_initial=True):
|
16 |
+
# this will convert a string to a CFG grammar
|
17 |
+
chars = filter(str.isalpha, s.upper())
|
18 |
+
grammar_rules = [('"- " ' if dash_initial else '') + f'"{char}" /[^-\\r\\n]+/ "\\n"' for char in chars]
|
19 |
+
return "?start: " + " ".join(grammar_rules)
|
20 |
+
|
21 |
+
def is_this_prompt_a_list(prompt):
|
22 |
+
return False
|
23 |
+
# ask the model if the prompt is a list, by constraining the generation to yes or no about a question whether the prompt is a list
|
24 |
+
question = f'This is a prompt that you have been asked to answer:\n\n```\n{prompt}\n```\n\nIs this prompt asking for a list of items, instead of a story? Begin your answer with "Yes" if asking for a list, otherwise "No", and then give an explanation of why.'
|
25 |
+
grammar = '?start: ("Yes" | "No")'
|
26 |
+
cfg_logits_processor = outlines.processors.CFGLogitsProcessor(grammar, outlines_tokenizer)
|
27 |
+
output = pipe([{"role": "user", "content": question}], logits_processor=transformers.LogitsProcessorList([cfg_logits_processor]), max_new_tokens=10,)
|
28 |
+
# output = pipe([{"role": "system", "content": "You are a helpful assistant who answers in one-word answers."}, {"role": "user", "content": question}], max_new_tokens=10,)
|
29 |
+
response = output[0]['generated_text'][-1]['content']
|
30 |
+
print("is this prompt a list?", response)
|
31 |
+
return response == "Yes"
|
32 |
|
33 |
|
34 |
def respond(
|
35 |
message,
|
36 |
history: list[tuple[str, str]],
|
37 |
system_message,
|
38 |
+
acrostic,
|
39 |
max_tokens,
|
40 |
temperature,
|
41 |
top_p,
|
42 |
):
|
43 |
+
print({"message": message, "history": history, "system_message": system_message, "acrostic": acrostic, "max_tokens": max_tokens, "temperature": temperature, "top_p": top_p})
|
44 |
+
# grammar = "\n".join(['?start: item item item','?item: "- " /[^-\\r\\n]+/ "\\n"'])
|
45 |
+
grammar = string_to_acrostic_grammar(acrostic, dash_initial=is_this_prompt_a_list(message))
|
46 |
+
two_items_logits_processor = outlines.processors.CFGLogitsProcessor( grammar , outlines_tokenizer )
|
47 |
+
output = pipe([{"role": "user", "content": message}], logits_processor=transformers.LogitsProcessorList([two_items_logits_processor]), max_new_tokens=max_tokens,)
|
48 |
+
print(output)
|
49 |
+
response = output[0]['generated_text'][-1]['content']
|
|
|
|
|
50 |
|
51 |
+
# messages = [{"role": "system", "content": system_message}]
|
|
|
52 |
|
53 |
+
# for val in history:
|
54 |
+
# if val[0]:
|
55 |
+
# messages.append({"role": "user", "content": val[0]})
|
56 |
+
# if val[1]:
|
57 |
+
# messages.append({"role": "assistant", "content": val[1]})
|
|
|
|
|
|
|
|
|
58 |
|
59 |
+
# messages.append({"role": "user", "content": message})
|
60 |
+
yield response
|
61 |
|
62 |
|
63 |
"""
|
|
|
67 |
respond,
|
68 |
additional_inputs=[
|
69 |
gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
|
70 |
+
gr.Textbox(value="I love you", label="acrostic"),
|
71 |
gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Maximum new tokens"),
|
72 |
gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
|
73 |
gr.Slider(
|
requirements.txt
CHANGED
@@ -1 +1,5 @@
|
|
1 |
-
|
|
|
|
|
|
|
|
|
|
1 |
+
torch
|
2 |
+
transformers
|
3 |
+
outlines
|
4 |
+
sentencepiece
|
5 |
+
datasets
|