mrmax14 commited on
Commit
2c51c74
·
1 Parent(s): 7611348

init commit

Browse files
Files changed (2) hide show
  1. app.py +29 -46
  2. requirements.txt +2 -5
app.py CHANGED
@@ -1,53 +1,36 @@
1
- import os
2
- from transformers import LlamaTokenizer, LlamaForCausalLM
3
  import torch
4
 
5
- # Налаштування шляхів
6
- repo_path = "meta-llama/Llama-2-7b-chat" # Локальний шлях до моделі
7
- config_path = os.path.join(repo_path, "config.json")
8
- tokenizer_path = os.path.join(repo_path, "tokenizer.model")
9
-
10
- # Перевірка наявності необхідних файлів
11
- if not os.path.exists(repo_path):
12
- raise FileNotFoundError(f"The specified repository path does not exist: {repo_path}")
13
-
14
- required_files = ["config.json", "tokenizer.model", "consolidated.00.pth", "params.json"]
15
- for file in required_files:
16
- if not os.path.exists(os.path.join(repo_path, file)):
17
- raise FileNotFoundError(f"Missing required file in {repo_path}: {file}")
18
-
19
- # Завантаження токенізатора
20
- print("Loading tokenizer...")
21
- tokenizer = LlamaTokenizer(vocab_file=tokenizer_path)
22
- print("Tokenizer loaded successfully!")
23
-
24
- # Завантаження моделі
25
- print("Loading model...")
26
- device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
27
-
28
- model = LlamaForCausalLM.from_pretrained(
29
- repo_path,
30
- torch_dtype=torch.float16 if torch.cuda.is_available() else torch.float32,
31
- low_cpu_mem_usage=True,
32
- )
33
- model = model.to(device)
34
- print("Model loaded successfully!")
35
 
36
- # Приклад використання
37
- input_text = "Привіт! Як ти себе почуваєш сьогодні?"
38
- inputs = tokenizer(input_text, return_tensors="pt").to(device)
39
 
40
- print("Generating response...")
41
- with torch.no_grad():
42
- outputs = model.generate(
43
  inputs["input_ids"],
44
- max_length=100,
45
- temperature=0.7,
46
- top_k=50,
47
- top_p=0.9,
48
- do_sample=True,
49
  )
50
 
51
- response = tokenizer.decode(outputs[0], skip_special_tokens=True)
52
- print("Response:")
53
- print(response)
 
 
 
 
1
+ from transformers import AutoModelForCausalLM, AutoTokenizer
 
2
  import torch
3
 
4
+ def main():
5
+ # Завантажуємо токенайзер
6
+ model_name = "meta-llama/Llama-2-7b-chat"
7
+ print(f"Завантажуємо модель {model_name}...")
8
+
9
+ tokenizer = AutoTokenizer.from_pretrained(model_name)
10
+
11
+ # Завантажуємо модель
12
+ model = AutoModelForCausalLM.from_pretrained(
13
+ model_name,
14
+ torch_dtype=torch.float32, # Використання full precision для CPU
15
+ device_map=None # Вимкнення автоматичного розподілу по GPU
16
+ )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
17
 
18
+ # Тестове введення
19
+ input_text = "Hello, how can I assist you today?"
20
+ inputs = tokenizer(input_text, return_tensors="pt")
21
 
22
+ # Генерація тексту
23
+ output = model.generate(
 
24
  inputs["input_ids"],
25
+ max_length=50, # Максимальна довжина відповіді
26
+ num_return_sequences=1, # Кількість відповідей
27
+ do_sample=True, # Випадкове семплування для різноманіття
28
+ temperature=0.7 # Регулювання "креативності"
 
29
  )
30
 
31
+ # Декодуємо та виводимо результат
32
+ decoded_output = tokenizer.decode(output[0], skip_special_tokens=True)
33
+ print(f"\nВідповідь моделі: {decoded_output}")
34
+
35
+ if __name__ == "__main__":
36
+ main()
requirements.txt CHANGED
@@ -1,5 +1,2 @@
1
- torch==1.13.1
2
- transformers==4.30.0
3
- gradio==3.22.0
4
- huggingface-hub>=0.13.0
5
- sentencepiece
 
1
+ transformers==4.33.0
2
+ torch==2.0.1