Spaces:
Sleeping
Sleeping
Asankhaya Sharma
commited on
Commit
·
af7e3ad
1
Parent(s):
5666a5c
init app
Browse files
app.py
CHANGED
@@ -1,71 +1,73 @@
|
|
1 |
import transformers
|
2 |
import streamlit as st
|
3 |
|
4 |
-
from transformers import
|
|
|
|
|
5 |
|
6 |
-
tokenizer = AutoTokenizer.from_pretrained(
|
7 |
-
@st.
|
8 |
def load_model(model_name):
|
9 |
-
model =
|
10 |
return model
|
11 |
|
12 |
-
model = load_model(
|
13 |
|
14 |
-
def infer(input_ids,
|
15 |
|
16 |
output_sequences = model.generate(
|
17 |
input_ids=input_ids,
|
18 |
-
|
19 |
temperature=temperature,
|
20 |
top_k=top_k,
|
21 |
top_p=top_p,
|
22 |
do_sample=True,
|
|
|
|
|
|
|
|
|
23 |
num_return_sequences=1
|
24 |
)
|
25 |
|
26 |
return output_sequences
|
27 |
-
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!"
|
28 |
|
|
|
|
|
29 |
#prompts
|
30 |
-
st.title("Write with
|
31 |
-
st.write("
|
|
|
|
|
32 |
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
top_k = st.sidebar.slider("Top-k", min_value = 0, max_value=5, value = 0)
|
37 |
top_p = st.sidebar.slider("Top-p", min_value = 0.0, max_value=1.0, step = 0.05, value = 0.9)
|
38 |
|
39 |
-
encoded_prompt = tokenizer.encode(sent, add_special_tokens=False, return_tensors="pt")
|
40 |
if encoded_prompt.size()[-1] == 0:
|
41 |
input_ids = None
|
42 |
else:
|
43 |
input_ids = encoded_prompt
|
44 |
|
45 |
-
|
46 |
-
output_sequences = infer(input_ids, max_length, temperature, top_k, top_p)
|
47 |
-
|
48 |
-
|
49 |
|
50 |
for generated_sequence_idx, generated_sequence in enumerate(output_sequences):
|
51 |
print(f"=== GENERATED SEQUENCE {generated_sequence_idx + 1} ===")
|
52 |
generated_sequences = generated_sequence.tolist()
|
53 |
|
54 |
# Decode text
|
55 |
-
text = tokenizer.decode(generated_sequence, clean_up_tokenization_spaces=True)
|
56 |
|
57 |
# Remove all text after the stop token
|
58 |
#text = text[: text.find(args.stop_token) if args.stop_token else None]
|
59 |
|
60 |
# Add the prompt at the beginning of the sequence. Remove the excess text that was used for pre-processing
|
61 |
total_sequence = (
|
62 |
-
sent + text[len(tokenizer.decode(encoded_prompt[0], clean_up_tokenization_spaces=True)) :]
|
63 |
)
|
64 |
|
65 |
generated_sequences.append(total_sequence)
|
66 |
print(total_sequence)
|
67 |
|
68 |
-
|
69 |
-
st.write(generated_sequences[-1])
|
70 |
-
|
71 |
-
|
|
|
1 |
import transformers
|
2 |
import streamlit as st
|
3 |
|
4 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer
|
5 |
+
|
6 |
+
checkpoint = "gpt2-large"
|
7 |
|
8 |
+
tokenizer = AutoTokenizer.from_pretrained(checkpoint)
|
9 |
+
@st.cache_resource
|
10 |
def load_model(model_name):
|
11 |
+
model = AutoModelForCausalLM.from_pretrained(model_name)
|
12 |
return model
|
13 |
|
14 |
+
model = load_model(checkpoint)
|
15 |
|
16 |
+
def infer(input_ids, max_tokens, temperature, top_k, top_p):
|
17 |
|
18 |
output_sequences = model.generate(
|
19 |
input_ids=input_ids,
|
20 |
+
max_new_tokens=max_tokens,
|
21 |
temperature=temperature,
|
22 |
top_k=top_k,
|
23 |
top_p=top_p,
|
24 |
do_sample=True,
|
25 |
+
no_repeat_ngram_size=2,
|
26 |
+
early_stopping=True,
|
27 |
+
num_beams=4,
|
28 |
+
pad_token_id=tokenizer.eos_token_id,
|
29 |
num_return_sequences=1
|
30 |
)
|
31 |
|
32 |
return output_sequences
|
|
|
33 |
|
34 |
+
default_value = "We are building the world's first"
|
35 |
+
|
36 |
#prompts
|
37 |
+
st.title("Write with vcGPT 🦄")
|
38 |
+
st.write("This is a LLM that was fine-tuned on a dataset of investment memos to help you generate your next pitch.")
|
39 |
+
|
40 |
+
sent = st.text_area("Text", default_value, height = 400)
|
41 |
|
42 |
+
max_tokens = st.sidebar.slider("Max Tokens", min_value = 32, max_value=512)
|
43 |
+
temperature = st.sidebar.slider("Temperature", value = 0.8, min_value = 0.0, max_value=1.0, step=0.05)
|
44 |
+
top_k = st.sidebar.slider("Top-k", min_value = 0, max_value=5, value = 4)
|
|
|
45 |
top_p = st.sidebar.slider("Top-p", min_value = 0.0, max_value=1.0, step = 0.05, value = 0.9)
|
46 |
|
47 |
+
encoded_prompt = tokenizer.encode(tokenizer.eos_token+sent, add_special_tokens=False, return_tensors="pt")
|
48 |
if encoded_prompt.size()[-1] == 0:
|
49 |
input_ids = None
|
50 |
else:
|
51 |
input_ids = encoded_prompt
|
52 |
|
53 |
+
output_sequences = infer(input_ids, max_tokens, temperature, top_k, top_p)
|
|
|
|
|
|
|
54 |
|
55 |
for generated_sequence_idx, generated_sequence in enumerate(output_sequences):
|
56 |
print(f"=== GENERATED SEQUENCE {generated_sequence_idx + 1} ===")
|
57 |
generated_sequences = generated_sequence.tolist()
|
58 |
|
59 |
# Decode text
|
60 |
+
text = tokenizer.decode(generated_sequence, clean_up_tokenization_spaces=True, skip_special_tokens=True)
|
61 |
|
62 |
# Remove all text after the stop token
|
63 |
#text = text[: text.find(args.stop_token) if args.stop_token else None]
|
64 |
|
65 |
# Add the prompt at the beginning of the sequence. Remove the excess text that was used for pre-processing
|
66 |
total_sequence = (
|
67 |
+
sent + text[len(tokenizer.decode(encoded_prompt[0], clean_up_tokenization_spaces=True, skip_special_tokens=True)) :]
|
68 |
)
|
69 |
|
70 |
generated_sequences.append(total_sequence)
|
71 |
print(total_sequence)
|
72 |
|
73 |
+
st.write(generated_sequences[-1])
|
|
|
|
|
|