File size: 1,766 Bytes
9f0bffc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
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)
            
            # Save model locally
            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)

# Singleton instance for reuse
parth = DistilGPT2Model()