Views
No views yet
1
2!pip install "unsloth[colab-new] @ git+https://github.com/unslothai/unsloth.git"
3!pip install --no-deps "xformers<0.0.27" "trl<0.9.0" peft accelerate bitsandbytes
4!pip install gradio
5
6import gradio as gr
7from unsloth import FastLanguageModel
8import torch
9
10# Load base model and tokenizer
11base_model, base_tokenizer = FastLanguageModel.from_pretrained(
12 model_name="unsloth/Meta-Llama-3.1-8B",
13 max_seq_length=2048,
14 dtype=None,
15 load_in_4bit=True,
16)
17FastLanguageModel.for_inference(base_model) # Enable native 2x faster inference
18
19# Load LoRA model and tokenizer
20lora_model, lora_tokenizer = FastLanguageModel.from_pretrained(
21 model_name="Omartificial-Intelligence-Space/Arabic-llama3.1-lora-FT", # Replace with your LoRA model path/name
22 max_seq_length=2048,
23 dtype=None,
24 load_in_4bit=True,
25)
26FastLanguageModel.for_inference(lora_model) # Enable native 2x faster inference
27
28simplified_prompt = """Input: {}
29Response: {}"""
30
31def extract_response(text):
32 """ Extracts the Response part from the generated text """
33 response_marker = "Response:"
34 if response_marker in text:
35 return text.split(response_marker, 1)[1].strip()
36 return text.strip()
37
38def generate_responses(input_text):
39 prompt = simplified_prompt.format(input_text, "")
40
41 # Tokenize input for base model
42 base_inputs = base_tokenizer([prompt], return_tensors="pt").to("cuda")
43 # Generate output using base model
44 base_outputs = base_model.generate(**base_inputs, max_new_tokens=128, use_cache=True)
45 # Decode base model output
46 base_decoded_outputs = base_tokenizer.batch_decode(base_outputs, skip_special_tokens=True)[0]
47 base_response = extract_response(base_decoded_outputs)
48
49 # Tokenize input for LoRA model
50 lora_inputs = lora_tokenizer([prompt], return_tensors="pt").to("cuda")
51 # Generate output using LoRA model
52 lora_outputs = lora_model.generate(**lora_inputs, max_new_tokens=128, use_cache=True)
53 # Decode LoRA model output
54 lora_decoded_outputs = lora_tokenizer.batch_decode(lora_outputs, skip_special_tokens=True)[0]
55 lora_response = extract_response(lora_decoded_outputs)
56
57 return base_response, lora_response
58
59# Custom CSS for the interface
60css = """
61h1 {
62 color: #1E90FF;
63 font-family: 'Arial', sans-serif;
64 text-align: center;
65 margin-bottom: 20px;
66}
67
68.description {
69 color: #4682B4;
70 font-family: 'Arial', sans-serif;
71 text-align: center;
72 font-size: 18px;
73 margin-bottom: 20px;
74}
75
76.gradio-container {
77 background-color: #F0F0F0;
78 border-radius: 10px;
79 padding: 20px;
80}
81
82.gr-button {
83 background-color: #FFA500;
84 color: white;
85 border: none;
86 padding: 10px 20px;
87 text-align: center;
88 display: inline-block;
89 font-size: 16px;
90 margin: 4px 2px;
91 cursor: pointer;
92}
93
94.gr-button:hover {
95 background-color: #FF8C00;
96}
97
98.gr-textbox {
99 border: 2px solid #1E90FF;
100 border-radius: 5px;
101 padding: 10px;
102}
103"""
104
105# JavaScript for additional functionality (if needed)
106js = """
107function createGradioAnimation() {
108 var container = document.createElement('div');
109 container.id = 'gradio-animation';
110 container.style.fontSize = '2em';
111 container.style.fontWeight = 'bold';
112 container.style.textAlign = 'center';
113 container.style.marginBottom = '20px';
114
115 var text = 'Omartificial Intelligence Space';
116 for (var i = 0; i < text.length; i++) {
117 (function(i){
118 setTimeout(function(){
119 var letter = document.createElement('span');
120 letter.style.opacity = '0';
121 letter.style.transition = 'opacity 0.5s';
122 letter.innerText = text[i];
123
124 container.appendChild(letter);
125
126 setTimeout(function() {
127 letter.style.opacity = '1';
128 }, 50);
129 }, i * 250);
130 })(i);
131 }
132
133 var gradioContainer = document.querySelector('.gradio-container');
134 gradioContainer.insertBefore(container, gradioContainer.firstChild);
135
136 return 'Animation created';
137}
138"""
139
140with gr.Blocks(css=css, js=js) as demo:
141 gr.Markdown("<h1>Arabic llaMa3.1 Lora Model (Version 1)</h1>")
142 gr.Markdown("<p class='description'>This model is the Arabic version of Llama3.1, utilized to answer in Arabic for different types of prompts.</p>")
143
144 with gr.Row():
145 input_text = gr.Textbox(lines=5, placeholder="Enter input text here...", elem_classes="gr-textbox")
146 base_output = gr.Textbox(label="Base Model Output", elem_classes="gr-textbox")
147 lora_output = gr.Textbox(label="LoRA Model Output", elem_classes="gr-textbox")
148
149 generate_button = gr.Button("Generate Responses", elem_classes="gr-button")
150
151 generate_button.click(generate_responses, inputs=input_text, outputs=[base_output, lora_output])
152
153demo.launch(debug = True)
1541## Citation
2
3If you use the Arabic llama3.1 Lora Model, please cite it as follows:
4
5```bibtex
6@model{nacar2024,
7 author = {Omer Nacar},
8 title = {Arabic llama3.1 Lora Model},
9 year = 2024,
10 url = {https://huggingface.co/Omartificial-Intelligence-Space/Arabic-llama3.1-Chat-lora},
11 version = {1.0.0},
12}