codelion's picture
Update app.py
fd3e5e5 verified
raw
history blame
No virus
2.83 kB
import transformers
import streamlit as st
from transformers import AutoModelForCausalLM, AutoTokenizer
import spaces
checkpoint = "."
tokenizer = AutoTokenizer.from_pretrained(checkpoint)
@st.cache_resource
def load_model(model_name):
model = AutoModelForCausalLM.from_pretrained(model_name)
return model
model = load_model(checkpoint)
@spaces.GPU
def infer(input_ids, bad_words_ids, max_tokens, temperature, top_k, top_p):
output_sequences = model.generate(
input_ids=input_ids,
bad_words_ids = bad_words_ids,
max_new_tokens=max_tokens,
temperature=temperature,
top_k=top_k,
top_p=top_p,
do_sample=True,
no_repeat_ngram_size=2,
early_stopping=True,
num_beams=4,
pad_token_id=tokenizer.eos_token_id,
num_return_sequences=1
)
return output_sequences
default_value = "We are building the first ever"
#prompts
st.title("Write with vcGPT 🦄")
st.write("This is a LLM that was fine-tuned on a dataset of investment memos to help you generate your next pitch.")
sent = st.text_area("Text", default_value)
max_tokens = st.sidebar.slider("Max Tokens", min_value = 16, max_value=64)
temperature = st.sidebar.slider("Temperature", value = 0.8, min_value = 0.05, max_value=1.0, step=0.05)
top_k = st.sidebar.slider("Top-k", min_value = 0, max_value=5, value = 4)
top_p = st.sidebar.slider("Top-p", min_value = 0.0, max_value=1.0, step = 0.05, value = 0.9)
# print(model.config.max_position_embeddings)
encoded_prompt = tokenizer.encode(tokenizer.eos_token+sent, max_length=1024, return_tensors="pt", truncation=True)
# get tokens of words that should not be generated
bad_words_ids = tokenizer(["confidential", "angel.co", "angellist.com", "angellist"], add_special_tokens=False).input_ids
if encoded_prompt.size()[-1] == 0:
input_ids = None
else:
input_ids = encoded_prompt
output_sequences = infer(input_ids, bad_words_ids, max_tokens, temperature, top_k, top_p)
for generated_sequence_idx, generated_sequence in enumerate(output_sequences):
print(f"=== GENERATED SEQUENCE {generated_sequence_idx + 1} ===")
generated_sequences = generated_sequence.tolist()
# Decode text
text = tokenizer.decode(generated_sequence, clean_up_tokenization_spaces=True, skip_special_tokens=True)
# Remove all text after the stop token
#text = text[: text.find(args.stop_token) if args.stop_token else None]
# Add the prompt at the beginning of the sequence. Remove the excess text that was used for pre-processing
total_sequence = (
sent + text[len(tokenizer.decode(encoded_prompt[0], clean_up_tokenization_spaces=True, skip_special_tokens=True)) :]
)
generated_sequences.append(total_sequence)
print(total_sequence)
st.markdown(generated_sequences[-1])