Update app.py
Browse files
app.py
CHANGED
|
@@ -9,7 +9,7 @@ from huggingface_hub import HfApi, hf_hub_download
|
|
| 9 |
# Configuration constants
|
| 10 |
MODEL_REPO = "Gajendra5490/Scrached_Trained_Model"
|
| 11 |
CURRENT_USER = "gajendra82"
|
| 12 |
-
CURRENT_UTC = "2025-05-06 15:
|
| 13 |
|
| 14 |
def setup_logging():
|
| 15 |
logging.basicConfig(
|
|
@@ -23,86 +23,70 @@ def setup_logging():
|
|
| 23 |
|
| 24 |
logger = setup_logging()
|
| 25 |
|
| 26 |
-
# Initialize Hugging Face API with token
|
| 27 |
-
try:
|
| 28 |
-
token = os.environ.get('HF_TOKEN')
|
| 29 |
-
if not token:
|
| 30 |
-
logger.error("No HF_TOKEN found in environment variables")
|
| 31 |
-
raise ValueError("HF_TOKEN not found")
|
| 32 |
-
|
| 33 |
-
api = HfApi(token=token)
|
| 34 |
-
logger.info("Successfully initialized Hugging Face API")
|
| 35 |
-
except Exception as e:
|
| 36 |
-
logger.error(f"Failed to initialize Hugging Face API: {e}")
|
| 37 |
-
raise
|
| 38 |
-
|
| 39 |
-
class EnhancedTokenizer:
|
| 40 |
-
def __init__(self, vocab):
|
| 41 |
-
self.vocab = vocab
|
| 42 |
-
self.special_tokens = {
|
| 43 |
-
"<user>": len(vocab),
|
| 44 |
-
"<assistant>": len(vocab) + 1,
|
| 45 |
-
"<sep>": len(vocab) + 2,
|
| 46 |
-
"<eos>": len(vocab) + 3
|
| 47 |
-
}
|
| 48 |
-
|
| 49 |
-
def encode(self, text):
|
| 50 |
-
tokens = text.split()
|
| 51 |
-
return [self.vocab.get(token, 0) if token not in self.special_tokens
|
| 52 |
-
else self.special_tokens[token] for token in tokens]
|
| 53 |
-
|
| 54 |
-
def decode(self, ids):
|
| 55 |
-
reverse_vocab = {v: k for k, v in self.vocab.items()}
|
| 56 |
-
reverse_special = {v: k for k, v in self.special_tokens.items()}
|
| 57 |
-
return " ".join(reverse_vocab.get(id, reverse_special.get(id, "<unk>"))
|
| 58 |
-
for id in ids)
|
| 59 |
-
|
| 60 |
class ImprovedTransformer(torch.nn.Module):
|
| 61 |
-
def __init__(
|
| 62 |
-
|
| 63 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 64 |
super().__init__()
|
| 65 |
|
| 66 |
self.d_model = d_model
|
| 67 |
self.embedding = torch.nn.Embedding(vocab_size, d_model)
|
| 68 |
-
self.pos_encoder = torch.nn.Embedding(max_seq_length, d_model)
|
| 69 |
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
)
|
| 77 |
-
|
|
|
|
|
|
|
| 78 |
d_model=d_model,
|
| 79 |
nhead=nhead,
|
|
|
|
|
|
|
| 80 |
dim_feedforward=dim_feedforward,
|
| 81 |
dropout=dropout,
|
| 82 |
batch_first=True
|
| 83 |
)
|
| 84 |
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
)
|
| 89 |
-
self.transformer_decoder = torch.nn.TransformerDecoder(
|
| 90 |
-
decoder_layer,
|
| 91 |
-
num_decoder_layers
|
| 92 |
-
)
|
| 93 |
-
|
| 94 |
-
self.output = torch.nn.Linear(d_model, vocab_size)
|
| 95 |
|
| 96 |
def forward(self, src, tgt):
|
| 97 |
-
|
| 98 |
-
|
|
|
|
| 99 |
|
| 100 |
-
|
| 101 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 102 |
|
| 103 |
-
|
| 104 |
-
output = self.
|
| 105 |
-
return self.
|
| 106 |
|
| 107 |
class ModelInference:
|
| 108 |
def __init__(self):
|
|
@@ -113,9 +97,12 @@ class ModelInference:
|
|
| 113 |
|
| 114 |
def load_model(self):
|
| 115 |
try:
|
| 116 |
-
|
| 117 |
-
|
|
|
|
| 118 |
|
|
|
|
|
|
|
| 119 |
model_path = hf_hub_download(
|
| 120 |
repo_id=MODEL_REPO,
|
| 121 |
filename="model.pt",
|
|
@@ -128,75 +115,86 @@ class ModelInference:
|
|
| 128 |
token=token
|
| 129 |
)
|
| 130 |
|
| 131 |
-
# Load
|
| 132 |
-
self.logger.info("Loading model...")
|
| 133 |
-
model_data = torch.load(
|
| 134 |
-
model_path,
|
| 135 |
-
map_location=self.device,
|
| 136 |
-
weights_only=False
|
| 137 |
-
)
|
| 138 |
-
|
| 139 |
-
# Load tokenizer
|
| 140 |
self.logger.info("Loading tokenizer...")
|
| 141 |
with open(tokenizer_path, 'r', encoding='utf-8') as f:
|
| 142 |
tokenizer_data = json.load(f)
|
| 143 |
|
| 144 |
-
# Initialize
|
| 145 |
-
|
| 146 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 147 |
|
|
|
|
|
|
|
| 148 |
self.model = ImprovedTransformer(
|
| 149 |
vocab_size=vocab_size,
|
| 150 |
-
d_model=
|
| 151 |
-
nhead=
|
| 152 |
-
num_encoder_layers=
|
| 153 |
-
num_decoder_layers=
|
| 154 |
-
dim_feedforward=
|
| 155 |
-
dropout=model_config.get('dropout', 0.1),
|
| 156 |
-
max_seq_length=model_config.get('max_seq_length', 128)
|
| 157 |
).to(self.device)
|
| 158 |
|
|
|
|
| 159 |
self.model.load_state_dict(model_data['model_state_dict'])
|
| 160 |
self.model.eval()
|
| 161 |
|
| 162 |
-
|
| 163 |
-
self.tokenizer = EnhancedTokenizer(tokenizer_data['vocab'])
|
| 164 |
-
|
| 165 |
-
self.logger.info("Model and tokenizer loaded successfully")
|
| 166 |
|
| 167 |
except Exception as e:
|
| 168 |
self.logger.error(f"Error loading model: {str(e)}")
|
| 169 |
raise
|
| 170 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 171 |
@torch.no_grad()
|
| 172 |
def generate_answer(self, input_text: str) -> str:
|
| 173 |
try:
|
| 174 |
-
# Clean input
|
| 175 |
input_text = input_text.strip()
|
| 176 |
self.logger.info(f"Processing input: {input_text}")
|
| 177 |
|
| 178 |
-
# Tokenize
|
| 179 |
-
input_ids = self.
|
| 180 |
input_tensor = torch.tensor([input_ids]).to(self.device)
|
| 181 |
|
| 182 |
# Initialize response
|
| 183 |
-
response_ids = [self.
|
|
|
|
| 184 |
|
| 185 |
-
# Generate
|
| 186 |
-
for _ in range(150):
|
| 187 |
-
|
| 188 |
-
|
| 189 |
-
torch.tensor([response_ids]).to(self.device)
|
| 190 |
-
)
|
| 191 |
-
next_token = curr_output[0, -1].argmax().item()
|
| 192 |
|
| 193 |
-
if next_token == self.
|
| 194 |
break
|
| 195 |
|
| 196 |
response_ids.append(next_token)
|
|
|
|
| 197 |
|
| 198 |
-
# Decode
|
| 199 |
-
answer = self.
|
| 200 |
answer = answer.replace("<assistant>", "").replace("<eos>", "").strip()
|
| 201 |
|
| 202 |
self.logger.info(f"Generated response: {answer}")
|
|
@@ -206,17 +204,10 @@ class ModelInference:
|
|
| 206 |
self.logger.error(f"Error generating answer: {str(e)}")
|
| 207 |
return f"Error generating answer: {str(e)}"
|
| 208 |
|
| 209 |
-
# Initialize model
|
| 210 |
-
|
| 211 |
-
print("Initializing model...")
|
| 212 |
-
model = ModelInference()
|
| 213 |
-
print("Model initialized successfully")
|
| 214 |
-
except Exception as e:
|
| 215 |
-
print(f"Error initializing model: {str(e)}")
|
| 216 |
-
model = None
|
| 217 |
|
| 218 |
def process_input(input_text):
|
| 219 |
-
"""Process input through Gradio"""
|
| 220 |
global model
|
| 221 |
try:
|
| 222 |
if model is None:
|
|
@@ -243,8 +234,6 @@ interface = gr.Interface(
|
|
| 243 |
Model Repository: {MODEL_REPO}
|
| 244 |
Current User: {CURRENT_USER}
|
| 245 |
Last Updated: {CURRENT_UTC} UTC
|
| 246 |
-
|
| 247 |
-
Enter your question and click submit to get a response.
|
| 248 |
""",
|
| 249 |
theme=gr.themes.Soft(),
|
| 250 |
examples=[
|
|
@@ -254,5 +243,5 @@ interface = gr.Interface(
|
|
| 254 |
]
|
| 255 |
)
|
| 256 |
|
| 257 |
-
#
|
| 258 |
interface.launch()
|
|
|
|
| 9 |
# Configuration constants
|
| 10 |
MODEL_REPO = "Gajendra5490/Scrached_Trained_Model"
|
| 11 |
CURRENT_USER = "gajendra82"
|
| 12 |
+
CURRENT_UTC = "2025-05-06 15:52:11"
|
| 13 |
|
| 14 |
def setup_logging():
|
| 15 |
logging.basicConfig(
|
|
|
|
| 23 |
|
| 24 |
logger = setup_logging()
|
| 25 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 26 |
class ImprovedTransformer(torch.nn.Module):
|
| 27 |
+
def __init__(
|
| 28 |
+
self,
|
| 29 |
+
vocab_size,
|
| 30 |
+
d_model=512,
|
| 31 |
+
nhead=8,
|
| 32 |
+
num_encoder_layers=6,
|
| 33 |
+
num_decoder_layers=6,
|
| 34 |
+
dim_feedforward=2048,
|
| 35 |
+
dropout=0.1,
|
| 36 |
+
max_seq_length=128
|
| 37 |
+
):
|
| 38 |
super().__init__()
|
| 39 |
|
| 40 |
self.d_model = d_model
|
| 41 |
self.embedding = torch.nn.Embedding(vocab_size, d_model)
|
|
|
|
| 42 |
|
| 43 |
+
# Position encoding
|
| 44 |
+
position = torch.arange(max_seq_length).unsqueeze(1)
|
| 45 |
+
div_term = torch.exp(torch.arange(0, d_model, 2) * (-torch.log(torch.tensor(10000.0)) / d_model))
|
| 46 |
+
pe = torch.zeros(max_seq_length, 1, d_model)
|
| 47 |
+
pe[:, 0, 0::2] = torch.sin(position * div_term)
|
| 48 |
+
pe[:, 0, 1::2] = torch.cos(position * div_term)
|
| 49 |
+
self.register_buffer('pe', pe)
|
| 50 |
+
|
| 51 |
+
# Main transformer
|
| 52 |
+
self.transformer = torch.nn.Transformer(
|
| 53 |
d_model=d_model,
|
| 54 |
nhead=nhead,
|
| 55 |
+
num_encoder_layers=num_encoder_layers,
|
| 56 |
+
num_decoder_layers=num_decoder_layers,
|
| 57 |
dim_feedforward=dim_feedforward,
|
| 58 |
dropout=dropout,
|
| 59 |
batch_first=True
|
| 60 |
)
|
| 61 |
|
| 62 |
+
# Output layers
|
| 63 |
+
self.output_layer = torch.nn.Linear(d_model, vocab_size)
|
| 64 |
+
self.norm = torch.nn.LayerNorm(d_model)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 65 |
|
| 66 |
def forward(self, src, tgt):
|
| 67 |
+
# Create masks
|
| 68 |
+
src_mask = self.transformer.generate_square_subsequent_mask(src.size(1)).to(src.device)
|
| 69 |
+
tgt_mask = self.transformer.generate_square_subsequent_mask(tgt.size(1)).to(tgt.device)
|
| 70 |
|
| 71 |
+
# Embeddings
|
| 72 |
+
src = self.embedding(src) * torch.sqrt(torch.tensor(self.d_model))
|
| 73 |
+
tgt = self.embedding(tgt) * torch.sqrt(torch.tensor(self.d_model))
|
| 74 |
+
|
| 75 |
+
# Add positional encoding
|
| 76 |
+
src = src + self.pe[:src.size(1)].transpose(0, 1)
|
| 77 |
+
tgt = tgt + self.pe[:tgt.size(1)].transpose(0, 1)
|
| 78 |
+
|
| 79 |
+
# Transform
|
| 80 |
+
output = self.transformer(
|
| 81 |
+
src,
|
| 82 |
+
tgt,
|
| 83 |
+
src_mask=src_mask,
|
| 84 |
+
tgt_mask=tgt_mask
|
| 85 |
+
)
|
| 86 |
|
| 87 |
+
# Output processing
|
| 88 |
+
output = self.norm(output)
|
| 89 |
+
return self.output_layer(output)
|
| 90 |
|
| 91 |
class ModelInference:
|
| 92 |
def __init__(self):
|
|
|
|
| 97 |
|
| 98 |
def load_model(self):
|
| 99 |
try:
|
| 100 |
+
token = os.environ.get('HF_TOKEN')
|
| 101 |
+
if not token:
|
| 102 |
+
raise ValueError("HF_TOKEN not found in environment variables")
|
| 103 |
|
| 104 |
+
# Download files
|
| 105 |
+
self.logger.info(f"Downloading from {MODEL_REPO}")
|
| 106 |
model_path = hf_hub_download(
|
| 107 |
repo_id=MODEL_REPO,
|
| 108 |
filename="model.pt",
|
|
|
|
| 115 |
token=token
|
| 116 |
)
|
| 117 |
|
| 118 |
+
# Load tokenizer first
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 119 |
self.logger.info("Loading tokenizer...")
|
| 120 |
with open(tokenizer_path, 'r', encoding='utf-8') as f:
|
| 121 |
tokenizer_data = json.load(f)
|
| 122 |
|
| 123 |
+
# Initialize tokenizer
|
| 124 |
+
self.vocab = tokenizer_data['vocab']
|
| 125 |
+
self.special_tokens = {
|
| 126 |
+
"<user>": len(self.vocab),
|
| 127 |
+
"<assistant>": len(self.vocab) + 1,
|
| 128 |
+
"<sep>": len(self.vocab) + 2,
|
| 129 |
+
"<eos>": len(self.vocab) + 3
|
| 130 |
+
}
|
| 131 |
+
|
| 132 |
+
# Load model
|
| 133 |
+
self.logger.info("Loading model...")
|
| 134 |
+
model_data = torch.load(
|
| 135 |
+
model_path,
|
| 136 |
+
map_location=self.device
|
| 137 |
+
)
|
| 138 |
|
| 139 |
+
# Initialize model with correct vocab size
|
| 140 |
+
vocab_size = len(self.vocab) + len(self.special_tokens)
|
| 141 |
self.model = ImprovedTransformer(
|
| 142 |
vocab_size=vocab_size,
|
| 143 |
+
d_model=512,
|
| 144 |
+
nhead=8,
|
| 145 |
+
num_encoder_layers=3,
|
| 146 |
+
num_decoder_layers=3,
|
| 147 |
+
dim_feedforward=2048
|
|
|
|
|
|
|
| 148 |
).to(self.device)
|
| 149 |
|
| 150 |
+
# Load state dict
|
| 151 |
self.model.load_state_dict(model_data['model_state_dict'])
|
| 152 |
self.model.eval()
|
| 153 |
|
| 154 |
+
self.logger.info("Model loaded successfully")
|
|
|
|
|
|
|
|
|
|
| 155 |
|
| 156 |
except Exception as e:
|
| 157 |
self.logger.error(f"Error loading model: {str(e)}")
|
| 158 |
raise
|
| 159 |
|
| 160 |
+
def encode(self, text):
|
| 161 |
+
tokens = text.split()
|
| 162 |
+
return [self.vocab.get(token, 0) if token not in self.special_tokens
|
| 163 |
+
else self.special_tokens[token] for token in tokens]
|
| 164 |
+
|
| 165 |
+
def decode(self, ids):
|
| 166 |
+
reverse_vocab = {v: k for k, v in self.vocab.items()}
|
| 167 |
+
reverse_special = {v: k for k, v in self.special_tokens.items()}
|
| 168 |
+
return " ".join(reverse_vocab.get(id, reverse_special.get(id, "<unk>"))
|
| 169 |
+
for id in ids)
|
| 170 |
+
|
| 171 |
@torch.no_grad()
|
| 172 |
def generate_answer(self, input_text: str) -> str:
|
| 173 |
try:
|
|
|
|
| 174 |
input_text = input_text.strip()
|
| 175 |
self.logger.info(f"Processing input: {input_text}")
|
| 176 |
|
| 177 |
+
# Tokenize
|
| 178 |
+
input_ids = self.encode(f"<user> {input_text} <sep>")
|
| 179 |
input_tensor = torch.tensor([input_ids]).to(self.device)
|
| 180 |
|
| 181 |
# Initialize response
|
| 182 |
+
response_ids = [self.special_tokens["<assistant>"]]
|
| 183 |
+
response_tensor = torch.tensor([response_ids]).to(self.device)
|
| 184 |
|
| 185 |
+
# Generate
|
| 186 |
+
for _ in range(150):
|
| 187 |
+
output = self.model(input_tensor, response_tensor)
|
| 188 |
+
next_token = output[0, -1].argmax().item()
|
|
|
|
|
|
|
|
|
|
| 189 |
|
| 190 |
+
if next_token == self.special_tokens["<eos>"]:
|
| 191 |
break
|
| 192 |
|
| 193 |
response_ids.append(next_token)
|
| 194 |
+
response_tensor = torch.tensor([response_ids]).to(self.device)
|
| 195 |
|
| 196 |
+
# Decode
|
| 197 |
+
answer = self.decode(response_ids)
|
| 198 |
answer = answer.replace("<assistant>", "").replace("<eos>", "").strip()
|
| 199 |
|
| 200 |
self.logger.info(f"Generated response: {answer}")
|
|
|
|
| 204 |
self.logger.error(f"Error generating answer: {str(e)}")
|
| 205 |
return f"Error generating answer: {str(e)}"
|
| 206 |
|
| 207 |
+
# Initialize model
|
| 208 |
+
model = None
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 209 |
|
| 210 |
def process_input(input_text):
|
|
|
|
| 211 |
global model
|
| 212 |
try:
|
| 213 |
if model is None:
|
|
|
|
| 234 |
Model Repository: {MODEL_REPO}
|
| 235 |
Current User: {CURRENT_USER}
|
| 236 |
Last Updated: {CURRENT_UTC} UTC
|
|
|
|
|
|
|
| 237 |
""",
|
| 238 |
theme=gr.themes.Soft(),
|
| 239 |
examples=[
|
|
|
|
| 243 |
]
|
| 244 |
)
|
| 245 |
|
| 246 |
+
# Launch
|
| 247 |
interface.launch()
|