valegro commited on
Commit
bd18104
·
verified ·
1 Parent(s): bf3f0aa

Create utils.py

Browse files
Files changed (1) hide show
  1. utils.py +22 -0
utils.py ADDED
@@ -0,0 +1,22 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import requests
3
+ import torch
4
+ import streamlit as st
5
+
6
+ def get_device():
7
+ return torch.device("cuda" if torch.cuda.is_available() else "cpu")
8
+
9
+ def download_checkpoint(url, filename):
10
+ if os.path.exists(filename):
11
+ st.info(f"Checkpoint già presente: {filename}")
12
+ return
13
+ st.warning(f"Scaricamento checkpoint SAM in corso... ({filename})")
14
+ resp = requests.get(url, stream=True)
15
+ total = int(resp.headers.get("content-length", 0))
16
+ dl = 0
17
+ bar = st.progress(0)
18
+ with open(filename, "wb") as f:
19
+ for chunk in resp.iter_content(1_048_576):
20
+ f.write(chunk); dl += len(chunk)
21
+ bar.progress(int(dl / total * 100))
22
+ st.success("Download completato!")