NoaiGPT commited on
Commit
58a16f2
1 Parent(s): 453187f

asdasdasdasd

Browse files
Files changed (1) hide show
  1. app.py +82 -50
app.py CHANGED
@@ -1,62 +1,94 @@
 
1
  import torch
2
  from transformers import AutoModelForCausalLM, AutoTokenizer
3
- from huggingface_hub import HfApi
4
- import gradio as gr
5
- import os
6
  from peft import PeftModel, PeftConfig
 
 
 
 
 
 
7
 
8
- # Function to merge models using PEFT
9
  def merge_models():
10
- base_model_name = "meta-llama/Meta-Llama-3-8B"
11
- finetuned_model_name = "NoaiGPT/autotrain-14mrs-fc44l"
12
 
13
- # Load the base model
14
- base_model = AutoModelForCausalLM.from_pretrained(base_model_name)
15
- base_tokenizer = AutoTokenizer.from_pretrained(base_model_name)
16
 
17
- # Load the fine-tuned model using PEFT
18
- peft_config = PeftConfig.from_pretrained(finetuned_model_name)
19
- finetuned_model = PeftModel.from_pretrained(base_model, peft_config)
20
 
21
- # Merge the models (simple weight averaging here for demonstration; adjust as needed)
22
- for param_base, param_finetuned in zip(base_model.parameters(), finetuned_model.parameters()):
23
- param_base.data = (param_base.data + param_finetuned.data) / 2
24
 
25
- # Save the merged model
26
- merged_model_name = "./merged_model"
27
- base_model.save_pretrained(merged_model_name)
28
- base_tokenizer.save_pretrained(merged_model_name)
29
 
30
- return merged_model_name
31
 
32
- # Function to upload the merged model to Hugging Face Hub
33
- def upload_to_hf(repo_id, merged_model_name):
34
  api = HfApi()
35
- model_files = [os.path.join(merged_model_name, f) for f in os.listdir(merged_model_name)]
36
-
37
- for file in model_files:
38
- api.upload_file(
39
- path_or_fileobj=file,
40
- path_in_repo=os.path.basename(file),
41
- repo_id=repo_id,
42
- repo_type="model"
43
- )
44
-
45
- return f"Model uploaded to Hugging Face Hub at {repo_id}."
46
-
47
- # Gradio function to handle the merge button click
48
- def merge_button_clicked():
49
- merged_model_name = merge_models()
50
- repo_id = "NoaiGPT/autotrain-14mrs-fc44l"
51
- return upload_to_hf(repo_id, merged_model_name)
52
-
53
- # Create the Gradio interface
54
- with gr.Blocks() as demo:
55
- with gr.Row():
56
- merge_button = gr.Button("Merge Models")
57
- output = gr.Textbox(label="Output")
58
-
59
- merge_button.click(merge_button_clicked, outputs=output)
60
-
61
- # Launch the Gradio interface
62
- demo.launch()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import json
2
  import torch
3
  from transformers import AutoModelForCausalLM, AutoTokenizer
 
 
 
4
  from peft import PeftModel, PeftConfig
5
+ from huggingface_hub import HfApi, Repository
6
+ import gradio as gr
7
+
8
+ # Global variables to store the model and tokenizer
9
+ merged_model = None
10
+ tokenizer = None
11
 
 
12
  def merge_models():
13
+ print("Loading base model...")
14
+ base_model = AutoModelForCausalLM.from_pretrained("meta-llama/Meta-Llama-3-8B-Instruct")
15
 
16
+ print("Loading fine-tuned model...")
17
+ peft_config = PeftConfig.from_pretrained("NoaiGPT/autotrain-14mrs-fc44l")
18
+ fine_tuned_model = PeftModel.from_pretrained(base_model, peft_config)
19
 
20
+ print("Merging models...")
21
+ merged_model = fine_tuned_model.merge_and_unload()
 
22
 
23
+ print("Saving merged model...")
24
+ merged_model.save_pretrained("merged_model")
 
25
 
26
+ print("Saving tokenizer...")
27
+ tokenizer = AutoTokenizer.from_pretrained("meta-llama/Meta-Llama-3-8B-Instruct")
28
+ tokenizer.save_pretrained("merged_model")
 
29
 
30
+ return merged_model, tokenizer
31
 
32
+ def push_to_hub(repo_name):
33
+ print(f"Pushing merged model to Hugging Face Hub: {repo_name}")
34
  api = HfApi()
35
+
36
+ try:
37
+ api.create_repo(repo_name, private=True)
38
+ print(f"Created new repository: {repo_name}")
39
+ except Exception as e:
40
+ print(f"Repository already exists or error occurred: {e}")
41
+
42
+ api.upload_folder(
43
+ folder_path="merged_model",
44
+ repo_id=repo_name,
45
+ repo_type="model",
46
+ )
47
+ print("Model pushed successfully!")
48
+
49
+ def generate_text(input_text):
50
+ global merged_model, tokenizer
51
+
52
+ if merged_model is None or tokenizer is None:
53
+ return "Model not loaded. Please run the merge process first."
54
+
55
+ input_ids = tokenizer.encode(input_text, return_tensors="pt")
56
+
57
+ with torch.no_grad():
58
+ output = merged_model.generate(input_ids, max_length=200, num_return_sequences=1)
59
+
60
+ generated_text = tokenizer.decode(output[0], skip_special_tokens=True)
61
+ return generated_text
62
+
63
+ def run_merge_and_push():
64
+ global merged_model, tokenizer
65
+
66
+ # Merge models
67
+ merged_model, tokenizer = merge_models()
68
+
69
+ # Push to Hugging Face Hub
70
+ hub_repo_name = "your-username/merged-llama3-8b-instruct"
71
+ push_to_hub(hub_repo_name)
72
+
73
+ return "Model merged and pushed to Hugging Face Hub successfully!"
74
+
75
+ # Gradio interface
76
+ iface = gr.Interface(
77
+ fn=generate_text,
78
+ inputs=gr.Textbox(lines=5, label="Input Text"),
79
+ outputs=gr.Textbox(lines=10, label="Generated Text"),
80
+ title="Merged Llama 3 8B Instruct Model",
81
+ description="Enter text to generate a response from the merged model.",
82
+ )
83
+
84
+ merge_and_push_button = gr.Button("Merge Models and Push to Hub")
85
+ merge_and_push_output = gr.Textbox(lines=20, label="Merge and Push Output")
86
+
87
+ merge_and_push_button.click(run_merge_and_push, outputs=merge_and_push_output)
88
+
89
+ # Launch the interface
90
+ if __name__ == "__main__":
91
+ gr.TabbedInterface(
92
+ [iface, gr.Interface(fn=lambda: "", inputs=None, outputs=merge_and_push_output)],
93
+ ["Generate Text", "Merge and Push"]
94
+ ).launch()