modelscope 0.1.5

A command-line interface tool for downloading models from Modelscope, which can also be used as a library
Documentation
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
use anyhow::{Context, bail};
use futures_util::StreamExt;
use indicatif::{MultiProgress, ProgressBar, ProgressStyle};
use serde::{Deserialize, Serialize};
use std::env::home_dir;
use std::fs;
use std::io::{BufWriter, Seek, Write};
use std::path::{Path, PathBuf};
use std::sync::Arc;

const FILES_URL: &str = "https://modelscope.cn/api/v1/models/<model_id>/repo/files?Recursive=true";
const DOWNLOAD_URL: &str = "https://modelscope.cn/models/<model_id>/resolve/master/<path>";
const LOGIN_URL: &str = "https://modelscope.cn/api/v1/login";
const DIR: &str = ".modelscope";
const COOKIES_FILE: &str = "cookies";

const UA: (&str, &str) = (
    "User-Agent",
    "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.90 Safari/537.36",
);
pub struct ModelScope;

#[derive(Debug, Deserialize)]
struct ModelScopeResponse {
    #[serde(rename = "Code")]
    #[allow(unused)]
    code: i64,
    #[serde(rename = "Success")]
    success: bool,
    #[serde(rename = "Message")]
    message: String,
    #[serde(rename = "Data")]
    data: Option<ModelScopeResponseData>,
}

#[derive(Debug, Deserialize)]
struct ModelScopeResponseData {
    #[serde(rename = "Files")]
    files: Vec<RepoFile>,
}
#[derive(Debug, Deserialize)]
struct RepoFile {
    #[serde(rename = "Name")]
    name: String,
    #[serde(rename = "Path")]
    path: String,
    #[serde(rename = "Size")]
    size: u64,
    #[serde(rename = "Sha256")]
    #[allow(unused)]
    sha256: String,
    #[serde(rename = "Type")]
    r#type: String,
}

pub struct DownloadOptions {
    /// Specify the files to download
    pub files: Option<Vec<String>>,
}

const BAR_STYLE: &str = "{msg:<30} {bar} {decimal_bytes:<10} / {decimal_total_bytes:<10} {decimal_bytes_per_sec:<12} {percent:<3}%  {eta_precise}";

impl ModelScope {
    async fn get_client() -> anyhow::Result<reqwest::Client> {
        let client = reqwest::Client::builder().connect_timeout(std::time::Duration::from_secs(10));
        let mut default_headers = reqwest::header::HeaderMap::new();
        if let Some(cookies) = Self::get_cookies()? {
            default_headers.insert("Cookie", cookies.parse()?);
        }
        let client = client.default_headers(default_headers);
        Ok(client.build()?)
    }

    /// Download model
    pub async fn download(model_id: &str, save_dir: impl Into<PathBuf>) -> anyhow::Result<()> {
        Self::download_with_options(model_id, save_dir, DownloadOptions { files: None }).await
    }

    /// Download the model with specified options
    pub async fn download_with_options(
        model_id: &str,
        save_dir: impl Into<PathBuf>,
        options: DownloadOptions,
    ) -> anyhow::Result<()> {
        // Model root dir
        let save_dir = save_dir.into();
        fs::create_dir_all(&save_dir)?;

        // Model save dir, like <save_dir>/<model_id>
        let model_dir = save_dir.join(model_id);

        println!();
        println!("Downloading model {} to: {}", model_id, model_dir.display());
        println!();

        fs::create_dir_all(&model_dir)?;

        let files_url = FILES_URL.replace("<model_id>", model_id);

        let client = Arc::new(Self::get_client().await?);

        let resp = client.get(files_url).send().await?;

        if !resp.status().is_success() {
            bail!(
                "Failed to get model files: {}\nTip: Maybe the model ID is incorrect or login is required",
                resp.text().await?
            );
        }

        let response = resp.json::<ModelScopeResponse>().await?;
        if !response.success {
            bail!("Failed to get model files: {}", response.message);
        }

        let data = response.data.unwrap();
        let repo_files = data.files;

        // Add the incoming model save path to the known model paths
        // This is used when using the list command
        Config::append_save_dir(&save_dir)?;

        let mut tasks = Vec::new();
        let bars = MultiProgress::new();

        for repo_file in repo_files.into_iter().filter(|f| f.r#type == "blob") {
            if let Some(files) = &options.files
                && !files.contains(&repo_file.path)
            {
                continue;
            }
            let model_id = model_id.to_string();
            let client = client.clone();
            let save_dir = model_dir.clone();

            let bar = ProgressBar::new(repo_file.size);
            let style = ProgressStyle::default_bar().template(BAR_STYLE)?;
            bar.set_style(style);

            bars.add(bar.clone());

            let task = tokio::spawn(async move {
                let res = Self::download_file(client, model_id, repo_file, save_dir, bar).await;
                if let Err(e) = res {
                    bail!("Error downloading file: {}", e);
                }
                Ok::<(), anyhow::Error>(())
            });

            tasks.push(task);
        }
        for task in tasks {
            task.await??;
        }

        Ok(())
    }

    async fn download_file(
        client: Arc<reqwest::Client>,
        model_id: String,
        repo_file: RepoFile,
        save_dir: PathBuf,
        bar: ProgressBar,
    ) -> anyhow::Result<()> {
        let path = &repo_file.path;
        let name = &repo_file.name;

        bar.set_message(name.clone());

        let file_path = save_dir.join(path);
        if let Some(parent) = file_path.parent() {
            fs::create_dir_all(parent)?;
        }

        let mut existing_size = 0;
        let mut file_options = fs::OpenOptions::new();
        file_options.write(true).create(true);

        if file_path.exists() {
            if let Ok(metadata) = fs::metadata(&file_path) {
                existing_size = metadata.len();
                file_options.append(true);
            }
        } else {
            file_options.truncate(true);
        }

        let mut file = BufWriter::new(file_options.open(&file_path)?);

        // Set progress bar initial position
        bar.set_position(existing_size);
        bar.set_length(repo_file.size);

        let url = DOWNLOAD_URL
            .replace("<model_id>", &model_id)
            .replace("<path>", path);

        let mut rb = client.get(&url).header(UA.0, UA.1);

        // Already downloaded, just return ok.
        // If file size equal repo file size, maybe check sha256
        // But I think the probability of files having the same number of bytes is relatively low, so I won't check here. 🙊
        if existing_size == repo_file.size {
            bar.finish();
            return Ok(());
        }

        // Resume download
        if existing_size < repo_file.size {
            rb = rb.header("Range", format!("bytes={}-", existing_size));
        }

        let response = rb.send().await?;

        let status = response.status();

        // Server doesn't support resume download, re-downloading from beginning
        // Or existing file size is larger than repo size, re-downloading from beginning
        if status == reqwest::StatusCode::OK && existing_size > 0 || existing_size > repo_file.size
        {
            file.rewind()?;
            file.get_ref().set_len(0)?;
            bar.set_position(0);
        }

        // If status is not success or partial content, bail
        if !response.status().is_success()
            && response.status() != reqwest::StatusCode::PARTIAL_CONTENT
        {
            bail!(
                "Failed to download file {}: HTTP {}",
                name,
                response.status()
            );
        }

        let mut stream = response.bytes_stream();

        while let Some(item) = stream.next().await {
            let chunk = item?;
            file.write_all(&chunk)?;
            bar.inc(chunk.len() as u64);
        }

        file.flush()?;

        bar.finish();

        Ok(())
    }

    pub async fn login(token: &str) -> anyhow::Result<()> {
        println!("Logging in...");
        let client = Self::get_client().await?;
        let resp = client
            .post(LOGIN_URL)
            .json(&serde_json::json!({
                "AccessToken": token
            }))
            .send()
            .await?;

        let status = resp.status();

        if !status.is_success() {
            bail!("Failed to login: {}", resp.text().await?);
        }

        let cookies: serde_json::Value = resp
            .cookies()
            .map(|cookie| (cookie.name().to_string(), cookie.value().to_string()))
            .collect();

        let dir = Dirs::config_dir()?;

        let cookies_file = dir.join(COOKIES_FILE);
        fs::write(cookies_file, cookies.to_string())?;

        println!("Login successful.");

        Ok(())
    }

    fn get_cookies() -> anyhow::Result<Option<String>> {
        let cookies_file = Dirs::config_dir()?.join(COOKIES_FILE);

        if cookies_file.exists() {
            let cookies = fs::read_to_string(cookies_file)?;
            let cookies: serde_json::Value = serde_json::from_str(&cookies)?;

            let cookies = cookies
                .as_object()
                .context("Failed to parse cookies")?
                .iter()
                .map(|(k, v)| format!("{}={}", k, v.as_str().unwrap_or_default()))
                .collect::<Vec<_>>()
                .join("; ");
            return Ok(Some(cookies));
        }

        Ok(None)
    }

    pub async fn logout() -> anyhow::Result<()> {
        // May just delete cookies file
        let cookies_file = Dirs::config_dir()?.join(COOKIES_FILE);
        if cookies_file.exists() {
            fs::remove_file(cookies_file)?;
        }
        println!("Logged out.");
        Ok(())
    }

    pub async fn list() -> anyhow::Result<Vec<(String, String)>> {
        // Known model save paths
        let model_paths = Config::get_known_save_dirs()?;

        let mut models = vec![];
        for model_path in model_paths {
            for dir in fs::read_dir(model_path)? {
                let dir = dir?;
                // This level is the model vendor, and the next level is the model name
                if dir.file_type()?.is_dir() {
                    for entry in fs::read_dir(dir.path())? {
                        let entry = entry?;
                        if entry.file_type()?.is_dir() {
                            models.push((
                                // Model ID
                                format!(
                                    "{}/{}",
                                    dir.file_name().display(),
                                    entry.file_name().display()
                                ),
                                // Model path
                                dir.path().display().to_string(),
                            ));
                        }
                    }
                }
            }
        }
        Ok(models)
    }
}

struct Dirs {}
impl Dirs {
    fn base_dir() -> anyhow::Result<PathBuf> {
        let base_dir = home_dir()
            .context("Failed to get home directory")?
            .join(DIR);
        if !base_dir.exists() {
            fs::create_dir_all(&base_dir)?;
        }
        Ok(base_dir)
    }

    fn config_dir() -> anyhow::Result<PathBuf> {
        let config_dir = Self::base_dir()?.join("config");
        if !config_dir.exists() {
            fs::create_dir_all(&config_dir)?;
        }
        Ok(config_dir)
    }

    #[allow(unused)]
    pub fn model_dir() -> anyhow::Result<PathBuf> {
        let model_dir = Self::base_dir()?.join("models");
        if !model_dir.exists() {
            fs::create_dir_all(&model_dir)?;
        }
        Ok(model_dir)
    }
}

#[derive(Debug, Serialize, Deserialize)]
struct Config {
    known_save_dirs: Vec<PathBuf>,
}

impl Config {
    const KNOWN_SAVE_DIRS: &'static str = "known_save_dirs";
    fn append_save_dir(dir: &Path) -> anyhow::Result<()> {
        let f = Dirs::config_dir()?.join(Self::KNOWN_SAVE_DIRS);

        // Get existing known save dirs
        let mut known_save_dirs = Self::get_known_save_dirs()?;

        // Canonicalize the directory
        let dir = dir.canonicalize()?;

        if known_save_dirs.contains(&dir) {
            return Ok(());
        }

        known_save_dirs.push(dir);
        fs::write(
            f,
            known_save_dirs
                .iter()
                .filter(|p| p.exists())
                .map(|p| p.display().to_string())
                .filter(|s| !s.trim().is_empty())
                .collect::<Vec<String>>()
                .join("\n"),
        )?;

        Ok(())
    }

    fn get_known_save_dirs() -> anyhow::Result<Vec<PathBuf>> {
        let config_dir = Dirs::config_dir()?;
        if !config_dir.exists() {
            fs::create_dir_all(&config_dir)?;
            return Ok(vec![]);
        }

        let f = config_dir.join(Self::KNOWN_SAVE_DIRS);
        if !f.exists() {
            return Ok(vec![]);
        }

        let paths = fs::read_to_string(f)?
            .lines()
            .map(PathBuf::from)
            // Filter out non-existent paths
            // These paths will be cleaned up when append_save_dir is called
            .filter(|p| p.exists())
            .collect::<Vec<_>>();

        Ok(paths)
    }
}