File size: 4,407 Bytes
9992bad
 
 
 
 
 
 
 
 
 
 
 
 
01a937e
 
 
 
9992bad
386c510
 
9992bad
 
01a937e
9992bad
01a937e
9992bad
01a937e
9992bad
 
01a937e
9992bad
 
01a937e
 
 
 
 
 
 
 
 
 
9992bad
01a937e
 
 
 
 
 
 
 
 
9992bad
 
01a937e
 
 
 
 
 
9992bad
 
 
 
01a937e
9992bad
01a937e
9992bad
01a937e
9992bad
01a937e
9992bad
 
01a937e
9992bad
 
 
 
01a937e
9992bad
 
01a937e
 
9992bad
 
 
 
01a937e
9992bad
01a937e
9992bad
01a937e
9992bad
01a937e
9992bad
01a937e
 
9992bad
 
01a937e
9992bad
01a937e
9992bad
01a937e
9992bad
01a937e
9992bad
01a937e
9992bad
01a937e
 
9992bad
01a937e
9992bad
01a937e
 
 
9992bad
01a937e
9992bad
01a937e
 
 
9992bad
01a937e
9992bad
01a937e
 
9992bad
 
 
 
 
 
 
 
 
 
 
 
 
 
01a937e
 
9992bad
 
01a937e
 
9992bad
 
01a937e
 
9992bad
 
01a937e
 
9992bad
 
 
 
 
 
 
01a937e
b761d6e
 
 
 
9992bad
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
<!doctype html public "-//W3C//DTD HTML 4.0 Transitional //EN">
<html>
<head>
  <meta name="GENERATOR" content="mkd2html 2.2.7 GITHUB_CHECKBOX">
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  <link rel="stylesheet"
        type="text/css"
        href="header.css" />
  <title></title>
</head>
<body>
<h1>Prepare Python environment to download Hugging Face models</h1>

<p>This guide will walk you through setting up a Python environment named
<strong><code>empower</code></strong>, installing the necessary Hugging Face packages, and
downloading and using the <strong><code>microsoft/Phi-4-mini-instruct</code></strong> model on
a GNU/Linux system.</p>

<p>Follow this guide, or <a href="index.html">return to the main page</a> if needed.</p>

<hr />

<h2>1. Set Up Python Environment</h2>

<h3>Check Python Installation</h3>

<p>Ensure Python 3 is installed by running:</p>

<p><code>bash
python3 --version
</code></p>

<p>If Python is not installed, install it using your package manager. For example:</p>

<ul>
<li><p>On Debian/Ubuntu:</p>

<p><code>bash
sudo apt update
sudo apt install python3 python3-venv
</code></p></li>
<li><p>On Fedora:</p>

<p><code>bash
sudo dnf install python3
</code></p></li>
</ul>


<h3>Create a Virtual Environment</h3>

<p>Create a virtual environment named <strong><code>empower</code></strong>:</p>

<p><code>bash
python3 -m venv empower
</code></p>

<p>Activate the virtual environment:
<code>bash
source empower/bin/activate
</code></p>

<hr />

<h2>2. Install Hugging Face Packages</h2>

<p>Install the necessary Hugging Face packages to interact with models and the Hugging Face Hub.</p>

<h3>Install <code>transformers</code> and <code>huggingface_hub</code></h3>

<p>Run the following command to install both packages:</p>

<p><code>bash
pip install transformers huggingface_hub
</code></p>

<h3>Verify Installation</h3>

<p>Check if the packages are installed correctly:</p>

<p><code>bash
python3 -c "from transformers import pipeline; print('Transformers installed successfully!')"
python3 -c "from huggingface_hub import HfApi; print('Hugging Face Hub installed successfully!')"
</code></p>

<hr />

<h2>3. Download the <code>microsoft/Phi-4-mini-instruct</code> Model</h2>

<p>To download and use the <strong><code>microsoft/Phi-4-mini-instruct</code></strong> model, follow these steps.</p>

<h3>Using <code>huggingface-cli</code> to Download the Model</h3>

<p>Run the following command to download the model:</p>

<p><code>bash
huggingface-cli download microsoft/Phi-4-mini-instruct
</code></p>

<p>This will download the model files to your current directory.</p>

<hr />

<h2>4. Load and Use the Model in Python</h2>

<p>Once the model is downloaded, you can load and use it in your Python code.</p>

<h3>Example Code</h3>

<p>```python
from transformers import AutoModelForCausalLM, AutoTokenizer</p>

<h1>Load the model and tokenizer</h1>

<p>model_name = &ldquo;microsoft/Phi-4-mini-instruct&rdquo;
tokenizer = AutoTokenizer.from_pretrained(model_name)
model = AutoModelForCausalLM.from_pretrained(model_name)</p>

<h1>Generate text</h1>

<p>input_text = &ldquo;What is the capital of France?&rdquo;
inputs = tokenizer(input_text, return_tensors=&ldquo;pt&rdquo;)
outputs = model.generate(**inputs, max_length=50)</p>

<h1>Decode and print the output</h1>

<p>print(tokenizer.decode(outputs[0], skip_special_tokens=True))
```</p>

<hr />

<h2>5. Summary of Commands</h2>

<table>
<thead>
<tr>
<th> Command                          </th>
<th> Description                                      </th>
</tr>
</thead>
<tbody>
<tr>
<td> <code>python3 -m venv empower</code>        </td>
<td> Create a virtual environment named <code>empower</code>.   </td>
</tr>
<tr>
<td> <code>source empower/bin/activate</code>    </td>
<td> Activate the <code>empower</code> environment.             </td>
</tr>
<tr>
<td> <code>pip install transformers huggingface_hub</code> </td>
<td> Install Hugging Face packages.          </td>
</tr>
<tr>
<td> <code>huggingface-cli download microsoft/Phi-4-mini-instruct</code> </td>
<td> Download the model.          </td>
</tr>
</tbody>
</table>


<hr />

<p>Now you’re ready to use the <strong><code>microsoft/Phi-4-mini-instruct</code></strong> model in your Python projects on GNU/Linux! 🚀</p>

<h1>Proceed to next step</h1>

<p><a href="index.html">Proceed to next step.</a></p>
</body>
</html>