|
|
from transformers import AutoTokenizer, AutoModelForCausalLM
|
|
|
import torch
|
|
|
import os
|
|
|
|
|
|
class DistilGPT2Model:
|
|
|
def __init__(self, model_name="distilgpt2", model_path="models"):
|
|
|
self.model_path = model_path
|
|
|
|
|
|
self.model_name = model_name
|
|
|
|
|
|
os.makedirs(model_path, exist_ok=True)
|
|
|
|
|
|
if os.path.exists(os.path.join(model_path, "model")):
|
|
|
print("Loading model from local storage...")
|
|
|
self.tokenizer = AutoTokenizer.from_pretrained(os.path.join(model_path, "model"))
|
|
|
self.model = AutoModelForCausalLM.from_pretrained(os.path.join(model_path, "model"))
|
|
|
else:
|
|
|
print("Downloading model from Hugging Face...")
|
|
|
self.tokenizer = AutoTokenizer.from_pretrained(model_name)
|
|
|
self.model = AutoModelForCausalLM.from_pretrained(model_name)
|
|
|
|
|
|
|
|
|
print("Saving model to local storage...")
|
|
|
self.model.save_pretrained(os.path.join(model_path, "model"))
|
|
|
self.tokenizer.save_pretrained(os.path.join(model_path, "model"))
|
|
|
|
|
|
self.device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
|
|
self.model.to(self.device)
|
|
|
|
|
|
def generate_text(self, prompt: str, max_length: int = 50):
|
|
|
inputs = self.tokenizer.encode(prompt, return_tensors="pt").to(self.device)
|
|
|
outputs = self.model.generate(
|
|
|
inputs,
|
|
|
max_length=max_length,
|
|
|
do_sample=True,
|
|
|
top_k=50,
|
|
|
pad_token_id=self.tokenizer.eos_token_id,
|
|
|
)
|
|
|
return self.tokenizer.decode(outputs[0], skip_special_tokens=True)
|
|
|
|
|
|
|
|
|
parth = DistilGPT2Model()
|
|
|
|