Spaces:
Sleeping
Sleeping
ShobhitKori
commited on
Commit
·
34cbacb
1
Parent(s):
62522ae
Updated codet5_model.py
Browse files- codet5_model.py +49 -15
- requirements.txt +0 -0
codet5_model.py
CHANGED
|
@@ -34,33 +34,67 @@
|
|
| 34 |
# output = model.generate(**inputs, max_length=256)
|
| 35 |
# return tokenizer.decode(output[0], skip_special_tokens=True)
|
| 36 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 37 |
import os
|
|
|
|
| 38 |
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
|
| 39 |
|
| 40 |
-
|
| 41 |
-
|
|
|
|
| 42 |
os.makedirs(MODEL_DIR, exist_ok=True)
|
| 43 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 44 |
tokenizer = None
|
| 45 |
model = None
|
| 46 |
|
| 47 |
def load_model():
|
| 48 |
global tokenizer, model
|
| 49 |
if tokenizer is None or model is None:
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
else:
|
| 55 |
-
print("Loading model from /data directory...")
|
| 56 |
-
tokenizer = AutoTokenizer.from_pretrained(MODEL_DIR)
|
| 57 |
-
model = AutoModelForSeq2SeqLM.from_pretrained(MODEL_DIR)
|
| 58 |
-
print("Model loaded.")
|
| 59 |
|
| 60 |
def generate_code(instruction: str) -> str:
|
| 61 |
-
load_model()
|
| 62 |
prompt = f"# Instruction: {instruction}\n# Python Code:\n"
|
| 63 |
|
| 64 |
inputs = tokenizer(prompt, return_tensors="pt", padding=True, truncation=True)
|
| 65 |
-
|
| 66 |
-
return tokenizer.decode(
|
|
|
|
| 34 |
# output = model.generate(**inputs, max_length=256)
|
| 35 |
# return tokenizer.decode(output[0], skip_special_tokens=True)
|
| 36 |
|
| 37 |
+
# import os
|
| 38 |
+
# from huggingface_hub import snapshot_download
|
| 39 |
+
# from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
|
| 40 |
+
|
| 41 |
+
# MODEL_DIR = snapshot_download(repo_id="Salesforce/codet5p-770m-py", cache_dir="./data")
|
| 42 |
+
|
| 43 |
+
# os.makedirs(MODEL_DIR, exist_ok=True)
|
| 44 |
+
# # Global variables but not initialized
|
| 45 |
+
# tokenizer = None
|
| 46 |
+
# model = None
|
| 47 |
+
|
| 48 |
+
# def load_model():
|
| 49 |
+
# global tokenizer, model
|
| 50 |
+
# if tokenizer is None or model is None:
|
| 51 |
+
# if not os.path.exists(MODEL_DIR):
|
| 52 |
+
# print("Downloading CodeT5 model to persistent /data directory...")
|
| 53 |
+
# tokenizer = AutoTokenizer.from_pretrained("Salesforce/codet5p-770m-py", cache_dir=MODEL_DIR)
|
| 54 |
+
# model = AutoModelForSeq2SeqLM.from_pretrained("Salesforce/codet5p-770m-py", cache_dir=MODEL_DIR)
|
| 55 |
+
# else:
|
| 56 |
+
# print("Loading model from /data directory...")
|
| 57 |
+
# tokenizer = AutoTokenizer.from_pretrained(MODEL_DIR)
|
| 58 |
+
# model = AutoModelForSeq2SeqLM.from_pretrained(MODEL_DIR)
|
| 59 |
+
# print("Model loaded.")
|
| 60 |
+
|
| 61 |
+
# def generate_code(instruction: str) -> str:
|
| 62 |
+
# load_model() # Ensure model is loaded before use
|
| 63 |
+
# prompt = f"# Instruction: {instruction}\n# Python Code:\n"
|
| 64 |
+
|
| 65 |
+
# inputs = tokenizer(prompt, return_tensors="pt", padding=True, truncation=True)
|
| 66 |
+
# output = model.generate(**inputs, max_length=256)
|
| 67 |
+
# return tokenizer.decode(output[0], skip_special_tokens=True)
|
| 68 |
+
|
| 69 |
+
|
| 70 |
import os
|
| 71 |
+
from huggingface_hub import snapshot_download
|
| 72 |
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
|
| 73 |
|
| 74 |
+
# Constants
|
| 75 |
+
HF_MODEL_ID = "Salesforce/codet5p-770m-py"
|
| 76 |
+
MODEL_DIR = "./data/codet5_model" # More explicit and organized
|
| 77 |
os.makedirs(MODEL_DIR, exist_ok=True)
|
| 78 |
+
|
| 79 |
+
# Snapshot download (will cache in MODEL_DIR)
|
| 80 |
+
snapshot_download(repo_id=HF_MODEL_ID, cache_dir=MODEL_DIR, local_dir=MODEL_DIR, local_dir_use_symlinks=False)
|
| 81 |
+
|
| 82 |
+
# Globals
|
| 83 |
tokenizer = None
|
| 84 |
model = None
|
| 85 |
|
| 86 |
def load_model():
|
| 87 |
global tokenizer, model
|
| 88 |
if tokenizer is None or model is None:
|
| 89 |
+
print("Loading CodeT5 model from persistent directory...")
|
| 90 |
+
tokenizer = AutoTokenizer.from_pretrained(MODEL_DIR)
|
| 91 |
+
model = AutoModelForSeq2SeqLM.from_pretrained(MODEL_DIR)
|
| 92 |
+
print("Model loaded successfully.")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 93 |
|
| 94 |
def generate_code(instruction: str) -> str:
|
| 95 |
+
load_model()
|
| 96 |
prompt = f"# Instruction: {instruction}\n# Python Code:\n"
|
| 97 |
|
| 98 |
inputs = tokenizer(prompt, return_tensors="pt", padding=True, truncation=True)
|
| 99 |
+
outputs = model.generate(**inputs, max_length=256)
|
| 100 |
+
return tokenizer.decode(outputs[0], skip_special_tokens=True)
|
requirements.txt
CHANGED
|
Binary files a/requirements.txt and b/requirements.txt differ
|
|
|