Spaces:
Sleeping
Sleeping
Commit
Β·
812fce0
1
Parent(s):
948bd8f
updated model.py
Browse files
model.py
CHANGED
|
@@ -2,7 +2,7 @@ import os
|
|
| 2 |
from transformers import AutoModelForCausalLM, AutoTokenizer
|
| 3 |
import torch
|
| 4 |
|
| 5 |
-
MODEL_NAME = "bigcode/starcoderbase-
|
| 6 |
HF_TOKEN = os.getenv("HUGGINGFACE_TOKEN")
|
| 7 |
|
| 8 |
device = "cpu"
|
|
@@ -10,39 +10,40 @@ device = "cpu"
|
|
| 10 |
# Load tokenizer and model
|
| 11 |
tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME, token=HF_TOKEN)
|
| 12 |
|
|
|
|
| 13 |
if tokenizer.pad_token is None:
|
| 14 |
-
tokenizer.pad_token = tokenizer.eos_token
|
| 15 |
|
| 16 |
model = AutoModelForCausalLM.from_pretrained(
|
| 17 |
MODEL_NAME,
|
| 18 |
token=HF_TOKEN,
|
| 19 |
-
torch_dtype=torch.float32,
|
| 20 |
trust_remote_code=True
|
| 21 |
).to(device)
|
| 22 |
|
| 23 |
def generate_code(prompt: str, max_tokens: int = 256):
|
| 24 |
-
#
|
| 25 |
-
formatted_prompt = f"{prompt}\n### Code:\n" # Hint that code follows
|
| 26 |
|
| 27 |
inputs = tokenizer(
|
| 28 |
formatted_prompt,
|
| 29 |
return_tensors="pt",
|
|
|
|
| 30 |
truncation=True,
|
| 31 |
-
max_length=
|
| 32 |
).to(device)
|
| 33 |
|
| 34 |
output = model.generate(
|
| 35 |
**inputs,
|
| 36 |
max_new_tokens=max_tokens,
|
| 37 |
pad_token_id=tokenizer.pad_token_id,
|
| 38 |
-
do_sample=True,
|
| 39 |
-
top_p=0.
|
| 40 |
-
temperature=0.
|
| 41 |
)
|
| 42 |
|
| 43 |
generated_code = tokenizer.decode(output[0], skip_special_tokens=True)
|
| 44 |
|
| 45 |
-
#
|
| 46 |
if generated_code.startswith(formatted_prompt):
|
| 47 |
generated_code = generated_code[len(formatted_prompt):]
|
| 48 |
|
|
|
|
| 2 |
from transformers import AutoModelForCausalLM, AutoTokenizer
|
| 3 |
import torch
|
| 4 |
|
| 5 |
+
MODEL_NAME = "bigcode/starcoderbase-3b"
|
| 6 |
HF_TOKEN = os.getenv("HUGGINGFACE_TOKEN")
|
| 7 |
|
| 8 |
device = "cpu"
|
|
|
|
| 10 |
# Load tokenizer and model
|
| 11 |
tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME, token=HF_TOKEN)
|
| 12 |
|
| 13 |
+
# Ensure the tokenizer has a pad token set
|
| 14 |
if tokenizer.pad_token is None:
|
| 15 |
+
tokenizer.pad_token = tokenizer.eos_token # Set pad_token to eos_token
|
| 16 |
|
| 17 |
model = AutoModelForCausalLM.from_pretrained(
|
| 18 |
MODEL_NAME,
|
| 19 |
token=HF_TOKEN,
|
| 20 |
+
torch_dtype=torch.float32, # Ensure compatibility with CPU
|
| 21 |
trust_remote_code=True
|
| 22 |
).to(device)
|
| 23 |
|
| 24 |
def generate_code(prompt: str, max_tokens: int = 256):
|
| 25 |
+
formatted_prompt = f"# Python\n{prompt}\n\n" # Ensure the model understands it's code
|
|
|
|
| 26 |
|
| 27 |
inputs = tokenizer(
|
| 28 |
formatted_prompt,
|
| 29 |
return_tensors="pt",
|
| 30 |
+
padding=True,
|
| 31 |
truncation=True,
|
| 32 |
+
max_length=1024 # Explicit max length to prevent issues
|
| 33 |
).to(device)
|
| 34 |
|
| 35 |
output = model.generate(
|
| 36 |
**inputs,
|
| 37 |
max_new_tokens=max_tokens,
|
| 38 |
pad_token_id=tokenizer.pad_token_id,
|
| 39 |
+
do_sample=True, # Enable randomness for better outputs
|
| 40 |
+
top_p=0.95, # Nucleus sampling to improve generation
|
| 41 |
+
temperature=0.7 # Control creativity
|
| 42 |
)
|
| 43 |
|
| 44 |
generated_code = tokenizer.decode(output[0], skip_special_tokens=True)
|
| 45 |
|
| 46 |
+
# Clean the output: remove the repeated prompt at the start
|
| 47 |
if generated_code.startswith(formatted_prompt):
|
| 48 |
generated_code = generated_code[len(formatted_prompt):]
|
| 49 |
|