Spaces:
Running
Running
feat: script d'installation automatique du modèle depuis Google Drive (download + extraction)
Browse files- install_agrilens.py +85 -0
install_agrilens.py
ADDED
@@ -0,0 +1,85 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import sys
|
3 |
+
import subprocess
|
4 |
+
import tarfile
|
5 |
+
import requests
|
6 |
+
|
7 |
+
VENV_DIR = "venv"
|
8 |
+
REQUIREMENTS = "requirements.txt"
|
9 |
+
|
10 |
+
FR = sys.platform.startswith("win")
|
11 |
+
|
12 |
+
# 1. Créer l'environnement virtuel si besoin
|
13 |
+
def create_venv():
|
14 |
+
if not os.path.isdir(VENV_DIR):
|
15 |
+
print("[INFO] Création de l'environnement virtuel...")
|
16 |
+
subprocess.check_call([sys.executable, "-m", "venv", VENV_DIR])
|
17 |
+
else:
|
18 |
+
print("[INFO] Environnement virtuel déjà présent.")
|
19 |
+
|
20 |
+
# 2. Installer les dépendances
|
21 |
+
def install_requirements():
|
22 |
+
pip_path = os.path.join(VENV_DIR, "Scripts" if FR else "bin", "pip")
|
23 |
+
print("[INFO] Installation des dépendances...")
|
24 |
+
subprocess.check_call([pip_path, "install", "-r", REQUIREMENTS])
|
25 |
+
|
26 |
+
def download_file_from_google_drive(id, destination):
|
27 |
+
URL = "https://docs.google.com/uc?export=download"
|
28 |
+
session = requests.Session()
|
29 |
+
response = session.get(URL, params={'id': id}, stream=True)
|
30 |
+
token = None
|
31 |
+
for key, value in response.cookies.items():
|
32 |
+
if key.startswith('download_warning'):
|
33 |
+
token = value
|
34 |
+
if token:
|
35 |
+
params = {'id': id, 'confirm': token}
|
36 |
+
response = session.get(URL, params=params, stream=True)
|
37 |
+
CHUNK_SIZE = 32768
|
38 |
+
with open(destination, "wb") as f:
|
39 |
+
for chunk in response.iter_content(CHUNK_SIZE):
|
40 |
+
if chunk:
|
41 |
+
f.write(chunk)
|
42 |
+
|
43 |
+
def extract_tar(tar_path, extract_path):
|
44 |
+
with tarfile.open(tar_path, "r") as tar:
|
45 |
+
tar.extractall(path=extract_path)
|
46 |
+
|
47 |
+
MODEL_DIR = "models/gemma-3n-transformers-gemma-3n-e2b-it-v1"
|
48 |
+
MODEL_TAR = "models/gemma-3n-transformers-gemma-3n-e2b-it-v1.tar"
|
49 |
+
GDRIVE_ID = "17WZeUKSxBHqFtfqm04MkAd7Ak6Yis-FM"
|
50 |
+
|
51 |
+
if not os.path.isdir(MODEL_DIR):
|
52 |
+
os.makedirs("models", exist_ok=True)
|
53 |
+
if not os.path.isfile(MODEL_TAR):
|
54 |
+
print("Téléchargement du modèle depuis Google Drive...")
|
55 |
+
download_file_from_google_drive(GDRIVE_ID, MODEL_TAR)
|
56 |
+
print("Décompression du modèle...")
|
57 |
+
extract_tar(MODEL_TAR, "models/")
|
58 |
+
print("Modèle prêt dans:", MODEL_DIR)
|
59 |
+
else:
|
60 |
+
print("Modèle déjà présent dans:", MODEL_DIR)
|
61 |
+
|
62 |
+
# 3. Vérifier la présence du modèle
|
63 |
+
def check_model():
|
64 |
+
if not os.path.isdir(MODEL_DIR):
|
65 |
+
print(f"[ERREUR] Le modèle Gemma 3n n'est pas trouvé dans {MODEL_DIR} !")
|
66 |
+
print("[EN] Gemma 3n model not found in", MODEL_DIR)
|
67 |
+
print("Veuillez placer le dossier du modèle téléchargé dans ce chemin avant de lancer l'application.")
|
68 |
+
sys.exit(1)
|
69 |
+
else:
|
70 |
+
print("[OK] Modèle Gemma 3n trouvé.")
|
71 |
+
|
72 |
+
# 4. Instructions de lancement
|
73 |
+
def print_instructions():
|
74 |
+
print("\n---")
|
75 |
+
print("🇫🇷 Installation terminée ! Lancez l'application avec :")
|
76 |
+
print(f" {VENV_DIR}\\Scripts\\activate && streamlit run src/streamlit_app.py" if FR else f" source {VENV_DIR}/bin/activate && streamlit run src/streamlit_app.py")
|
77 |
+
print("\n🇬🇧 Installation complete! Launch the app with:")
|
78 |
+
print(f" {VENV_DIR}\\Scripts\\activate && streamlit run src/streamlit_app.py" if FR else f" source {VENV_DIR}/bin/activate && streamlit run src/streamlit_app.py")
|
79 |
+
print("---\n")
|
80 |
+
|
81 |
+
if __name__ == "__main__":
|
82 |
+
create_venv()
|
83 |
+
install_requirements()
|
84 |
+
check_model()
|
85 |
+
print_instructions()
|