wintergw commited on
Commit
2c87f8e
·
verified ·
1 Parent(s): 86f7acc

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +45 -3
app.py CHANGED
@@ -1,4 +1,4 @@
1
- from huggingface_hub import hf_hub_download, snapshot_download
2
  import pandas as pd
3
  import importlib
4
  import importlib.util
@@ -6,12 +6,18 @@ import streamlit as st
6
  import sys
7
  import os
8
  from pathlib import Path
 
 
 
 
9
 
10
- from huggingface_hub import login
11
  #login(token=os.environ.get("HF_TOKEN_LLAMA"))
12
  HF_TOKEN = os.environ.get("HF_TOKEN") #get HF_TOKEN
13
  login(token=HF_TOKEN)
14
 
 
 
 
15
  #USER_NAME = os.getenv("USER_NAME", "").strip().strip('"')
16
  #PRIVATE_SPACE_NAME = os.getenv("PRIVATE_SPACE_NAME", "").strip().strip('"')
17
 
@@ -44,6 +50,17 @@ REPO_TYPE = "space"
44
  # download_private_assets(cache_dir)
45
 
46
  #Download the entire space (optional, if needed)
 
 
 
 
 
 
 
 
 
 
 
47
  repo_dir = snapshot_download(
48
  repo_id=REPO_ID,
49
  repo_type=REPO_TYPE,
@@ -51,9 +68,34 @@ repo_dir = snapshot_download(
51
  cache_dir="private_space_cache"
52
  )
53
 
54
- # Add repo directory to sys.path so Python can find modules inside it
 
 
 
 
 
 
 
55
  sys.path.append(repo_dir)
56
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
57
 
58
 
59
  # Download specific files (if snapshot_download wasn't used)
 
1
+ from huggingface_hub import hf_hub_download, snapshot_download, login
2
  import pandas as pd
3
  import importlib
4
  import importlib.util
 
6
  import sys
7
  import os
8
  from pathlib import Path
9
+ from transformers import AutoModelForCausalLM, AutoTokenizer
10
+ from peft import PeftModel
11
+
12
+
13
 
 
14
  #login(token=os.environ.get("HF_TOKEN_LLAMA"))
15
  HF_TOKEN = os.environ.get("HF_TOKEN") #get HF_TOKEN
16
  login(token=HF_TOKEN)
17
 
18
+ # Check if the token is valid and has access
19
+ print(whoami())
20
+
21
  #USER_NAME = os.getenv("USER_NAME", "").strip().strip('"')
22
  #PRIVATE_SPACE_NAME = os.getenv("PRIVATE_SPACE_NAME", "").strip().strip('"')
23
 
 
50
  # download_private_assets(cache_dir)
51
 
52
  #Download the entire space (optional, if needed)
53
+ # repo_dir = snapshot_download(
54
+ # repo_id=REPO_ID,
55
+ # repo_type=REPO_TYPE,
56
+ # token=HF_TOKEN,
57
+ # cache_dir="private_space_cache"
58
+ # )
59
+
60
+ # # Add repo directory to sys.path so Python can find modules inside it
61
+ # sys.path.append(repo_dir)
62
+
63
+ # Download the entire space, including the fine-tuned model folder
64
  repo_dir = snapshot_download(
65
  repo_id=REPO_ID,
66
  repo_type=REPO_TYPE,
 
68
  cache_dir="private_space_cache"
69
  )
70
 
71
+ # The fine-tuned model is located in "fine_tuned_llama3" inside the Space
72
+ fine_tuned_model_path = os.path.join(repo_dir, "fine_tuned_llama3")
73
+
74
+ # Verify the model path
75
+ if not os.path.exists(fine_tuned_model_path):
76
+ raise FileNotFoundError(f"Fine-tuned model not found at {fine_tuned_model_path}")
77
+
78
+ # Add repo directory to sys.path
79
  sys.path.append(repo_dir)
80
 
81
+ # Load the base model
82
+ base_model = AutoModelForCausalLM.from_pretrained(
83
+ "meta-llama/Meta-Llama-3-8B",
84
+ token=HF_TOKEN
85
+ )
86
+
87
+ # Load fine-tuned adapter (PEFT)
88
+ fine_tuned_model = PeftModel.from_pretrained(base_model, fine_tuned_model_path)
89
+
90
+ # Load tokenizer
91
+ tokenizer = AutoTokenizer.from_pretrained(fine_tuned_model_path)
92
+
93
+ print("Fine-tuned model loaded successfully!")
94
+
95
+
96
+
97
+
98
+
99
 
100
 
101
  # Download specific files (if snapshot_download wasn't used)