Skip to content

Commit

Permalink
weird fix
Browse files Browse the repository at this point in the history
  • Loading branch information
jaytimm committed Aug 28, 2024
1 parent 88b30c0 commit 56e6b6c
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 15 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,5 @@
.Rhistory
.RData
.Ruserdata
README.Rmd
inst/doc
4 changes: 2 additions & 2 deletions R/hollr.R
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ hollr <- function(id,
extract_json = TRUE) {

# Determine if the model is OpenAI or local
is_openai_model <- grepl("gpt-3.5-turbo|gpt-4|gpt-4o|gpt-4o-mini", model, ignore.case = TRUE)
is_openai_model <- grepl("^gpt-", model, ignore.case = TRUE)

if (!is_openai_model && batch_size > 1) {
# Run the local model with batch processing
Expand Down Expand Up @@ -110,7 +110,7 @@ hollr <- function(id,
force_json,
max_attempts)

cleaned_response <- gsub("^\\s*\\[\\s*\\{.*\\}\\s*\\]\\s*$", "", validation_result$response)
cleaned_response <- gsub("^```json\\s*|```$", "", validation_result$response)

list(id = row$id,
annotator_id = row$annotator_id,
Expand Down
21 changes: 8 additions & 13 deletions inst/python/llm_functions.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@


def initialize_model(model_name):
"""
Initialize a text generation model using the Hugging Face transformers library.
Expand All @@ -10,22 +8,18 @@ def initialize_model(model_name):
Returns:
pipeline: A text generation pipeline using the specified model and tokenizer.
"""
import os
import torch
from transformers import AutoModelForCausalLM, AutoTokenizer, pipeline

# Set the CUDA allocation configuration
os.environ['PYTORCH_CUDA_ALLOC_CONF'] = 'expandable_segments:True'

# Optionally, clear the CUDA cache before starting
torch.cuda.empty_cache()
import torch

# Load the tokenizer for the specified model
tokenizer = AutoTokenizer.from_pretrained(model_name)
print("Initial pad token:", tokenizer.pad_token) # Debugging: Print initial pad token

# Set pad token if it does not exist
if tokenizer.pad_token is None:
print("No pad token found, adding '[PAD]' as pad_token")
tokenizer.add_special_tokens({'pad_token': '[PAD]'})
print("New pad token set:", tokenizer.pad_token)

# Load the model
model = AutoModelForCausalLM.from_pretrained(model_name,
Expand All @@ -34,17 +28,18 @@ def initialize_model(model_name):

# Update model embeddings
model.resize_token_embeddings(len(tokenizer))

# Optionally, clear the CUDA cache after model initialization
torch.cuda.empty_cache()
print("Model embeddings updated to reflect new tokenizer size.")

# Create text generation pipeline
model_pipeline = pipeline("text-generation", model=model, tokenizer=tokenizer)
print("Pipeline created with pad token:", tokenizer.pad_token) # Confirm pad token at pipeline creation

return model_pipeline





def generate_text_batch(model_pipeline, prompt, temperature, max_length, max_new_tokens=None):
"""
Generate text in batch using the specified text generation pipeline, ensuring the input prompt is not included in the output.
Expand Down

0 comments on commit 56e6b6c

Please sign in to comment.