Spaces:
Running
Running
abhisheksan
commited on
Commit
•
2442c76
1
Parent(s):
9e5e405
Add model and configuration files; implement GPT-2 tokenizer and model initialization in main.py
Browse files- logs/poetry_generation.log +102 -0
- main.py +19 -35
- models/config.json +21 -0
- models/pytorch_model.bin +3 -0
logs/poetry_generation.log
CHANGED
@@ -3,3 +3,105 @@
|
|
3 |
2024-11-16 23:21:32,229 - main - INFO - Loading model...
|
4 |
2024-11-16 23:21:32,229 - main - ERROR - Model file not found at ./models\poeticagpt-quantized-new.pth
|
5 |
2024-11-16 23:21:32,231 - main - ERROR - Failed to initialize model manager
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
3 |
2024-11-16 23:21:32,229 - main - INFO - Loading model...
|
4 |
2024-11-16 23:21:32,229 - main - ERROR - Model file not found at ./models\poeticagpt-quantized-new.pth
|
5 |
2024-11-16 23:21:32,231 - main - ERROR - Failed to initialize model manager
|
6 |
+
2024-11-16 23:30:46,037 - main - INFO - Loading tokenizer...
|
7 |
+
2024-11-16 23:30:46,798 - main - WARNING - Could not load custom vocabulary: property 'vocab' of 'GPT2TokenizerFast' object has no setter
|
8 |
+
2024-11-16 23:30:46,799 - main - INFO - Loading model...
|
9 |
+
2024-11-16 23:30:46,799 - main - ERROR - Error initializing model: Incorrect path_or_model_id: './models/poeticagpt.pth'. Please provide either the path to a local folder or the repo_id of a model on the Hub.
|
10 |
+
2024-11-16 23:30:46,800 - main - ERROR - Detailed traceback:
|
11 |
+
Traceback (most recent call last):
|
12 |
+
File "e:\Self Work\My Projects\Poetica HuggingFace Server\.venv\Lib\site-packages\transformers\utils\hub.py", line 402, in cached_file
|
13 |
+
resolved_file = hf_hub_download(
|
14 |
+
^^^^^^^^^^^^^^^^
|
15 |
+
File "e:\Self Work\My Projects\Poetica HuggingFace Server\.venv\Lib\site-packages\huggingface_hub\utils\_validators.py", line 106, in _inner_fn
|
16 |
+
validate_repo_id(arg_value)
|
17 |
+
File "e:\Self Work\My Projects\Poetica HuggingFace Server\.venv\Lib\site-packages\huggingface_hub\utils\_validators.py", line 154, in validate_repo_id
|
18 |
+
raise HFValidationError(
|
19 |
+
huggingface_hub.errors.HFValidationError: Repo id must be in the form 'repo_name' or 'namespace/repo_name': './models/poeticagpt.pth'. Use `repo_type` argument if needed.
|
20 |
+
|
21 |
+
The above exception was the direct cause of the following exception:
|
22 |
+
|
23 |
+
Traceback (most recent call last):
|
24 |
+
File "E:\Self Work\My Projects\Poetica HuggingFace Server\poetica\main.py", line 88, in initialize
|
25 |
+
self.model = AutoModelForCausalLM.from_pretrained(model_path, local_files_only=True)
|
26 |
+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
27 |
+
File "e:\Self Work\My Projects\Poetica HuggingFace Server\.venv\Lib\site-packages\transformers\models\auto\auto_factory.py", line 485, in from_pretrained
|
28 |
+
resolved_config_file = cached_file(
|
29 |
+
^^^^^^^^^^^^
|
30 |
+
File "e:\Self Work\My Projects\Poetica HuggingFace Server\.venv\Lib\site-packages\transformers\utils\hub.py", line 466, in cached_file
|
31 |
+
raise EnvironmentError(
|
32 |
+
OSError: Incorrect path_or_model_id: './models/poeticagpt.pth'. Please provide either the path to a local folder or the repo_id of a model on the Hub.
|
33 |
+
2024-11-16 23:30:46,803 - main - ERROR - Failed to initialize model manager
|
34 |
+
2024-11-16 23:33:40,483 - main - INFO - Loading tokenizer...
|
35 |
+
2024-11-16 23:33:41,621 - main - WARNING - Could not load custom vocabulary: property 'vocab' of 'GPT2TokenizerFast' object has no setter
|
36 |
+
2024-11-16 23:33:41,622 - main - INFO - Loading model...
|
37 |
+
2024-11-16 23:33:43,332 - main - ERROR - Error initializing model: Error no file named pytorch_model.bin, model.safetensors, tf_model.h5, model.ckpt.index or flax_model.msgpack found in directory ./models/.
|
38 |
+
2024-11-16 23:33:43,333 - main - ERROR - Detailed traceback:
|
39 |
+
Traceback (most recent call last):
|
40 |
+
File "E:\Self Work\My Projects\Poetica HuggingFace Server\poetica\main.py", line 88, in initialize
|
41 |
+
self.model = AutoModelForCausalLM.from_pretrained(model_path, local_files_only=True)
|
42 |
+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
43 |
+
File "e:\Self Work\My Projects\Poetica HuggingFace Server\.venv\Lib\site-packages\transformers\models\auto\auto_factory.py", line 564, in from_pretrained
|
44 |
+
return model_class.from_pretrained(
|
45 |
+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
46 |
+
File "e:\Self Work\My Projects\Poetica HuggingFace Server\.venv\Lib\site-packages\transformers\modeling_utils.py", line 3447, in from_pretrained
|
47 |
+
raise EnvironmentError(
|
48 |
+
OSError: Error no file named pytorch_model.bin, model.safetensors, tf_model.h5, model.ckpt.index or flax_model.msgpack found in directory ./models/.
|
49 |
+
2024-11-16 23:33:43,335 - main - ERROR - Failed to initialize model manager
|
50 |
+
2024-11-16 23:34:18,283 - main - INFO - Loading tokenizer...
|
51 |
+
2024-11-16 23:34:18,966 - main - WARNING - Could not load custom vocabulary: property 'vocab' of 'GPT2TokenizerFast' object has no setter
|
52 |
+
2024-11-16 23:34:18,966 - main - INFO - Loading model...
|
53 |
+
2024-11-16 23:34:20,499 - main - ERROR - Error initializing model: Error no file named pytorch_model.bin, model.safetensors, tf_model.h5, model.ckpt.index or flax_model.msgpack found in directory ./models/.
|
54 |
+
2024-11-16 23:34:20,500 - main - ERROR - Detailed traceback:
|
55 |
+
Traceback (most recent call last):
|
56 |
+
File "E:\Self Work\My Projects\Poetica HuggingFace Server\poetica\main.py", line 88, in initialize
|
57 |
+
self.model = AutoModelForCausalLM.from_pretrained(model_path, local_files_only=True)
|
58 |
+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
59 |
+
File "e:\Self Work\My Projects\Poetica HuggingFace Server\.venv\Lib\site-packages\transformers\models\auto\auto_factory.py", line 564, in from_pretrained
|
60 |
+
return model_class.from_pretrained(
|
61 |
+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
62 |
+
File "e:\Self Work\My Projects\Poetica HuggingFace Server\.venv\Lib\site-packages\transformers\modeling_utils.py", line 3447, in from_pretrained
|
63 |
+
raise EnvironmentError(
|
64 |
+
OSError: Error no file named pytorch_model.bin, model.safetensors, tf_model.h5, model.ckpt.index or flax_model.msgpack found in directory ./models/.
|
65 |
+
2024-11-16 23:34:20,502 - main - ERROR - Failed to initialize model manager
|
66 |
+
2024-11-16 23:35:15,983 - main - INFO - Loading tokenizer...
|
67 |
+
2024-11-16 23:35:17,111 - main - WARNING - Could not load custom vocabulary: property 'vocab' of 'GPT2TokenizerFast' object has no setter
|
68 |
+
2024-11-16 23:35:17,111 - main - INFO - Loading model...
|
69 |
+
2024-11-16 23:35:18,795 - main - ERROR - Error initializing model: Unable to load weights from pytorch checkpoint file for './models/pytorch_model.bin' at './models/pytorch_model.bin'. If you tried to load a PyTorch model from a TF 2.0 checkpoint, please set from_tf=True.
|
70 |
+
2024-11-16 23:35:18,796 - main - ERROR - Detailed traceback:
|
71 |
+
Traceback (most recent call last):
|
72 |
+
File "e:\Self Work\My Projects\Poetica HuggingFace Server\.venv\Lib\site-packages\transformers\modeling_utils.py", line 575, in load_state_dict
|
73 |
+
return torch.load(
|
74 |
+
^^^^^^^^^^^
|
75 |
+
File "e:\Self Work\My Projects\Poetica HuggingFace Server\.venv\Lib\site-packages\torch\serialization.py", line 1024, in load
|
76 |
+
raise pickle.UnpicklingError(UNSAFE_MESSAGE + str(e)) from None
|
77 |
+
_pickle.UnpicklingError: Weights only load failed. Re-running `torch.load` with `weights_only` set to `False` will likely succeed, but it can result in arbitrary code execution.Do it only if you get the file from a trusted source. WeightsUnpickler error: Unsupported class torch.qint8
|
78 |
+
|
79 |
+
During handling of the above exception, another exception occurred:
|
80 |
+
|
81 |
+
Traceback (most recent call last):
|
82 |
+
File "e:\Self Work\My Projects\Poetica HuggingFace Server\.venv\Lib\site-packages\transformers\modeling_utils.py", line 584, in load_state_dict
|
83 |
+
if f.read(7) == "version":
|
84 |
+
^^^^^^^^^
|
85 |
+
File "D:\Program Files\Python\Lib\encodings\cp1252.py", line 23, in decode
|
86 |
+
return codecs.charmap_decode(input,self.errors,decoding_table)[0]
|
87 |
+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
88 |
+
UnicodeDecodeError: 'charmap' codec can't decode byte 0x81 in position 1651: character maps to <undefined>
|
89 |
+
|
90 |
+
During handling of the above exception, another exception occurred:
|
91 |
+
|
92 |
+
Traceback (most recent call last):
|
93 |
+
File "E:\Self Work\My Projects\Poetica HuggingFace Server\poetica\main.py", line 88, in initialize
|
94 |
+
self.model = AutoModelForCausalLM.from_pretrained(model_path, local_files_only=True)
|
95 |
+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
96 |
+
File "e:\Self Work\My Projects\Poetica HuggingFace Server\.venv\Lib\site-packages\transformers\models\auto\auto_factory.py", line 564, in from_pretrained
|
97 |
+
return model_class.from_pretrained(
|
98 |
+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
99 |
+
File "e:\Self Work\My Projects\Poetica HuggingFace Server\.venv\Lib\site-packages\transformers\modeling_utils.py", line 3703, in from_pretrained
|
100 |
+
state_dict = load_state_dict(resolved_archive_file)
|
101 |
+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
102 |
+
File "e:\Self Work\My Projects\Poetica HuggingFace Server\.venv\Lib\site-packages\transformers\modeling_utils.py", line 596, in load_state_dict
|
103 |
+
raise OSError(
|
104 |
+
OSError: Unable to load weights from pytorch checkpoint file for './models/pytorch_model.bin' at './models/pytorch_model.bin'. If you tried to load a PyTorch model from a TF 2.0 checkpoint, please set from_tf=True.
|
105 |
+
2024-11-16 23:35:18,815 - main - ERROR - Failed to initialize model manager
|
106 |
+
2024-11-16 23:37:05,649 - main - INFO - Loading tokenizer...
|
107 |
+
2024-11-16 23:37:06,372 - main - INFO - Loading model...
|
main.py
CHANGED
@@ -1,39 +1,32 @@
|
|
1 |
import os
|
2 |
from typing import Optional, Dict, Any
|
3 |
-
from enum import Enum
|
4 |
from fastapi import FastAPI, HTTPException, status
|
5 |
-
from pathlib import Path
|
6 |
import logging
|
7 |
import sys
|
8 |
from pydantic import BaseModel, Field
|
9 |
import torch
|
10 |
-
from transformers import
|
11 |
import json
|
12 |
|
13 |
# Define base model directory
|
14 |
BASE_MODEL_DIR = "./models/"
|
|
|
15 |
|
16 |
-
# Configure logging with fallback to stdout if file writing fails
|
17 |
def setup_logging():
|
18 |
logger = logging.getLogger(__name__)
|
19 |
logger.setLevel(logging.DEBUG)
|
20 |
|
21 |
-
# Create formatter
|
22 |
formatter = logging.Formatter(
|
23 |
'%(asctime)s - %(name)s - %(levelname)s - %(message)s'
|
24 |
)
|
25 |
|
26 |
-
# Always add stdout handler
|
27 |
stdout_handler = logging.StreamHandler(sys.stdout)
|
28 |
stdout_handler.setFormatter(formatter)
|
29 |
logger.addHandler(stdout_handler)
|
30 |
|
31 |
-
# Try to add file handler, but don't fail if we can't
|
32 |
try:
|
33 |
-
# First try logs directory in current working directory
|
34 |
log_dir = os.path.join(os.getcwd(), 'logs')
|
35 |
-
|
36 |
-
os.makedirs(log_dir, exist_ok=True)
|
37 |
|
38 |
file_handler = logging.FileHandler(os.path.join(log_dir, 'poetry_generation.log'))
|
39 |
file_handler.setFormatter(formatter)
|
@@ -43,7 +36,6 @@ def setup_logging():
|
|
43 |
|
44 |
return logger
|
45 |
|
46 |
-
# Set up logging
|
47 |
logger = setup_logging()
|
48 |
|
49 |
class GenerateRequest(BaseModel):
|
@@ -63,31 +55,25 @@ class ModelManager:
|
|
63 |
"""Initialize the model and tokenizer"""
|
64 |
try:
|
65 |
logger.info("Loading tokenizer...")
|
66 |
-
|
67 |
-
|
68 |
-
self.tokenizer =
|
69 |
-
|
70 |
-
# Now customize it with your vocabulary if needed
|
71 |
-
vocab_path = os.path.join(BASE_MODEL_DIR, "vocab.json")
|
72 |
-
if os.path.exists(vocab_path):
|
73 |
-
try:
|
74 |
-
with open(vocab_path, 'r', encoding='utf-8') as f:
|
75 |
-
custom_vocab = json.load(f)
|
76 |
-
self.tokenizer.vocab = custom_vocab
|
77 |
-
self.tokenizer.ids_to_tokens = {v: k for k, v in custom_vocab.items()}
|
78 |
-
except Exception as e:
|
79 |
-
logger.warning(f"Could not load custom vocabulary: {str(e)}")
|
80 |
|
81 |
logger.info("Loading model...")
|
82 |
-
|
83 |
-
|
84 |
-
logger.error(f"Model file not found at {model_path}")
|
85 |
return False
|
86 |
-
|
87 |
-
# Load the model weights
|
88 |
-
self.model = AutoModelForCausalLM.from_pretrained(model_path, local_files_only=True)
|
89 |
|
90 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
91 |
self.model.to('cpu')
|
92 |
self.model.eval()
|
93 |
|
@@ -127,6 +113,7 @@ class ModelManager:
|
|
127 |
pad_token_id=self.tokenizer.eos_token_id,
|
128 |
)
|
129 |
|
|
|
130 |
generated_text = self.tokenizer.decode(outputs[0], skip_special_tokens=True)
|
131 |
|
132 |
return {
|
@@ -157,9 +144,6 @@ async def startup():
|
|
157 |
"""Initialize the model during startup"""
|
158 |
if not model_manager.initialize():
|
159 |
logger.error("Failed to initialize model manager")
|
160 |
-
# In production, we might want to continue running even if model fails to load
|
161 |
-
# Instead of exiting, we'll just log the error
|
162 |
-
# sys.exit(1)
|
163 |
|
164 |
@app.get("/health")
|
165 |
async def health_check():
|
|
|
1 |
import os
|
2 |
from typing import Optional, Dict, Any
|
|
|
3 |
from fastapi import FastAPI, HTTPException, status
|
|
|
4 |
import logging
|
5 |
import sys
|
6 |
from pydantic import BaseModel, Field
|
7 |
import torch
|
8 |
+
from transformers import GPT2Tokenizer, GPT2LMHeadModel
|
9 |
import json
|
10 |
|
11 |
# Define base model directory
|
12 |
BASE_MODEL_DIR = "./models/"
|
13 |
+
MODEL_PATH = os.path.join(BASE_MODEL_DIR, "poeticagpt.pth")
|
14 |
|
|
|
15 |
def setup_logging():
|
16 |
logger = logging.getLogger(__name__)
|
17 |
logger.setLevel(logging.DEBUG)
|
18 |
|
|
|
19 |
formatter = logging.Formatter(
|
20 |
'%(asctime)s - %(name)s - %(levelname)s - %(message)s'
|
21 |
)
|
22 |
|
|
|
23 |
stdout_handler = logging.StreamHandler(sys.stdout)
|
24 |
stdout_handler.setFormatter(formatter)
|
25 |
logger.addHandler(stdout_handler)
|
26 |
|
|
|
27 |
try:
|
|
|
28 |
log_dir = os.path.join(os.getcwd(), 'logs')
|
29 |
+
os.makedirs(log_dir, exist_ok=True)
|
|
|
30 |
|
31 |
file_handler = logging.FileHandler(os.path.join(log_dir, 'poetry_generation.log'))
|
32 |
file_handler.setFormatter(formatter)
|
|
|
36 |
|
37 |
return logger
|
38 |
|
|
|
39 |
logger = setup_logging()
|
40 |
|
41 |
class GenerateRequest(BaseModel):
|
|
|
55 |
"""Initialize the model and tokenizer"""
|
56 |
try:
|
57 |
logger.info("Loading tokenizer...")
|
58 |
+
# Load the base GPT-2 tokenizer
|
59 |
+
self.tokenizer = GPT2Tokenizer.from_pretrained('gpt2')
|
60 |
+
self.tokenizer.pad_token = self.tokenizer.eos_token
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
61 |
|
62 |
logger.info("Loading model...")
|
63 |
+
if not os.path.exists(MODEL_PATH):
|
64 |
+
logger.error(f"Model file not found at {MODEL_PATH}")
|
|
|
65 |
return False
|
|
|
|
|
|
|
66 |
|
67 |
+
# Initialize a GPT2 model with default configuration
|
68 |
+
self.model = GPT2LMHeadModel.from_pretrained('gpt2')
|
69 |
+
|
70 |
+
# Load your trained weights
|
71 |
+
state_dict = torch.load(MODEL_PATH, map_location='cpu')
|
72 |
+
|
73 |
+
# Load the state dictionary into the model
|
74 |
+
self.model.load_state_dict(state_dict)
|
75 |
+
|
76 |
+
# Force model to CPU and eval mode
|
77 |
self.model.to('cpu')
|
78 |
self.model.eval()
|
79 |
|
|
|
113 |
pad_token_id=self.tokenizer.eos_token_id,
|
114 |
)
|
115 |
|
116 |
+
# Decode the generated text
|
117 |
generated_text = self.tokenizer.decode(outputs[0], skip_special_tokens=True)
|
118 |
|
119 |
return {
|
|
|
144 |
"""Initialize the model during startup"""
|
145 |
if not model_manager.initialize():
|
146 |
logger.error("Failed to initialize model manager")
|
|
|
|
|
|
|
147 |
|
148 |
@app.get("/health")
|
149 |
async def health_check():
|
models/config.json
ADDED
@@ -0,0 +1,21 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
{
|
2 |
+
"architectures": [
|
3 |
+
"GPT2LMHeadModel"
|
4 |
+
],
|
5 |
+
"model_type": "gpt2",
|
6 |
+
"activation_function": "gelu_new",
|
7 |
+
"attn_pdrop": 0.1,
|
8 |
+
"bos_token_id": 50256,
|
9 |
+
"embd_pdrop": 0.1,
|
10 |
+
"eos_token_id": 50256,
|
11 |
+
"initializer_range": 0.02,
|
12 |
+
"layer_norm_epsilon": 1e-5,
|
13 |
+
"n_ctx": 1024,
|
14 |
+
"n_embd": 768,
|
15 |
+
"n_head": 12,
|
16 |
+
"n_layer": 12,
|
17 |
+
"n_positions": 1024,
|
18 |
+
"resid_pdrop": 0.1,
|
19 |
+
"vocab_size": 50257,
|
20 |
+
"use_cache": true
|
21 |
+
}
|
models/pytorch_model.bin
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:f77da9534fcf01b36f4780cd24ebe46e4d7f8740a1b17b66d5173d8694d6a62e
|
3 |
+
size 139310252
|