Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,55 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import transformers
|
2 |
+
import streamlit as st
|
3 |
+
from transformers import AutoTokenizer, AutoModelWithLMHead
|
4 |
+
tokenizer = AutoTokenizer.from_pretrained("anonymous-german-nlp/german-gpt2")
|
5 |
+
@st.cache
|
6 |
+
def load_model(model_name):
|
7 |
+
model = AutoModelWithLMHead.from_pretrained("Jipski/MegStuart_gpt-2")
|
8 |
+
return model
|
9 |
+
model = load_model("Jipski/MegStuart_gpt-2")
|
10 |
+
def infer(input_ids, max_length, temperature, top_k, top_p, num_return_sequences):
|
11 |
+
output_sequences = model.generate(
|
12 |
+
input_ids=input_ids,
|
13 |
+
max_length=max_length,
|
14 |
+
temperature=temperature,
|
15 |
+
top_k=top_k,
|
16 |
+
top_p=top_p,
|
17 |
+
do_sample=True,
|
18 |
+
num_return_sequences=num_return_sequences,
|
19 |
+
)
|
20 |
+
return output_sequences
|
21 |
+
def update_showing():
|
22 |
+
st.session_state.showing = st.session_state.gen
|
23 |
+
|
24 |
+
default_value = "Jetzt tippen!"
|
25 |
+
#prompts
|
26 |
+
st.title("Meg Stuart gpt-2")
|
27 |
+
#st.write("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.")
|
28 |
+
sent = st.text_area("Text", default_value, key='showing', height = 275)
|
29 |
+
max_length = st.sidebar.slider("Max Length", min_value = 50, max_value=500)
|
30 |
+
temperature = st.sidebar.slider("Temperature", value = 1.0, min_value = 0.0, max_value=1.0, step=0.05)
|
31 |
+
top_k = st.sidebar.slider("Top-k", min_value = 0, max_value=5, value = 0)
|
32 |
+
top_p = st.sidebar.slider("Top-p", min_value = 0.0, max_value=1.0, step = 0.05, value = 0.9)
|
33 |
+
num_return_sequences = st.sidebar.number_input('Number of Return Sequences', min_value=1, max_value=5, value=1, step=1)
|
34 |
+
encoded_prompt = tokenizer.encode(sent, add_special_tokens=False, return_tensors="pt")
|
35 |
+
if encoded_prompt.size()[-1] == 0:
|
36 |
+
input_ids = None
|
37 |
+
else:
|
38 |
+
input_ids = encoded_prompt
|
39 |
+
output_sequences = infer(input_ids, max_length, temperature, top_k, top_p, num_return_sequences)
|
40 |
+
for generated_sequence_idx, generated_sequence in enumerate(output_sequences):
|
41 |
+
|
42 |
+
print(f"=== GENERATED SEQUENCE {generated_sequence_idx + 1} ===")
|
43 |
+
generated_sequences = generated_sequence.tolist()
|
44 |
+
# Decode text
|
45 |
+
text = tokenizer.decode(generated_sequence, clean_up_tokenization_spaces=True)
|
46 |
+
# Remove all text after the stop token
|
47 |
+
#text = text[: text.find(args.stop_token) if args.stop_token else None]
|
48 |
+
# Add the prompt at the beginning of the sequence. Remove the excess text that was used for pre-processing
|
49 |
+
total_sequence = (
|
50 |
+
sent + text[len(tokenizer.decode(encoded_prompt[0], clean_up_tokenization_spaces=True)) :]
|
51 |
+
)
|
52 |
+
generated_sequences.append(total_sequence)
|
53 |
+
print(total_sequence)
|
54 |
+
|
55 |
+
st.write(generated_sequences[-1])
|