Tjay143 commited on
Commit
f098411
1 Parent(s): e0145be

Upload merge_lora.py

Browse files
Files changed (1) hide show
  1. merge_lora.py +56 -0
merge_lora.py ADDED
@@ -0,0 +1,56 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ from safetensors.torch import load_file, save_file
3
+ from transformers import AutoTokenizer, AutoModel
4
+ from diffusers import StableDiffusionPipeline
5
+ import torch
6
+
7
+ file1_path = r"D:\hansika lora\hansika lora.safetensors"
8
+ file2_path = r"D:\first lora\first lora.safetensors"
9
+ file3_path = r"D:\NSFW_master.safetensors"
10
+ merged_file_path = r"D:\new lora\new lora.safetensors"
11
+
12
+ print("File 1 Path:", file1_path)
13
+ print("File exists:", os.path.exists(file1_path))
14
+ print("File size:", os.path.getsize(file1_path))
15
+
16
+ def load_tensors(file_path):
17
+ return load_file(file_path)
18
+
19
+ try:
20
+ tensors1 = load_tensors(file1_path)
21
+ tensors2 = load_tensors(file2_path)
22
+ tensors3 = load_tensors(file3_path)
23
+
24
+ # Merge tensors
25
+ merged_tensors = {**tensors1, **tensors2, **tensors3}
26
+
27
+ # Save merged tensors
28
+ save_file(merged_tensors, merged_file_path)
29
+
30
+ print(f"Merged file saved at: {merged_file_path}")
31
+
32
+ # Validate the merged file
33
+ merged_load = load_file(merged_file_path)
34
+ print(merged_load.keys())
35
+
36
+ try:
37
+ # Load the tokenizer and model
38
+ tokenizer = AutoTokenizer.from_pretrained(merged_file_path)
39
+ model = AutoModel.from_pretrained(merged_file_path, from_tf=False, from_safetensors=True)
40
+ pipeline = StableDiffusionPipeline.from_pretrained(merged_file_path, torch_dtype=torch.float16)
41
+ print("It is successfully loaded!")
42
+ except Exception as e:
43
+ print(f"Error: {e}")
44
+ except Exception as e:
45
+ print(f"An error occurred: {e}")
46
+
47
+
48
+
49
+
50
+
51
+
52
+
53
+
54
+
55
+
56
+