File size: 3,874 Bytes
3848545 b4fc608 cf75435 b4fc608 b990905 d24bd47 d322739 3dcad86 8ef7c37 3dcad86 8ef7c37 b4fc608 c23eaa3 8ef7c37 2bef81d 8ef7c37 2bef81d 57c9efe 2bef81d 3848545 b4fc608 3848545 8ef7c37 b4fc608 3848545 d24bd47 b990905 b4fc608 8ef7c37 b4fc608 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 |
# from transformers import AutoModelForCausalLM, AutoTokenizer
import time
import datetime
import streamlit as st
question = "Name the planets in the solar system? A: "
question = "Quais são os planetas do sistema solar?"
question = "Qual é o maior planeta do sistema solar?"
before = datetime.datetime.now()
# Load model directly
# from transformers import AutoTokenizer, AutoModelForCausalLM
# tokenizer = AutoTokenizer.from_pretrained("01-ai/Yi-1.5-6B-Chat")
# model = AutoModelForCausalLM.from_pretrained("01-ai/Yi-1.5-6B-Chat")
# Load model directly
from transformers import AutoTokenizer, Phi3ForCausalLM
model = Phi3ForCausalLM.from_pretrained("microsoft/phi-3-mini-4k-instruct")
tokenizer = AutoTokenizer.from_pretrained("microsoft/phi-3-mini-4k-instruct")
prompt = "Question: Qual é o maior planeta do sistema solar ?"
inputs = tokenizer(prompt, return_tensors="pt")
# Generate
generate_ids = model.generate(inputs.input_ids, max_length=100)
output = tokenizer.batch_decode(generate_ids, skip_special_tokens=True, clean_up_tokenization_spaces=False)[0]
with st.container():
st.write('\n\n')
st.write('LLM-LANAChat')
st.write('\n\n' + output)
# tokenizer = AutoTokenizer.from_pretrained("FacebookAI/roberta-base")
# model = TFRobertaModel.from_pretrained("FacebookAI/roberta-base")
# st.write('tokenizando...')
# inputs = tokenizer(question, return_tensors="tf")
# st.write('gerando a saida...')
# outputs = model(inputs)
# last_hidden_states = outputs.last_hidden_state
# output = last_hidden_states
# st.write(output)
# st.write('tokenizando...')
# prompt = "Qual é o maior planeta do sistema solar ?"
# # inputs = tokenizer(prompt, return_tensors="pt")
# # Generate
# st.write('gerando a saida...')
# # generate_ids = model.generate(inputs.input_ids, max_length=30)
# # output = tokenizer.batch_decode(generate_ids, skip_special_tokens=True, clean_up_tokenization_spaces=False)[0]
# st.write('saída gerada')
# st.write(output)
# # Use a pipeline as a high-level helper
# # from transformers import pipeline
# # messages = [
# # {"role": "user", "content": question},
# # ]
# print('gerando a saida...')
# st.write('gerando a saida...')
# pipe = pipeline("text-generation", model="01-ai/Yi-1.5-34B-Chat")
# st.write('pipeline...')
# output = pipe(messages)
# st.write('saída gerada...')
# st.write(output)
# print('tokenizando...')
# tokenizer = AutoTokenizer.from_pretrained(model_path, use_fast=False)
# print('tokenizado.')
# print('carregando o modelo...')
# # Since transformers 4.35.0, the GPT-Q/AWQ model can be loaded using AutoModelForCausalLM.
# model = AutoModelForCausalLM.from_pretrained(
# model_path,
# device_map="auto",
# torch_dtype='auto'
# ).eval()
# print('modelo carreegado.')
# # Prompt content: "hi"
# messages = [
# {"role": "user", "content": question}
# ]
# print('tokenizando o prompt...')
# input_ids = tokenizer.apply_chat_template(conversation=messages, tokenize=True, return_tensors='pt')
# print('prompt tokenizado.')
# print('gerando a saida...')
# output_ids = model.generate(input_ids, eos_token_id=tokenizer.eos_token_id,
# max_new_tokens=10) #10 # 45
# # max_new_tokens=22)
print('saida gerada.')
# print('Decodificando a saida...')
# response = tokenizer.decode(output_ids[0][input_ids.shape[1]:], skip_special_tokens=True)
# print('saida decodificada.')
# Model response: "Hello! How can I assist you today?"
# print(response)
# question = output['choices'][0]['text'].split('A:')[0]
# answer = output['choices'][0]['text'].split('A:')[1]
# answer = 'A: ' + answer
print('\n\n')
# print(question)
# print(response)
after = datetime.datetime.now()
current_time = (after - before) # .strftime("%H:%M:%S")
print("\nTime Elapsed: ", current_time)
|