Chen An commited on
Commit ·
c111b83
1
Parent(s): ca7e7f5
up
Browse files12345678910111213141516171819202122232425262728293031323334353637383940import gradio as grfrom transformers import AutoModelForCausalLM, AutoTokenizerimport torchimport jsonimport osread_hf_token = os.getenv("read_hf_token")# Load the fine-tuned model and tokenizermodel_name = "laohan/mini_liao" # Replace with your model nametokenizer = AutoTokenizer.from_pretrained(model_name, use_fast=False)model = AutoModelForCausalLM.from_pretrained(model_name,use_fast=False,use_auth_token=read_hf_token)model.eval()# Move model to GPU if availabledevice = torch.device("cuda" if torch.cuda.is_available() else "cpu")model.to(device)# Set pad_token_id to eos_token_idtokenizer.pad_token_id = tokenizer.eos_token_idmodel.config.pad_token_id = model.config.eos_token_iddef generate_category(description):instruction = "Categorize the following project, based on the following info:"prompt = f"Instruction: {instruction}\n\nInput: {description}\n\nOutput:"# Tokenize the promptinputs = tokenizer(prompt, return_tensors='pt', padding=True).to(device)# Generate outputoutput_ids = model.generate(inputs['input_ids'],attention_mask=inputs['attention_mask'],max_length=512,num_beams=5,early_stopping=True,no_repeat_ngram_size=2,12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455import gradio as grfrom transformers import AutoModelForCausalLM, LlamaTokenizerimport torchimport jsonimport os# Retrieve the Hugging Face token from environment variableshf_token = os.getenv('read_hf_token')# Verify that the token is availableif not hf_token:raise ValueError("HUGGINGFACE_TOKEN environment variable not set.")# Load the tokenizer from the base modeltokenizer = LlamaTokenizer.from_pretrained("meta-llama/Llama-3.2-1B",use_fast=False,use_auth_token=hf_token # Use token if base model is private)# Load the fine-tuned modelmodel = AutoModelForCausalLM.from_pretrained("laohan/mini_liao", # Replace with your fine-tuned model nameuse_auth_token=hf_token # Necessary if the fine-tuned model is private)# Move the model to GPU if availabledevice = torch.device("cuda" if torch.cuda.is_available() else "cpu")model.to(device)# Set pad_token_id to eos_token_id to handle paddingtokenizer.pad_token_id = tokenizer.eos_token_idmodel.config.pad_token_id = tokenizer.eos_token_iddef generate_category(description):instruction = "Categorize the following project, based on the following info:"prompt = f"Instruction: {instruction}\n\nInput: {description}\n\nOutput:"# Tokenize the prompt with attention maskinputs = tokenizer(prompt,return_tensors='pt',padding=True,truncation=True,max_length=1024 # Adjust as needed).to(device)# Generate output with attention_maskoutput_ids = model.generate(inputs['input_ids'],attention_mask=inputs['attention_mask'],max_length=512, # Adjust as needednum_beams=5,early_stopping=True,no_repeat_ngram_size=2,
app.py+32-17