Chen An commited on
Commit ·
4e25d6b
1
Parent(s): 30215be
update
Browse files123455 unmodified lines1112131415161718192021222324252627285 unmodified lines343536373839402 unmodified lines43444546474849505152535455565758596061626364656667686911 unmodified lines81828384import gradio as grfrom transformers import AutoModelForCausalLM, LlamaTokenizerimport torchimport jsonimport os5 unmodified linesif not hf_token:raise ValueError("HUGGINGFACE_TOKEN environment variable not set.")tokenizer = LlamaTokenizer.from_pretrained("meta-llama/Llama-3.2-1B",use_fast=False,use_auth_token=hf_token)# Load the fine-tuned modelmodel = AutoModelForCausalLM.from_pretrained("laohan/mini_liao", # Replace with your fine-tuned model nameuse_auth_token=hf_token)# Move the model to GPU if availabledevice = torch.device("cuda" if torch.cuda.is_available() else "cpu")model.to(device)5 unmodified linesdef 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,2 unmodified linestruncation=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=1024, # Adjust as needednum_beams=5,early_stopping=True,no_repeat_ngram_size=2,)# Decode the generated tokensoutput_text = tokenizer.decode(output_ids[0], skip_special_tokens=True)# Extract the output after 'Output:'output_parts = output_text.split('Output:')if len(output_parts) > 1:category_output = output_parts[1].strip()else:category_output = "No output generated."# Parse JSON if possibletry:category_dict = json.loads(category_output)11 unmodified lines)# Launch the interfaceiface.launch()12345675 unmodified lines131415161718192021222324252627282930315 unmodified lines373839404142432 unmodified lines46474849505152535455565758596061626364656667686970717211 unmodified lines84858687import gradio as grfrom transformers import AutoModelForCausalLM, LlamaTokenizerfrom transformers import LlamaTokenizerFastimport torchimport jsonimport os5 unmodified linesif not hf_token:raise ValueError("HUGGINGFACE_TOKEN environment variable not set.")tokenizer = LlamaTokenizerFast.from_pretrained("meta-llama/Llama-3.2-1B",use_fast=False,token=hf_token # Updated parameter)model = AutoModelForCausalLM.from_pretrained("laohan/mini_liao",token=hf_token # Updated parameter)# Move the model to GPU if availabledevice = torch.device("cuda" if torch.cuda.is_available() else "cpu")model.to(device)5 unmodified linesdef 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,2 unmodified linestruncation=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,)# Decode the generated tokensoutput_text = tokenizer.decode(output_ids[0], skip_special_tokens=True)# Extract the output after 'Output:'output_parts = output_text.split('Output:')if len(output_parts) > 1:category_output = output_parts[1].strip()else:category_output = "No output generated."# Parse JSON if possibletry:category_dict = json.loads(category_output)11 unmodified lines)# Launch the interfaceiface.launch()
app.py+15-12