File size: 3,222 Bytes
58a16f2
a261ff8
 
453187f
58a16f2
 
 
 
 
 
6cc4234
a261ff8
58a16f2
 
a261ff8
58a16f2
 
 
a261ff8
58a16f2
 
a261ff8
58a16f2
 
a261ff8
58a16f2
 
 
a261ff8
58a16f2
6cc4234
58a16f2
 
a261ff8
58a16f2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
122c44e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
58a16f2
 
 
122c44e
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
import json
import torch
from transformers import AutoModelForCausalLM, AutoTokenizer
from peft import PeftModel, PeftConfig
from huggingface_hub import HfApi, Repository
import gradio as gr

# Global variables to store the model and tokenizer
merged_model = None
tokenizer = None

def merge_models():
    print("Loading base model...")
    base_model = AutoModelForCausalLM.from_pretrained("meta-llama/Meta-Llama-3-8B-Instruct")
    
    print("Loading fine-tuned model...")
    peft_config = PeftConfig.from_pretrained("NoaiGPT/autotrain-14mrs-fc44l")
    fine_tuned_model = PeftModel.from_pretrained(base_model, peft_config)
    
    print("Merging models...")
    merged_model = fine_tuned_model.merge_and_unload()
    
    print("Saving merged model...")
    merged_model.save_pretrained("merged_model")
    
    print("Saving tokenizer...")
    tokenizer = AutoTokenizer.from_pretrained("meta-llama/Meta-Llama-3-8B-Instruct")
    tokenizer.save_pretrained("merged_model")
    
    return merged_model, tokenizer

def push_to_hub(repo_name):
    print(f"Pushing merged model to Hugging Face Hub: {repo_name}")
    api = HfApi()
    
    try:
        api.create_repo(repo_name, private=True)
        print(f"Created new repository: {repo_name}")
    except Exception as e:
        print(f"Repository already exists or error occurred: {e}")
    
    api.upload_folder(
        folder_path="merged_model",
        repo_id=repo_name,
        repo_type="model",
    )
    print("Model pushed successfully!")

def generate_text(input_text):
    global merged_model, tokenizer
    
    if merged_model is None or tokenizer is None:
        return "Model not loaded. Please run the merge process first."
    
    input_ids = tokenizer.encode(input_text, return_tensors="pt")
    
    with torch.no_grad():
        output = merged_model.generate(input_ids, max_length=200, num_return_sequences=1)
    
    generated_text = tokenizer.decode(output[0], skip_special_tokens=True)
    return generated_text

def run_merge_and_push():
    global merged_model, tokenizer
    
    # Merge models
    merged_model, tokenizer = merge_models()
    
    # Push to Hugging Face Hub
    hub_repo_name = "your-username/merged-llama3-8b-instruct"
    push_to_hub(hub_repo_name)
    
    return "Model merged and pushed to Hugging Face Hub successfully!"

# Gradio interface
with gr.Blocks() as demo:
    with gr.TabbedInterface(["Generate Text", "Merge and Push"]) as tabs:
        with tabs[0]:
            iface = gr.Interface(
                fn=generate_text,
                inputs=gr.Textbox(lines=5, label="Input Text"),
                outputs=gr.Textbox(lines=10, label="Generated Text"),
                title="Merged Llama 3 8B Instruct Model",
                description="Enter text to generate a response from the merged model.",
            )
            iface.launch()
        
        with tabs[1]:
            merge_and_push_button = gr.Button("Merge Models and Push to Hub")
            merge_and_push_output = gr.Textbox(lines=20, label="Merge and Push Output")
            merge_and_push_button.click(run_merge_and_push, outputs=merge_and_push_output)

# Launch the interface
if __name__ == "__main__":
    demo.launch()