wintergw commited on
Commit
e02ab32
·
verified ·
1 Parent(s): c4ca648

Upload 2 files

Browse files
Files changed (2) hide show
  1. app.py +51 -6
  2. requirements.txt +4 -1
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,13 +6,21 @@ import streamlit as st
6
  import sys
7
  import os
8
  from pathlib import Path
 
 
9
 
10
- USER_NAME = os.getenv("USER_NAME", "").strip().strip('"')
11
- PRIVATE_SPACE_NAME = os.getenv("PRIVATE_SPACE_NAME", "").strip().strip('"')
 
 
 
 
 
 
12
 
13
  # Construct the repo ID
14
  #REPO_ID = f"{USER_NAME}/{PRIVATE_SPACE_NAME}"
15
- REPO_ID ="textbook/textbook_coop"
16
  REPO_TYPE = "space"
17
 
18
  # def setup_cache_directory():
@@ -39,15 +47,52 @@ REPO_TYPE = "space"
39
  # download_private_assets(cache_dir)
40
 
41
  #Download the entire space (optional, if needed)
 
 
 
 
 
 
 
 
 
 
 
42
  repo_dir = snapshot_download(
43
  repo_id=REPO_ID,
44
  repo_type=REPO_TYPE,
 
45
  cache_dir="private_space_cache"
46
  )
47
 
48
- # Add repo directory to sys.path so Python can find modules inside it
 
 
 
 
 
 
 
49
  sys.path.append(repo_dir)
50
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
51
 
52
 
53
  # Download specific files (if snapshot_download wasn't used)
@@ -58,7 +103,7 @@ app_path = hf_hub_download(
58
  )
59
 
60
  # Load and execute `app.py`
61
- spec_app = importlib.util.spec_from_file_location("app", app_path)
62
  app_module = importlib.util.module_from_spec(spec_app)
63
  spec_app.loader.exec_module(app_module)
64
 
 
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
+ #login(token=os.environ.get("HF_TOKEN_LLAMA"))
13
+ HF_TOKEN = os.environ.get("HF_TOKEN") #get HF_TOKEN
14
+ login(token=HF_TOKEN)
15
+
16
+
17
+
18
+ #USER_NAME = os.getenv("USER_NAME", "").strip().strip('"')
19
+ #PRIVATE_SPACE_NAME = os.getenv("PRIVATE_SPACE_NAME", "").strip().strip('"')
20
 
21
  # Construct the repo ID
22
  #REPO_ID = f"{USER_NAME}/{PRIVATE_SPACE_NAME}"
23
+ REPO_ID ="wintergw/textbook_coop"
24
  REPO_TYPE = "space"
25
 
26
  # def setup_cache_directory():
 
47
  # download_private_assets(cache_dir)
48
 
49
  #Download the entire space (optional, if needed)
50
+ # repo_dir = snapshot_download(
51
+ # repo_id=REPO_ID,
52
+ # repo_type=REPO_TYPE,
53
+ # token=HF_TOKEN,
54
+ # cache_dir="private_space_cache"
55
+ # )
56
+
57
+ # # Add repo directory to sys.path so Python can find modules inside it
58
+ # sys.path.append(repo_dir)
59
+
60
+ # Download the entire space, including the fine-tuned model folder
61
  repo_dir = snapshot_download(
62
  repo_id=REPO_ID,
63
  repo_type=REPO_TYPE,
64
+ token=HF_TOKEN,
65
  cache_dir="private_space_cache"
66
  )
67
 
68
+ # The fine-tuned model is located in "fine_tuned_llama3" inside the Space
69
+ fine_tuned_model_path = os.path.join(repo_dir, "fine_tuned_llama3")
70
+
71
+ # Verify the model path
72
+ if not os.path.exists(fine_tuned_model_path):
73
+ raise FileNotFoundError(f"Fine-tuned model not found at {fine_tuned_model_path}")
74
+
75
+ # Add repo directory to sys.path
76
  sys.path.append(repo_dir)
77
 
78
+ # Load the base model
79
+ base_model = AutoModelForCausalLM.from_pretrained(
80
+ "meta-llama/Meta-Llama-3-8B",
81
+ token=HF_TOKEN
82
+ )
83
+
84
+ # Load fine-tuned adapter (PEFT)
85
+ fine_tuned_model = PeftModel.from_pretrained(base_model, fine_tuned_model_path)
86
+
87
+ # Load tokenizer
88
+ tokenizer = AutoTokenizer.from_pretrained(fine_tuned_model_path)
89
+
90
+ print("Fine-tuned model loaded successfully!")
91
+
92
+
93
+
94
+
95
+
96
 
97
 
98
  # Download specific files (if snapshot_download wasn't used)
 
103
  )
104
 
105
  # Load and execute `app.py`
106
+ spec_app = importlib.util.spec_from_file_location("*", app_path)
107
  app_module = importlib.util.module_from_spec(spec_app)
108
  spec_app.loader.exec_module(app_module)
109
 
requirements.txt CHANGED
@@ -3,4 +3,7 @@ pandas
3
  openpyxl
4
  pyscipopt
5
  gurobipy
6
- huggingface_hub
 
 
 
 
3
  openpyxl
4
  pyscipopt
5
  gurobipy
6
+ huggingface_hub
7
+ transformers
8
+ torch
9
+ peft