šLink to colab
Step 1: Set up Colab Environment
- Open a new Colab notebook.
- Ensure the GPU runtime is enabled.
- Go to
Runtime
āChange runtime type
ā SetHardware accelerator
toGPU
Step 2: Install Required Libraries
!pip install torch transformers accelerate requests
This installs:
torch
for PyTorch (DeepSeek’s framework)transformers
for model supportrequests
for Dog API integration.
Step 3: Load DeepSeek-R1 Model
import torch
from transformers import AutoTokenizer, AutoModelForCausalLM
# Model ID for DeepSeek-R1
model_id = "deepseek-ai/DeepSeek-R1-Distill-Qwen-1.5B"
# Load tokenizer and model
tokenizer = AutoTokenizer.from_pretrained(model_id, trust_remote_code=True)
model = AutoModelForCausalLM.from_pretrained(
model_id,
torch_dtype=torch.float16, # Use half-precision for faster inference
device_map="auto", # Automatically map model to GPU
trust_remote_code=True
)
# Set the model to evaluation mode
model.eval()
print("DeepSeek-R1 loaded successfully!")
Step 4: Connect to the Dog API
import requests
def fetch_random_dog_image():
url = "https://dog.ceo/api/breeds/image/random"
response = requests.get(url)
if response.status_code == 200:
data = response.json()
return data["message"] # Returns the URL of the dog image
else:
return "Failed to fetch dog image"
# Test the API
dog_image_url = fetch_random_dog_image()
print(f"Random Dog Image URL: {dog_image_url}")
Step 5: Integrate DeepSeek with the Dog API
Letās create an AI agent that generates a caption or story for the random dog image:
def generate_caption(image_url):
prompt = f"Write a short caption or story about this dog: {image_url}\n\n"
inputs = tokenizer(prompt, return_tensors="pt").to(model.device)
with torch.no_grad():
outputs = model.generate(inputs["input_ids"], max_new_tokens=50, temperature=0.7)
caption = tokenizer.decode(outputs[0], skip_special_tokens=True)
return caption
# Generate caption for the dog image
caption = generate_caption(dog_image_url)
print(f"Caption: {caption}")
Learn more How to Use š³Deepseek R1 Via Colab Without Download Locally (Quick Test w/Dog API š¾)