Spaces:
Runtime error
Runtime error
Commit ·
d36dc81
1
Parent(s): ad04391
updated requirement
Browse files9 unmodified lines101112131415161718192021222324259 unmodified linesif not HF_TOKEN:raise ValueError("Missing Hugging Face token. Set HUGGINGFACE_TOKEN as an environment variable.")# Load tokenizer and model with authenticationtokenizer = AutoTokenizer.from_pretrained(MODEL_NAME, token=HF_TOKEN)model = AutoModelForCausalLM.from_pretrained(MODEL_NAME, token=HF_TOKEN, device_map="auto")def generate_code(prompt: str, max_tokens: int = 256):"""Generates code based on the input prompt."""if not prompt.strip():return "Error: Empty prompt provided."device = "cuda" if torch.cuda.is_available() else "cpu"inputs = tokenizer(prompt, return_tensors="pt").to(device)output = model.generate(**inputs, max_new_tokens=max_tokens)return tokenizer.decode(output[0], skip_special_tokens=True)9 unmodified lines1011121314151617181920212223242526272829303132333435369 unmodified linesif not HF_TOKEN:raise ValueError("Missing Hugging Face token. Set HUGGINGFACE_TOKEN as an environment variable.")# Set devicedevice = "cuda" if torch.cuda.is_available() else "cpu"# Load tokenizer with authenticationtokenizer = AutoTokenizer.from_pretrained(MODEL_NAME, token=HF_TOKEN)# Load model with optimizationsmodel = AutoModelForCausalLM.from_pretrained(MODEL_NAME,token=HF_TOKEN,torch_dtype=torch.float16, # Reduce memory usagelow_cpu_mem_usage=True, # Optimize loadingdevice_map="auto", # Automatic device placementoffload_folder="offload" # Offload to disk if needed).to(device)def generate_code(prompt: str, max_tokens: int = 256):"""Generates code based on the input prompt."""if not prompt.strip():return "Error: Empty prompt provided."inputs = tokenizer(prompt, return_tensors="pt").to(device)output = model.generate(**inputs, max_new_tokens=max_tokens)return tokenizer.decode(output[0], skip_special_tokens=True)
model.py+14-3