Commit
·
4d5cbc9
1
Parent(s):
b5b0c27
better documentation
Browse files
app.py
CHANGED
@@ -43,7 +43,7 @@ if __name__ == "__main__":
|
|
43 |
grammar_processor = GrammarConstrainedLogitsProcessor(grammar)
|
44 |
|
45 |
outputs = model.generate(
|
46 |
-
**inputs, max_new_tokens=
|
47 |
)
|
48 |
# Important: don't forget to set `normalize_logits=True` to obtain normalized probabilities (i.e. sum(p) = 1)
|
49 |
transition_scores = model.compute_transition_scores(outputs.sequences, outputs.scores, normalize_logits=True)
|
@@ -72,11 +72,15 @@ if __name__ == "__main__":
|
|
72 |
gr.Markdown(
|
73 |
"""
|
74 |
# 👻 Transformers-CFG JSON Demo
|
75 |
-
This is a demo of how you can constrain the output of a GPT-2 model to be a valid JSON string.
|
76 |
Here we use a simple JSON grammar to constrain the output of the model.
|
77 |
The grammar is defined in `json_minimal.ebnf` and is written in the Extended Backus-Naur Form (EBNF).
|
|
|
78 |
Internally, it relies on the library [`transformers-cfg`](https://github.com/epfl-dlab/transformers-CFG).
|
79 |
-
For demo purpose, gpt2 is used, but you can use much larger models for better performance.
|
|
|
|
|
|
|
80 |
"""
|
81 |
)
|
82 |
|
|
|
43 |
grammar_processor = GrammarConstrainedLogitsProcessor(grammar)
|
44 |
|
45 |
outputs = model.generate(
|
46 |
+
**inputs, max_new_tokens=30, repetition_penalty=1, return_dict_in_generate=True, output_scores=True, logits_processor=[grammar_processor]
|
47 |
)
|
48 |
# Important: don't forget to set `normalize_logits=True` to obtain normalized probabilities (i.e. sum(p) = 1)
|
49 |
transition_scores = model.compute_transition_scores(outputs.sequences, outputs.scores, normalize_logits=True)
|
|
|
72 |
gr.Markdown(
|
73 |
"""
|
74 |
# 👻 Transformers-CFG JSON Demo
|
75 |
+
This is a demo of how you can constrain the output of a GPT-2 model to be a valid JSON string(up to truncation).
|
76 |
Here we use a simple JSON grammar to constrain the output of the model.
|
77 |
The grammar is defined in `json_minimal.ebnf` and is written in the Extended Backus-Naur Form (EBNF).
|
78 |
+
|
79 |
Internally, it relies on the library [`transformers-cfg`](https://github.com/epfl-dlab/transformers-CFG).
|
80 |
+
For demo purpose, gpt2-large is used, but you can use much larger models for better performance.
|
81 |
+
|
82 |
+
The inference is a bit slow because of the inference is run on **CPU(~20s for 30 tokens)**.
|
83 |
+
The constraint itself **doesn't** introduce significant overhead to the inference.
|
84 |
"""
|
85 |
)
|
86 |
|