Share
Explore

Programming Models with HuggingFace Spaces

Learning outcomes:
Using huggingface spaces to create my own model foundationed on DeepSeek R1

Creating Your Own Model Based on DeepSeek R1 in Hugging Face Spaces

This Lab will walk you through the process of creating your own model foundation based on DeepSeek R1 using Hugging Face Spaces.
DeepSeek R1 is a powerful open-source reasoning model that can be fine-tuned or adapted for your specific use case.

Step 1: Set Up Your Hugging Face Account

Go to and create an account if you don’t already have one.
Once logged in, navigate to the Spaces section.

Step 2: Fork or Clone an Existing Space

Search for DeepSeek R1 in the Spaces section (e.g., "DeepSeek R1 WebGPU" or "DeepSeek R1 Chat Assistant").
Open the space you want to use as a base.
Click the "Duplicate this Space" button (usually found in the top-right corner).
This will create a copy of the space in your account.

Step 3: Customize the Space

Access the Code Files:
In your duplicated space, click on the "Files" tab to view the code and configuration files.
You’ll see files like app.py, requirements.txt, and model configuration files.
Modify the Code:
Open app.py (or the main script file) to customize the logic of the application.
For example, you can change the prompts, add new features, or modify the user interface.
Update the Model:
If you want to fine-tune the DeepSeek R1 model, replace the pre-trained model with your own fine-tuned version.
You can upload your fine-tuned model to Hugging Face’s Model Hub and reference it in the code.

Step 4: Fine-Tune DeepSeek R1

Download the Model:
Clone the DeepSeek R1 repository from Hugging Face or GitHub (e.g., ).
Prepare Your Dataset:
Collect and preprocess the dataset you want to use for fine-tuning.
Ensure the dataset is in a format compatible with Hugging Face’s datasets library.
Fine-Tune the Model:
Use Hugging Face’s transformers library to fine-tune the model. Example code:
python
from transformers import AutoModelForCausalLM, AutoTokenizer, Trainer, TrainingArguments

model_name = "deepseek-ai/DeepSeek-R1"
tokenizer = AutoTokenizer.from_pretrained(model_name)
model = AutoModelForCausalLM.from_pretrained(model_name)

# Load your dataset
from datasets import load_dataset
dataset = load_dataset("your_dataset_name")

# Define training arguments
training_args = TrainingArguments(
output_dir="./results",
evaluation_strategy="epoch",
learning_rate=2e-5,
per_device_train_batch_size=4,
num_train_epochs=3,
save_steps=10_000,
save_total_limit=2,
)

trainer = Trainer(
model=model,
args=training_args,
train_dataset=dataset["train"],
eval_dataset=dataset["validation"],
)

trainer.train()

Upload the Fine-Tuned Model:
Once fine-tuning is complete, upload your model to Hugging Face’s Model Hub using the transformers-cli tool:
bash
transformers-cli login
transformers-cli upload ./path_to_your_model

Step 5: Deploy Your Space

Test Locally:
Run your space locally to ensure everything works as expected. Use the gradio library to test the interface:
bash
python app.py

Push to Hugging Face:
Once satisfied, push your changes to Hugging Face Spaces. Use the following commands:
bash
git add .
git commit -m "Customized DeepSeek R1 Space"
git push

Set Runtime Environment:
In the Settings tab of your space, configure the runtime environment (e.g., CPU, GPU, or WebGPU) based on your model’s requirements.

Step 6: Share and Iterate

Share your space with others by providing the link.
Gather feedback and iterate on your model and space to improve its functionality.
By following these steps, you can create a customized model and deploy it on Hugging Face Spaces, leveraging the power of DeepSeek R1 for your specific use case.
Want to print your doc?
This is not the way.
Try clicking the ⋯ next to your doc name or using a keyboard shortcut (
CtrlP
) instead.