Spaces:
Runtime error
Runtime error
Stefan Dumitrescu
commited on
Commit
β’
e816bb5
1
Parent(s):
4dfa08a
Update
Browse files
app.py
ADDED
@@ -0,0 +1,72 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import transformers
|
2 |
+
import streamlit as st
|
3 |
+
|
4 |
+
from transformers import AutoTokenizer, AutoModelWithLMHead
|
5 |
+
|
6 |
+
tokenizer = AutoTokenizer.from_pretrained("gpt2-base")
|
7 |
+
|
8 |
+
|
9 |
+
@st.cache
|
10 |
+
def load_model(model_name):
|
11 |
+
model = AutoModelWithLMHead.from_pretrained("gpt2-base")
|
12 |
+
return model
|
13 |
+
|
14 |
+
|
15 |
+
model = load_model("gpt2-base")
|
16 |
+
|
17 |
+
|
18 |
+
def infer(input_ids, max_length, temperature, top_k, top_p):
|
19 |
+
output_sequences = model.generate(
|
20 |
+
input_ids=input_ids,
|
21 |
+
max_length=max_length,
|
22 |
+
temperature=temperature,
|
23 |
+
top_k=top_k,
|
24 |
+
top_p=top_p,
|
25 |
+
do_sample=True,
|
26 |
+
num_return_sequences=1
|
27 |
+
)
|
28 |
+
|
29 |
+
return output_sequences
|
30 |
+
|
31 |
+
|
32 |
+
default_value = "See how a modern neural network auto-completes your text π€ This site, built by the Hugging Face team, lets you write a whole document directly from your browser, and you can trigger the Transformer anywhere using the Tab key. Its like having a smart machine that completes your thoughts π Get started by typing a custom snippet, check out the repository, or try one of the examples. Have fun!"
|
33 |
+
|
34 |
+
# prompts
|
35 |
+
st.title("Write with Transformers π¦")
|
36 |
+
st.write(
|
37 |
+
"The almighty king of text generation, GPT-2 comes in four available sizes, only three of which have been publicly made available. Feared for its fake news generation capabilities, it currently stands as the most syntactically coherent model. A direct successor to the original GPT, it reinforces the already established pre-training/fine-tuning killer duo. From the paper: Language Models are Unsupervised Multitask Learners by Alec Radford, Jeffrey Wu, Rewon Child, David Luan, Dario Amodei and Ilya Sutskever.")
|
38 |
+
|
39 |
+
sent = st.text_area("Text", default_value, height=275)
|
40 |
+
max_length = st.sidebar.slider("Max Length", min_value=10, max_value=30)
|
41 |
+
temperature = st.sidebar.slider("Temperature", value=1.0, min_value=0.0, max_value=1.0, step=0.05)
|
42 |
+
top_k = st.sidebar.slider("Top-k", min_value=0, max_value=5, value=0)
|
43 |
+
top_p = st.sidebar.slider("Top-p", min_value=0.0, max_value=1.0, step=0.05, value=0.9)
|
44 |
+
|
45 |
+
encoded_prompt = tokenizer.encode(sent, add_special_tokens=False, return_tensors="pt")
|
46 |
+
if encoded_prompt.size()[-1] == 0:
|
47 |
+
input_ids = None
|
48 |
+
else:
|
49 |
+
input_ids = encoded_prompt
|
50 |
+
|
51 |
+
output_sequences = infer(input_ids, max_length, temperature, top_k, top_p)
|
52 |
+
|
53 |
+
for generated_sequence_idx, generated_sequence in enumerate(output_sequences):
|
54 |
+
print(f"=== GENERATED SEQUENCE {generated_sequence_idx + 1} ===")
|
55 |
+
generated_sequences = generated_sequence.tolist()
|
56 |
+
|
57 |
+
# Decode text
|
58 |
+
text = tokenizer.decode(generated_sequence, clean_up_tokenization_spaces=True)
|
59 |
+
|
60 |
+
# Remove all text after the stop token
|
61 |
+
# text = text[: text.find(args.stop_token) if args.stop_token else None]
|
62 |
+
|
63 |
+
# Add the prompt at the beginning of the sequence. Remove the excess text that was used for pre-processing
|
64 |
+
total_sequence = (
|
65 |
+
sent + text[len(tokenizer.decode(encoded_prompt[0], clean_up_tokenization_spaces=True)):]
|
66 |
+
)
|
67 |
+
|
68 |
+
generated_sequences.append(total_sequence)
|
69 |
+
print(total_sequence)
|
70 |
+
|
71 |
+
st.write(generated_sequences[-1])
|
72 |
+
|