How to Access Phi-4 Using Hugging Face?

Janvi Kumari Last Updated : 13 Jan, 2025
4 min read

Microsoft’s Phi-4 model is available on Hugging Face, offering developers a powerful tool for advanced text generation and reasoning tasks. In this article, we’ll walk you through the steps to access and use Phi-4, from creating a Hugging Face account to generating outputs with the model. We’ll also explore key features, including its optimized performance for memory and compute-constrained environments, and how you can effectively use Phi-4 in various applications.

Phi 4 and its Features

Phi-4,  is a state-of-the-art language model designed for advanced reasoning and high-quality text generation. In this Phi-4, we are having about 14 billion parameters that align well in memory and computationally limited scenarios to make it highly suitable for developers seeking to incorporate efficient artificial intelligence in their applications.

Overview of Phi-4 and Its Features
Source: Author

The Phi-4 model follows a decoder-only transformer architecture with 14 billion parameters, designed to process text through a sophisticated pipeline. At its core, the input text is first tokenized using the Tiktoken tokenizer with a vocabulary size of 100,352, which then feeds into the token embedding layer. The main transformer architecture consists of multiple layers of self-attention mechanisms capable of handling a 16K token context window (expanded from 4K during midtraining), followed by feed-forward networks.

The model was trained on approximately 10 trillion tokens with a diverse data composition: 40% synthetic data, 15% web rewrites, 15% filtered web data, 20% code data, and 10% targeted acquisitions. The training pipeline progressed through three main phases: pre-training (with 4K context), mid-training (expanded to 16K context), and fine-tuning. Post-training enhancements included Supervised Fine-tuning (SFT), Direct Preference Optimization (DPO) with pivotal token search, and judge-guided data, culminating in a language model that outputs probability distributions over its vocabulary to generate responses.

You can read more about Phi-4 here.

Features of Phi-4

  • Context Length: Phi-4 supports a context length of up to 16,000 tokens, allowing for extensive conversations or detailed text generation.
  • Safety Measures: The model incorporates robust safety features, including supervised fine-tuning and preference optimization, to ensure safe and helpful interactions.

Prerequisites

Before getting started into the process of accessing PHI 4, make sure you have the following prerequisites:

  • Hugging Face Account: You will need a Hugging Face account to access and use models from the Hub.
  • Python Environment: Ensure you have Python 3.7 or later installed on your machine.
  • Libraries: Install the necessary libraries.

Use the following commands to install them:

pip install transformers
pip install torch

How to Access Phi-4 Using Hugging Face?

Below we’ll show you how to easily access and utilize Microsoft’s Phi-4 model on Hugging Face, enabling powerful text generation and reasoning capabilities for your applications. Follow our step-by-step instructions to get started quickly and efficiently.

Step 1: Creating a Hugging Face Account

To access PHI 4 and other models, you first need to create an account on Hugging Face. Visit Hugging Face’s website and sign up. After creating an account, you’ll be able to access private and public models hosted on the platform.

Step 2: Authenticate with Hugging Face

To access private models like PHI 4, you need to authenticate your Hugging Face account. You can use the Hugging Face CLI tool to do so:

Install the CLI tool:

pip install huggingface_hub

Log in to your Hugging Face account by running the following command:

huggingface-cli login

Enter your credentials or token when prompted.

Step 3: Install Required Libraries

First, ensure you have the transformers library installed. You can install it using pip:

pip install transformers

Step 4: Load the Phi-4 Model

Once the library is installed, you can load the Phi-4 model using the pipeline API from Hugging Face. Here’s how you can do it:

import transformers

# Load the Phi-4 model
pipeline = transformers.pipeline(
    "text-generation",
    model="microsoft/phi-4",
    model_kwargs={"torch_dtype": "auto"},
    device_map="auto",
)

Step 5: Prepare Your Input

Phi-4 is optimized for chat-style prompts. You can structure your input as follows:

messages = [
    {"role": "system", "content": "You are a data scientist providing insights and explanations to a curious audience."},
    {"role": "user", "content": "How should I explain machine learning to someone new to the field?"},
]

Step 6: Generating Output

Use the pipeline to generate responses based on your input:

outputs = pipeline(messages, max_new_tokens=128)
print(outputs[0]['generated_text'])

Output:

output phi-4

Conclusion

Phi-4 is now fully accessible on Hugging Face, making it easier than ever for developers and researchers to leverage its capabilities for various applications. Whether you’re building chatbots, educational tools, or any application requiring advanced language understanding, Phi-4 stands out as a powerful option.

For further details and updates, you can refer to the official Hugging Face documentation and explore the capabilities of this innovative model.

Frequently Asked Questions

Q1. What is Phi-4?

A. Microsoft developed Phi-4, a state-of-the-art language model, to excel in advanced reasoning and high-quality text generation. Featuring 14 billion parameters, it optimizes performance for memory and compute-constrained environments.

Q2. What are the system requirements for using Phi-4?

A. You need Python 3.7 or later, and libraries such as transformers, torch, and huggingface_hub. Ensure your machine meets the necessary compute requirements, especially for handling large models.

Q3. What kind of tasks is Phi-4 suitable for?

A. Phi-4 is ideal for text generation, advanced reasoning, chatbot development, educational tools, and any application requiring extensive language understanding and generation.

Q4. What input format does Phi-4 support?

A. Microsoft optimized Phi-4 for chat-style prompts, structuring inputs as a list of messages, each with a role (e.g., system, user) and content.

Q5. What are the key features of Phi-4?

A. The key features of Phi-4 are:
14 Billion Parameters : For advanced text generation
Context Length :  Up to 16,000 tokens.
Safety Features: Supervised fine-tuning and preference optimization for safe interactions.
Efficiency: Optimized for memory and compute-constrained environments.

Hi, I am Janvi, a passionate data science enthusiast currently working at Analytics Vidhya. My journey into the world of data began with a deep curiosity about how we can extract meaningful insights from complex datasets.

Responses From Readers

Clear

We use cookies essential for this site to function well. Please click to help us improve its usefulness with additional cookies. Learn about our use of cookies in our Privacy Policy & Cookies Policy.

Show details