Views
No views yet
1# GITHUB REPO
2PUBLIC_WHL_URL="https://github.com/ysnrfd/ysnrfd_architecture_whl/releases/download/WHL/ysnrfd-1.1.0-py3-none-any.whl"
3
4# PACKAGE INSTALL
5!pip install $PUBLIC_WHL_URL1import torch
2from transformers import AutoTokenizer, PreTrainedTokenizer
3import logging
4
5# Logging settings
6logging.basicConfig(level=logging.INFO)
7logger = logging.getLogger(__name__)
8
9# ----------------------------------------------------
10# 1. Importing architecture classes (from your locally installed package)
11# ----------------------------------------------------
12# This line loads Ysnrfd classes from your installed package (.whl), without needing the source code.
13try:
14 from ysnrfd import YsnrfdConfig, YsnrfdForCausalLM
15 logger.info(" Ysnrfd classes were successfully loaded from the local package.")
16except ImportError:
17 logger.error(" Import error: Make sure the 'ysnrfd' package (.whl file) is installed.")
18 # You may stop the program here
19 exit()
20
21# ----------------------------------------------------
22# 2. Setting the model identifier (Hugging Face Hub)
23# ----------------------------------------------------
24# This is the trained model identifier and its weights.
25HF_MODEL_ID = "ysn-rfd/ysnrfd-base-V2"
26
27# Device selection (GPU or CPU)
28device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
29logger.info(f"Using device: {device}")
30
31# ----------------------------------------------------
32# 3. Loading the tokenizer and configuration
33# ----------------------------------------------------
34try:
35 # Load tokenizer using AutoTokenizer standard
36 tokenizer: PreTrainedTokenizer = AutoTokenizer.from_pretrained(HF_MODEL_ID)
37
38 # Load config.json from the Hub and inject it into the custom YsnrfdConfig class
39 # This class comes from your package, but the values are read from the Hub's config.json.
40 config: YsnrfdConfig = YsnrfdConfig.from_pretrained(HF_MODEL_ID)
41
42 # Set pad_token_id if not defined (required for generation)
43 if tokenizer.pad_token_id is None:
44 tokenizer.pad_token_id = tokenizer.eos_token_id
45
46 logger.info(f" Configuration and tokenizer loaded successfully. Config type: {type(config)}")
47
48except Exception as e:
49 logger.error(f" Error loading configuration/tokenizer from the Hub: {e}")
50 exit()
51
52
53# ----------------------------------------------------
54# 4. Loading the model and weights
55# ----------------------------------------------------
56try:
57 # Load model weights from the Hub into your custom architecture class
58 model: YsnrfdForCausalLM = YsnrfdForCausalLM.from_pretrained(HF_MODEL_ID, config=config)
59 model.to(device)
60 model.eval()
61 logger.info(" Model and weights loaded successfully and moved to device.")
62except Exception as e:
63 logger.error(f" Error loading model from the Hub: {e}")
64 exit()
65
66# ----------------------------------------------------
67# 5. Running Inference (text generation)
68# ----------------------------------------------------
69prompt = "On a sunny day in Tehran,"
70logger.info(f"\n--- Starting text generation ---")
71logger.info(f"Prompt: {prompt}")
72
73# Tokenize the input
74input_ids = tokenizer.encode(prompt, return_tensors="pt").to(device)
75
76# Run generation
77with torch.no_grad():
78 outputs = model.generate(
79 input_ids,
80 max_length=100,
81 do_sample=True,
82 top_p=0.9,
83 temperature=0.7,
84 pad_token_id=tokenizer.pad_token_id
85 )
86
87# Decode output
88generated_text = tokenizer.decode(outputs[0], skip_special_tokens=True)
89
90logger.info("\n--- Final Model Output ---")
91print(generated_text)
92logger.info("--- Done ---")
93
94
95# ----------------------------------------------------
96# 4. MODEL WEIGTH LOADING
97# ----------------------------------------------------
98try:
99 # loading moedl weight
100 model: YsnrfdForCausalLM = YsnrfdForCausalLM.from_pretrained(HF_MODEL_ID, config=config)
101 model.to(device)
102 model.eval()
103 logger.info(" MODEL AND WEIGHT ARE SUCCESFULY LOADED")
104except Exception as e:
105 logger.error(f" MODEL LOADING ERROR OF Hub: {e}")
106 exit()
107
108# ----------------------------------------------------
109# 5. INFERENCE / RUN
110# ----------------------------------------------------
111prompt = "IN A SUNNY DAY"
112logger.info(f"\n--- START TEXT GENERATION ---")
113logger.info(f"پرامپت: {prompt}")
114
115# TOKENIZING INPUTS
116input_ids = tokenizer.encode(prompt, return_tensors="pt").to(device)
117
118# RUN GENERATION
119with torch.no_grad():
120 outputs = model.generate(
121 input_ids,
122 max_length=100,
123 do_sample=True,
124 top_p=0.9,
125 temperature=0.7,
126 pad_token_id=tokenizer.pad_token_id
127 )
128
129# decoding output
130generated_text = tokenizer.decode(outputs[0], skip_special_tokens=True)
131
132logger.info("\n--- FINAL MODEL OUTPUT ---")
133print(generated_text)
134logger.info("--- END ---")