asdasdasdasd
Browse files
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 |
-
|
11 |
-
|
12 |
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
|
17 |
-
|
18 |
-
|
19 |
-
finetuned_model = PeftModel.from_pretrained(base_model, peft_config)
|
20 |
|
21 |
-
|
22 |
-
|
23 |
-
param_base.data = (param_base.data + param_finetuned.data) / 2
|
24 |
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
base_tokenizer.save_pretrained(merged_model_name)
|
29 |
|
30 |
-
return
|
31 |
|
32 |
-
|
33 |
-
|
34 |
api = HfApi()
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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()
|