AhmedSSabir commited on
Commit
267d2e8
·
verified ·
1 Parent(s): 3582b44

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +7 -7
app.py CHANGED
@@ -50,9 +50,12 @@ def get_sim(x):
50
  #tokenizer = GPT2Tokenizer.from_pretrained('distilgpt2')
51
 
52
 
 
 
53
 
54
- tokenizer = GPT2Tokenizer.from_pretrained('gpt2')
55
- model = GPT2LMHeadModel.from_pretrained('gpt2')
 
56
 
57
 
58
 
@@ -60,22 +63,19 @@ def sentence_prob_mean(text):
60
  # Tokenize the input text and add special tokens
61
  input_ids = tokenizer.encode(text, return_tensors='pt')
62
 
63
- # Obtain model outputs
64
  with torch.no_grad():
65
  outputs = model(input_ids, labels=input_ids)
66
  logits = outputs.logits # logits are the model outputs before applying softmax
67
 
68
- # Shift logits and labels so that tokens are aligned:
69
  shift_logits = logits[..., :-1, :].contiguous()
70
  shift_labels = input_ids[..., 1:].contiguous()
71
 
72
- # Calculate the softmax probabilities
73
  probs = softmax(shift_logits, dim=-1)
74
 
75
- # Gather the probabilities of the actual token IDs
76
  gathered_probs = torch.gather(probs, 2, shift_labels.unsqueeze(-1)).squeeze(-1)
77
 
78
- # Compute the mean probability across the tokens
79
  mean_prob = torch.mean(gathered_probs).item()
80
 
81
  return mean_prob
 
50
  #tokenizer = GPT2Tokenizer.from_pretrained('distilgpt2')
51
 
52
 
53
+ tokenizer = AutoTokenizer.from_pretrained("meta-llama/Llama-3.2-1B")
54
+ model = AutoModelForCausalLM.from_pretrained("meta-llama/Llama-3.2-1B")
55
 
56
+
57
+ #tokenizer = GPT2Tokenizer.from_pretrained('gpt2')
58
+ #model = GPT2LMHeadModel.from_pretrained('gpt2')
59
 
60
 
61
 
 
63
  # Tokenize the input text and add special tokens
64
  input_ids = tokenizer.encode(text, return_tensors='pt')
65
 
 
66
  with torch.no_grad():
67
  outputs = model(input_ids, labels=input_ids)
68
  logits = outputs.logits # logits are the model outputs before applying softmax
69
 
70
+
71
  shift_logits = logits[..., :-1, :].contiguous()
72
  shift_labels = input_ids[..., 1:].contiguous()
73
 
74
+
75
  probs = softmax(shift_logits, dim=-1)
76
 
 
77
  gathered_probs = torch.gather(probs, 2, shift_labels.unsqueeze(-1)).squeeze(-1)
78
 
 
79
  mean_prob = torch.mean(gathered_probs).item()
80
 
81
  return mean_prob