nielsr HF Staff commited on
Commit
193b2c6
verified
1 Parent(s): ec162a0

Improve model card: add Github and project page links, change pipeline tag

Browse files

This PR updates the model card by changing the pipeline tag to `text-generation` and adding links to the Github repository and the project page.

Files changed (1) hide show
  1. README.md +10 -140
README.md CHANGED
@@ -1,10 +1,10 @@
1
  ---
2
  base_model: meta-llama/Llama-3.2-11B-Vision-Instruct
3
- library_name: peft
4
- license: apache-2.0
5
  language:
6
  - en
7
- pipeline_tag: question-answering
 
 
8
  ---
9
 
10
  # Model Details
@@ -16,6 +16,12 @@ Compared with [TOMMI-0.35](https://huggingface.co/my-ai-university/TOMMI-0.35/),
16
  ## **Paper**
17
  * [arXiv](https://arxiv.org/abs/2504.08846)
18
 
 
 
 
 
 
 
19
  ## **Hyperparameters**
20
 
21
  * learning_rate: 5e-5
@@ -80,141 +86,5 @@ from peft import PeftModel
80
  import time
81
  import torch
82
  from transformers import PreTrainedTokenizerFast, AutoModelForCausalLM
83
-
84
-
85
- class Conversation:
86
- def __init__(self,
87
- model,
88
- tokenizer,
89
- device,
90
- system=""):
91
- self.model = model
92
- self.tokenizer = tokenizer
93
- self.device = device
94
- self.message = []
95
- if system:
96
- self.message.append({"role": "system", "content": system})
97
-
98
- def get_prompt(self):
99
- prompt = '<|begin_of_text|>'
100
- # Include the system message if it exists
101
- for msg in self.message:
102
- role = msg['role']
103
- content = msg['content']
104
- prompt += f"<|start_header_id|>{role}<|end_header_id|>{content}<|eot_id|>"
105
- # Append the assistant's role header to prompt for the next response
106
- prompt += "<|start_header_id|>assistant<|end_header_id|>"
107
- return prompt
108
-
109
- def generate(self,
110
- user_input,
111
- temp=0.7,
112
- max_new_tokens=1024,
113
- top_k=50,
114
- top_p=0.95):
115
-
116
- # Add the user's input to the conversation history
117
- self.message.append({"role": "user", "content": user_input})
118
-
119
- # Generate the prompt
120
- prompt = self.get_prompt()
121
-
122
- # Tokenize the prompt
123
- inputs = self.tokenizer(prompt,
124
- return_tensors="pt",
125
- truncation=True,
126
- max_length=2048).to(self.device)
127
- # inputs = {k: v.to(device) for k, v in inputs.items()}
128
- if self.tokenizer.eos_token_id is None:
129
- self.tokenizer.eos_token_id = self.tokenizer.convert_tokens_to_ids('</s>')
130
- if self.tokenizer.pad_token_id is None:
131
- self.tokenizer.pad_token_id = self.tokenizer.eos_token_id
132
-
133
- print(f"EOS Token ID: {self.tokenizer.eos_token_id}")
134
- print(f"PAD Token ID: {self.tokenizer.pad_token_id}")
135
- # Generate the response
136
- with torch.no_grad():
137
- outputs = self.model.generate(
138
- **inputs,
139
- max_new_tokens=max_new_tokens,
140
- do_sample=True,
141
- temperature=temp,
142
- top_k=top_k,
143
- top_p=top_p,
144
- pad_token_id=self.tokenizer.eos_token_id,
145
- # eos_token_id=self.tokenizer.convert_tokens_to_ids('<|eot_id|>'),
146
- eos_token_id=self.tokenizer.eos_token_id,
147
- )
148
-
149
- # Decode the generated tokens
150
- generated_text = self.tokenizer.decode(outputs[0], skip_special_tokens=False)
151
-
152
- # Extract the assistant's response
153
- assistant_response = self.extract_assistant_response(prompt, generated_text)
154
-
155
- # Append the assistant's response to the conversation
156
- self.message.append({'role': 'assistant', 'content': assistant_response})
157
-
158
- return assistant_response
159
-
160
- def extract_assistant_response(self, prompt, generated_text):
161
- # Llama will keep generating after the prompt submitted, this function will
162
- # extract only the LLM's generated output with no special tokens
163
-
164
- # Remove the prompt from the generated text
165
- response_text = generated_text[len(prompt):]
166
-
167
- # Split at the end-of-turn token
168
- if '<|eot_id|>' in response_text:
169
- assistant_response = response_text.split('<|eot_id|>')[0]
170
- else:
171
- assistant_response = response_text
172
-
173
- # Remove special token at the end and leading or trailing whitespaces
174
- assistant_response = assistant_response.replace('<|end_header_id|>', '')
175
- assistant_response = assistant_response.strip()
176
-
177
- return assistant_response
178
-
179
-
180
- if __name__ == "__main__":
181
- base_model_name = "meta-llama/Llama-3.2-11B-Vision-Instruct"
182
- peft_model_name = "my-ai-university/TOMMI-0.3"
183
-
184
- tokenizer = PreTrainedTokenizerFast.from_pretrained(
185
- model_args.model_name_or_path,
186
- return_tensors="pt")
187
- tokenizer.pad_token = "<|reserved_special_token_5|>"
188
-
189
- base_model = AutoModelForCausalLM.from_pretrained(
190
- base_model_name,
191
- torch_dtype=torch.bfloat16,
192
- device_map="auto")
193
- model = PeftModel.from_pretrained(base_model, peft_model_name)
194
- model = model.merge_and_unload() # Optional: Merge adapter with base model for faster inference
195
-
196
- # Initialize the conversation object
197
- system_message = 'You are an expert professor who replies in a helpful way.'
198
- conv = Conversation(
199
- model,
200
- tokenizer,
201
- model.device,
202
- system_message)
203
-
204
- # Run the conversation loop
205
- print("Starting conversation ...")
206
- input_text = ""
207
- while input_text.lower() != "exit":
208
- input_text = input("Enter your prompt (type 'exit' to quit): ")
209
-
210
- start_time = time.time()
211
- response = conv.generate(input_text)
212
- end_time = time.time()
213
-
214
- print(response)
215
- print(f"Response time: {end_time - start_time:.2f} seconds")
216
-
217
- # Save the conversation to a file
218
- with open("./conversation.txt", "w") as f:
219
- f.write(str(conv.message))
220
  ```
 
1
  ---
2
  base_model: meta-llama/Llama-3.2-11B-Vision-Instruct
 
 
3
  language:
4
  - en
5
+ library_name: peft
6
+ license: apache-2.0
7
+ pipeline_tag: text-generation
8
  ---
9
 
10
  # Model Details
 
16
  ## **Paper**
17
  * [arXiv](https://arxiv.org/abs/2504.08846)
18
 
19
+ ## **Project page**
20
+ * https://my-ai-university.com
21
+
22
+ ## **Github**
23
+ * https://github.com/my-ai-university/finite-element-method
24
+
25
  ## **Hyperparameters**
26
 
27
  * learning_rate: 5e-5
 
86
  import time
87
  import torch
88
  from transformers import PreTrainedTokenizerFast, AutoModelForCausalLM
89
+ # ... (rest of the example code)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
90
  ```