Hugging_Space / app.py
Pippoz's picture
Update app.py
567921c
raw
history blame
1.6 kB
import streamlit as st
import time
import tokenizers
from transformers import pipeline
import torch
#from transformers import AutoModelForCausalLM, AutoTokenizer
#@st.cache(allow_output_mutation=True)
#def define_model():
# model = AutoModelForCausalLM.from_pretrained("facebook/opt-1.3b", torch_dtype=torch.float16).cuda()
# tokenizer = AutoTokenizer.from_pretrained("facebook/opt-1.3b", use_fast=False)
# return model, tokenizer
@st.cache(hash_funcs={tokenizers.Tokenizer: lambda _: None, tokenizers.AddedToken: lambda _: None})
def define_model():
generator = pipeline('text-generation', model="facebook/opt-1.3b", skip_special_tokens=True)
return generator
#@st.cache(allow_output_mutation=True)
#def opt_model(prompt, model, tokenizer, num_sequences = 1, max_length = 50):
# input_ids = tokenizer(prompt, return_tensors="pt").input_ids.cuda()
# generated_ids = model.generate(input_ids, num_return_sequences=num_sequences, max_length=max_length)
# answer = tokenizer.batch_decode(generated_ids, skip_special_tokens=True)
# return answer
#model, tokenizer = define_model()
generator = define_model()
prompt= st.text_area('Your prompt here',
'''Hello, I'm am conscious and''')
answer = generator(prompt, max_length=100,no_repeat_ngram_size=3, early_stopping=True, num_beams=10)
#answer = opt_model(prompt, model, tokenizer,)
#lst = ['ciao come stai sjfsbd dfhsdf fuahfuf feuhfu wefwu ']
#answer = define_model(prompt)
lst = answer[0]['generated_text']
t = st.empty()
for i in range(len(lst)):
t.markdown(" %s..." % lst[0:i])
time.sleep(0.04)