Record & Share Like a Pro
Free Screen Recording Tool
Made with ❤️ by developers at Codersera, forever free
3 min to read
Stable Code 3B is a state-of-the-art large language model (LLM) designed for code completion, boasting a compact size that allows it to run efficiently even on standard laptops without dedicated GPUs.
Stable Code 3B is a large language model developed by Stability AI, specifically tailored for code generation tasks. With 3 billion parameters, it delivers performance comparable to larger models while maintaining a significantly smaller footprint.
Before installing Stable Code 3B, ensure that your system meets the following requirements:
Ensure your system is up-to-date before proceeding with the installation.
sudo apt update && sudo apt upgrade -y
Install necessary libraries and tools:
sudo apt install python3-pip git -y
Stable Code 3B can be accessed via Hugging Face’s model hub. Install the required library:
pip install huggingface-hub
Create a directory for the model files:
mkdir stable-code-3b-GPTQ
Then, download the model using Hugging Face CLI:
huggingface-cli download TheBloke/stable-code-3b-GPTQ --local-dir stable-code-3b-GPTQ --local-dir-use-symlinks False
Use a virtual environment for dependency management:
python3 -m venv stablecode-env
source stablecode-env/bin/activate
For running with PyTorch:
pip install torch torchvision torchaudio --extra-index-url https://download.pytorch.org/whl/cu113
Check if the model is accessible through Python:
from huggingface_hub import hf_hub_download
model_path = hf_hub_download("TheBloke/stable-code-3b-GPTQ")
print("Model downloaded at:", model_path)
Create a Python script (e.g., run_stable_code.py
) and add the following code:
import torch
from transformers import AutoModelForCausalLM, AutoTokenizer
# Load model and tokenizer from Hugging Face Hub
model_name = "TheBloke/stable-code-3b-GPTQ"
tokenizer = AutoTokenizer.from_pretrained(model_name)
model = AutoModelForCausalLM.from_pretrained(model_name)
Use the model to generate code snippets:
def generate_code(prompt):
inputs = tokenizer(prompt, return_tensors="pt")
outputs = model.generate(**inputs)
generated_code = tokenizer.decode(outputs[0], skip_special_tokens=True)
return generated_code
# Example prompt
prompt = "Write a Python function to calculate Fibonacci numbers."
code_snippet = generate_code(prompt)
print(code_snippet)
Execute the script:
python run_stable_code.py
Installing and running Stable Code 3B on Ubuntu is a straightforward process that enables developers to leverage advanced code generation tools. By following this guide, you should be able to set up your environment successfully and begin utilizing this powerful AI model for various coding tasks.
With its compact size and impressive capabilities, Stable Code 3B is an essential tool for modern software development workflows, enhancing productivity and creativity in programming.
Need expert guidance? Connect with a top Codersera professional today!