Spaces:
Running
Running
Jean Louis
commited on
Commit
·
01a937e
1
Parent(s):
0c3183e
Updated HTML files and CSS
Browse files- 01-prepare-python.html +74 -62
01-prepare-python.html
CHANGED
|
@@ -11,105 +11,121 @@
|
|
| 11 |
<body>
|
| 12 |
<h1>Prepare Python environment to download Hugging Face models</h1>
|
| 13 |
|
| 14 |
-
<p>This guide will
|
| 15 |
-
|
| 16 |
-
|
|
|
|
| 17 |
|
| 18 |
<p>Follow this guide, or <a href="index.html">return to the main page</a> if needed.</p>
|
| 19 |
|
| 20 |
<hr />
|
| 21 |
|
| 22 |
-
<h2>1.
|
| 23 |
|
| 24 |
-
<
|
| 25 |
|
| 26 |
-
<
|
| 27 |
-
|
| 28 |
-
<p>Run the following command in your terminal or command prompt:</p>
|
| 29 |
|
| 30 |
<p><code>bash
|
| 31 |
-
|
| 32 |
</code></p>
|
| 33 |
|
| 34 |
-
<
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 35 |
|
| 36 |
-
<p>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 37 |
|
| 38 |
<p><code>bash
|
| 39 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 40 |
</code></p>
|
| 41 |
|
| 42 |
<hr />
|
| 43 |
|
| 44 |
-
<h2>2. Install
|
| 45 |
|
| 46 |
-
<p>
|
| 47 |
|
| 48 |
-
<h3>
|
| 49 |
|
| 50 |
-
<p>Run the following command:</p>
|
| 51 |
|
| 52 |
<p><code>bash
|
| 53 |
-
pip install huggingface_hub
|
| 54 |
</code></p>
|
| 55 |
|
| 56 |
<h3>Verify Installation</h3>
|
| 57 |
|
| 58 |
-
<p>
|
| 59 |
|
| 60 |
<p><code>bash
|
| 61 |
-
|
|
|
|
| 62 |
</code></p>
|
| 63 |
|
| 64 |
<hr />
|
| 65 |
|
| 66 |
-
<h2>3.
|
| 67 |
-
|
| 68 |
-
<p>The <code>huggingface-cli</code> tool allows you to interact with the Hugging Face Hub directly from the command line.</p>
|
| 69 |
|
| 70 |
-
<
|
| 71 |
|
| 72 |
-
<h3>
|
| 73 |
|
| 74 |
-
<p>
|
| 75 |
|
| 76 |
-
<p><code>
|
| 77 |
-
huggingface-cli
|
| 78 |
</code></p>
|
| 79 |
|
| 80 |
-
<
|
| 81 |
|
| 82 |
-
<
|
| 83 |
-
|
| 84 |
-
<p><code>
|
| 85 |
-
huggingface-cli download gpt2
|
| 86 |
-
</code></p>
|
| 87 |
|
| 88 |
-
<
|
| 89 |
|
| 90 |
-
<p>
|
| 91 |
|
| 92 |
-
<
|
| 93 |
-
huggingface-cli --help
|
| 94 |
-
</code></p>
|
| 95 |
|
| 96 |
-
<
|
|
|
|
| 97 |
|
| 98 |
-
<
|
| 99 |
|
| 100 |
-
<p>
|
|
|
|
|
|
|
| 101 |
|
| 102 |
-
<
|
| 103 |
-
from transformers import pipeline
|
| 104 |
|
| 105 |
-
|
| 106 |
-
|
|
|
|
| 107 |
|
| 108 |
-
|
| 109 |
-
output = generator("Hello, how are you?", max_length=50)
|
| 110 |
-
print(output)
|
| 111 |
-
</pre>
|
| 112 |
|
|
|
|
|
|
|
| 113 |
|
| 114 |
<hr />
|
| 115 |
|
|
@@ -124,24 +140,20 @@ print(output)
|
|
| 124 |
</thead>
|
| 125 |
<tbody>
|
| 126 |
<tr>
|
| 127 |
-
<td> <code>
|
| 128 |
-
<td>
|
| 129 |
-
</tr>
|
| 130 |
-
<tr>
|
| 131 |
-
<td> <code>pip install huggingface_hub</code> </td>
|
| 132 |
-
<td> Install the <code>huggingface_hub</code> package. </td>
|
| 133 |
</tr>
|
| 134 |
<tr>
|
| 135 |
-
<td> <code>
|
| 136 |
-
<td>
|
| 137 |
</tr>
|
| 138 |
<tr>
|
| 139 |
-
<td> <code>
|
| 140 |
-
<td>
|
| 141 |
</tr>
|
| 142 |
<tr>
|
| 143 |
-
<td> <code>huggingface-cli download
|
| 144 |
-
<td> Download the
|
| 145 |
</tr>
|
| 146 |
</tbody>
|
| 147 |
</table>
|
|
@@ -149,7 +161,7 @@ print(output)
|
|
| 149 |
|
| 150 |
<hr />
|
| 151 |
|
| 152 |
-
<p>Now you’re ready to use
|
| 153 |
|
| 154 |
<h1>Proceed to next step</h1>
|
| 155 |
|
|
|
|
| 11 |
<body>
|
| 12 |
<h1>Prepare Python environment to download Hugging Face models</h1>
|
| 13 |
|
| 14 |
+
<p>This guide will walk you through setting up a Python environment named
|
| 15 |
+
<strong><code>empower</code></strong>, installing the necessary Hugging Face packages, and
|
| 16 |
+
downloading and using the <strong><code>microsoft/Phi-4-mini-instruct</code></strong> model on
|
| 17 |
+
a GNU/Linux system.</p>
|
| 18 |
|
| 19 |
<p>Follow this guide, or <a href="index.html">return to the main page</a> if needed.</p>
|
| 20 |
|
| 21 |
<hr />
|
| 22 |
|
| 23 |
+
<h2>1. Set Up Python Environment</h2>
|
| 24 |
|
| 25 |
+
<h3>Check Python Installation</h3>
|
| 26 |
|
| 27 |
+
<p>Ensure Python 3 is installed by running:</p>
|
|
|
|
|
|
|
| 28 |
|
| 29 |
<p><code>bash
|
| 30 |
+
python3 --version
|
| 31 |
</code></p>
|
| 32 |
|
| 33 |
+
<p>If Python is not installed, install it using your package manager. For example:</p>
|
| 34 |
+
|
| 35 |
+
<ul>
|
| 36 |
+
<li><p>On Debian/Ubuntu:</p>
|
| 37 |
+
|
| 38 |
+
<p><code>bash
|
| 39 |
+
sudo apt update
|
| 40 |
+
sudo apt install python3 python3-venv
|
| 41 |
+
</code></p></li>
|
| 42 |
+
<li><p>On Fedora:</p>
|
| 43 |
|
| 44 |
+
<p><code>bash
|
| 45 |
+
sudo dnf install python3
|
| 46 |
+
</code></p></li>
|
| 47 |
+
</ul>
|
| 48 |
+
|
| 49 |
+
|
| 50 |
+
<h3>Create a Virtual Environment</h3>
|
| 51 |
+
|
| 52 |
+
<p>Create a virtual environment named <strong><code>empower</code></strong>:</p>
|
| 53 |
|
| 54 |
<p><code>bash
|
| 55 |
+
python3 -m venv empower
|
| 56 |
+
</code></p>
|
| 57 |
+
|
| 58 |
+
<p>Activate the virtual environment:
|
| 59 |
+
<code>bash
|
| 60 |
+
source empower/bin/activate
|
| 61 |
</code></p>
|
| 62 |
|
| 63 |
<hr />
|
| 64 |
|
| 65 |
+
<h2>2. Install Hugging Face Packages</h2>
|
| 66 |
|
| 67 |
+
<p>Install the necessary Hugging Face packages to interact with models and the Hugging Face Hub.</p>
|
| 68 |
|
| 69 |
+
<h3>Install <code>transformers</code> and <code>huggingface_hub</code></h3>
|
| 70 |
|
| 71 |
+
<p>Run the following command to install both packages:</p>
|
| 72 |
|
| 73 |
<p><code>bash
|
| 74 |
+
pip install transformers huggingface_hub
|
| 75 |
</code></p>
|
| 76 |
|
| 77 |
<h3>Verify Installation</h3>
|
| 78 |
|
| 79 |
+
<p>Check if the packages are installed correctly:</p>
|
| 80 |
|
| 81 |
<p><code>bash
|
| 82 |
+
python3 -c "from transformers import pipeline; print('Transformers installed successfully!')"
|
| 83 |
+
python3 -c "from huggingface_hub import HfApi; print('Hugging Face Hub installed successfully!')"
|
| 84 |
</code></p>
|
| 85 |
|
| 86 |
<hr />
|
| 87 |
|
| 88 |
+
<h2>3. Download the <code>microsoft/Phi-4-mini-instruct</code> Model</h2>
|
|
|
|
|
|
|
| 89 |
|
| 90 |
+
<p>To download and use the <strong><code>microsoft/Phi-4-mini-instruct</code></strong> model, follow these steps.</p>
|
| 91 |
|
| 92 |
+
<h3>Using <code>huggingface-cli</code> to Download the Model</h3>
|
| 93 |
|
| 94 |
+
<p>Run the following command to download the model:</p>
|
| 95 |
|
| 96 |
+
<p><code>bash
|
| 97 |
+
huggingface-cli download microsoft/Phi-4-mini-instruct
|
| 98 |
</code></p>
|
| 99 |
|
| 100 |
+
<p>This will download the model files to your current directory.</p>
|
| 101 |
|
| 102 |
+
<hr />
|
|
|
|
|
|
|
|
|
|
|
|
|
| 103 |
|
| 104 |
+
<h2>4. Load and Use the Model in Python</h2>
|
| 105 |
|
| 106 |
+
<p>Once the model is downloaded, you can load and use it in your Python code.</p>
|
| 107 |
|
| 108 |
+
<h3>Example Code</h3>
|
|
|
|
|
|
|
| 109 |
|
| 110 |
+
<p>```python
|
| 111 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer</p>
|
| 112 |
|
| 113 |
+
<h1>Load the model and tokenizer</h1>
|
| 114 |
|
| 115 |
+
<p>model_name = “microsoft/Phi-4-mini-instruct”
|
| 116 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 117 |
+
model = AutoModelForCausalLM.from_pretrained(model_name)</p>
|
| 118 |
|
| 119 |
+
<h1>Generate text</h1>
|
|
|
|
| 120 |
|
| 121 |
+
<p>input_text = “What is the capital of France?”
|
| 122 |
+
inputs = tokenizer(input_text, return_tensors=“pt”)
|
| 123 |
+
outputs = model.generate(**inputs, max_length=50)</p>
|
| 124 |
|
| 125 |
+
<h1>Decode and print the output</h1>
|
|
|
|
|
|
|
|
|
|
| 126 |
|
| 127 |
+
<p>print(tokenizer.decode(outputs[0], skip_special_tokens=True))
|
| 128 |
+
```</p>
|
| 129 |
|
| 130 |
<hr />
|
| 131 |
|
|
|
|
| 140 |
</thead>
|
| 141 |
<tbody>
|
| 142 |
<tr>
|
| 143 |
+
<td> <code>python3 -m venv empower</code> </td>
|
| 144 |
+
<td> Create a virtual environment named <code>empower</code>. </td>
|
|
|
|
|
|
|
|
|
|
|
|
|
| 145 |
</tr>
|
| 146 |
<tr>
|
| 147 |
+
<td> <code>source empower/bin/activate</code> </td>
|
| 148 |
+
<td> Activate the <code>empower</code> environment. </td>
|
| 149 |
</tr>
|
| 150 |
<tr>
|
| 151 |
+
<td> <code>pip install transformers huggingface_hub</code> </td>
|
| 152 |
+
<td> Install Hugging Face packages. </td>
|
| 153 |
</tr>
|
| 154 |
<tr>
|
| 155 |
+
<td> <code>huggingface-cli download microsoft/Phi-4-mini-instruct</code> </td>
|
| 156 |
+
<td> Download the model. </td>
|
| 157 |
</tr>
|
| 158 |
</tbody>
|
| 159 |
</table>
|
|
|
|
| 161 |
|
| 162 |
<hr />
|
| 163 |
|
| 164 |
+
<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>
|
| 165 |
|
| 166 |
<h1>Proceed to next step</h1>
|
| 167 |
|