Momin Aziz commited on
Commit
54972b3
1 Parent(s): 25b7701

testing gpt2 large

Browse files
Files changed (2) hide show
  1. app.py +64 -0
  2. requirements.txt +3 -0
app.py ADDED
@@ -0,0 +1,64 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ from transformers import AutoTokenizer, AutoModelWithLMHead, pipeline
3
+
4
+ model_name = "gpt2-large"
5
+ tokenizer = AutoTokenizer.from_pretrained(model_name)
6
+ @st.cache
7
+ def load_model(model_name):
8
+ model = AutoModelWithLMHead.from_pretrained(model_name)
9
+ return model
10
+
11
+ model = load_model(model_name)
12
+
13
+ def infer(input_ids, **generator_args):
14
+
15
+ output_sequences = model.generate(
16
+ input_ids,generator_args
17
+ )
18
+
19
+ return output_sequences
20
+ default_value = "See how a modern neural network auto-completes your text 🤗 Have fun!"
21
+
22
+ #prompts
23
+ st.title("Text Extension")
24
+ st.write("Some other texts, like instructions...")
25
+
26
+ sent = st.text_area("Text", default_value, height = 275)
27
+ max_length = st.sidebar.slider("Max Length", min_value = 10, max_value=50)
28
+ temperature = st.sidebar.slider("Temperature", value = 1.0, min_value = 0.0, max_value=1.0, step=0.05)
29
+ num_return_sequences = st.sidebar.slider("Num Return Sequences", min_value = 1, max_value=4, value = 1)
30
+ num_beams = st.sidebar.slider("Num Beams", min_value = 4, max_value=6, value = 4)
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
+
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
+
40
+
41
+ output_sequences = infer(input_ids, max_length=max_length,num_return_sequences=num_return_sequences,
42
+ num_beams=num_beams,
43
+ temperature=temperature, top_k=top_k, top_p=top_p)
44
+
45
+ for generated_sequence_idx, generated_sequence in enumerate(output_sequences):
46
+ print(f"=== GENERATED SEQUENCE {generated_sequence_idx + 1} ===")
47
+ generated_sequences = generated_sequence.tolist()
48
+
49
+ # Decode text
50
+ text = tokenizer.decode(generated_sequence, clean_up_tokenization_spaces=True)
51
+
52
+ # Remove all text after the stop token
53
+ #text = text[: text.find(args.stop_token) if args.stop_token else None]
54
+
55
+ # Add the prompt at the beginning of the sequence. Remove the excess text that was used for pre-processing
56
+ total_sequence = (
57
+ sent + text[len(tokenizer.decode(encoded_prompt[0], clean_up_tokenization_spaces=True)) :]
58
+ )
59
+
60
+ generated_sequences.append(total_sequence)
61
+ print(total_sequence)
62
+
63
+
64
+ st.write(generated_sequences[-1])
requirements.txt ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ transformers
2
+ streamlit
3
+ torch