ashioyajotham commited on
Commit
fde41fa
1 Parent(s): 7139589

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +13 -171
app.py CHANGED
@@ -1,177 +1,19 @@
1
- from datasets import load_dataset
2
 
3
- # Specify the name of the dataset
4
- dataset_name = "yahma/alpaca-cleaned"
 
 
5
 
6
- # Load the dataset from the specified name and select the "train" split
7
- dataset = load_dataset(dataset_name, split="train")
8
 
9
- # We will be loading the Falcon 7B model, applying 4bit quantization to it, and then adding LoRA adapters to the model.
10
- import torch
11
-
12
- from transformers import FalconForCausalLM, AutoTokenizer, BitsAndBytesConfig
13
- from transformers import AutoModelForCausalLM, AutoTokenizer, BitsAndBytesConfig
14
-
15
- # Defining the name of the Falcon model
16
- model_name = "ybelkada/falcon-7b-sharded-bf16"
17
-
18
- # Configuring the BitsAndBytes quantization
19
- bnb_config = BitsAndBytesConfig(
20
- load_in_4bit=True,
21
- bnb_4bit_quant_type="nf4",
22
- bnb_4bit_compute_dtype=torch.float16,
23
- )
24
-
25
- # Loading the Falcon model with quantization configuration
26
- model = FalconForCausalLM.from_pretrained(
27
- model_name,
28
- quantization_config=bnb_config,
29
- trust_remote_code=True
30
- )
31
-
32
- # Disabling cache usage in the model configuration
33
- model.config.use_cache = False
34
-
35
- # Load the tokenizer for the Falcon 7B model with remote code trust
36
- tokenizer = AutoTokenizer.from_pretrained(model_name, trust_remote_code=True)
37
-
38
- # Set the padding token to be the same as the end-of-sequence token
39
- tokenizer.pad_token = tokenizer.eos_token
40
-
41
- # Import the necessary module for LoRA configuration
42
- from peft import LoraConfig
43
-
44
- # Define the parameters for LoRA configuration
45
- lora_alpha = 16
46
- lora_dropout = 0.1
47
- lora_r = 64
48
-
49
- # Create the LoRA configuration object
50
- peft_config = LoraConfig(
51
- lora_alpha=lora_alpha,
52
- lora_dropout=lora_dropout,
53
- r=lora_r,
54
- bias="none",
55
- task_type="CAUSAL_LM",
56
- target_modules=[
57
- "query_key_value",
58
- "dense",
59
- "dense_h_to_4h",
60
- "dense_4h_to_h",
61
- ]
62
- )
63
-
64
- from transformers import TrainingArguments
65
- # Define the directory to save training results
66
- output_dir = "./results"
67
-
68
- # Set the batch size per device during training
69
- per_device_train_batch_size = 4
70
-
71
- # Number of steps to accumulate gradients before updating the model
72
- gradient_accumulation_steps = 4
73
-
74
- # Choose the optimizer type (e.g., "paged_adamw_32bit")
75
- optim = "paged_adamw_32bit"
76
-
77
- # Interval to save model checkpoints (every 10 steps)
78
- save_steps = 10
79
-
80
- # Interval to log training metrics (every 10 steps)
81
- logging_steps = 10
82
-
83
- # Learning rate for optimization
84
- learning_rate = 2e-4
85
-
86
- # Maximum gradient norm for gradient clipping
87
- max_grad_norm = 0.3
88
-
89
- # Maximum number of training steps
90
- max_steps = 50
91
-
92
- # Warmup ratio for learning rate scheduling
93
- warmup_ratio = 0.03
94
-
95
- # Type of learning rate scheduler (e.g., "constant")
96
- lr_scheduler_type = "constant"
97
-
98
- # Create a TrainingArguments object to configure the training process
99
- training_arguments = TrainingArguments(
100
- output_dir=output_dir,
101
- per_device_train_batch_size=per_device_train_batch_size,
102
- gradient_accumulation_steps=gradient_accumulation_steps,
103
- optim=optim,
104
- save_steps=save_steps,
105
- logging_steps=logging_steps,
106
- learning_rate=learning_rate,
107
- fp16=True, # Use mixed precision training (16-bit)
108
- max_grad_norm=max_grad_norm,
109
- max_steps=max_steps,
110
- warmup_ratio=warmup_ratio,
111
- group_by_length=True,
112
- lr_scheduler_type=lr_scheduler_type,
113
- )
114
-
115
-
116
- dataset = dataset.map(lambda x: {"text": x["input"]+x["output"]})
117
-
118
- # Import the SFTTrainer from the TRL library
119
- from trl import SFTTrainer
120
-
121
- # Set the maximum sequence length
122
- max_seq_length = 512
123
-
124
- # Create a trainer instance using SFTTrainer
125
- trainer = SFTTrainer(
126
- model=model,
127
- train_dataset=dataset,
128
- peft_config=peft_config,
129
- dataset_text_field="text",
130
- max_seq_length=max_seq_length,
131
- tokenizer=tokenizer,
132
- args=training_arguments,
133
  )
134
 
 
135
 
136
- # Iterate through the named modules of the trainer's model
137
- for name, module in trainer.model.named_modules():
138
-
139
- # Check if the name contains "norm"
140
- if "norm" in name:
141
- # Convert the module to use torch.float32 data type
142
- module = module.to(torch.float32)
143
-
144
- trainer.train()
145
-
146
-
147
- prompt = "Generate a python script to add prime numbers between one and ten"
148
-
149
- inputs = tokenizer.encode(prompt, return_tensors='pt')
150
-
151
- outputs = model.generate(inputs, max_length=100, temperature = .7, do_sample=True)
152
-
153
- completion = tokenizer.decode(outputs[0])
154
-
155
- print(completion)
156
-
157
-
158
-
159
-
160
- from transformers import AutoModelForCausalLM, AutoTokenizer
161
-
162
- checkpoint_name= model
163
- model = AutoModelForCausalLM.from_pretrained(checkpoint_name)
164
- tokenizer = AutoTokenizer.from_pretrained(checkpoint_name)
165
-
166
- prompt = "Create a gradio application that help to convert temperature in celcius into temperature in Fahrenheit"
167
- inputs = tokenizer(f"Question: {prompt}\n\nAnswer: ", return_tensors="pt")
168
-
169
- outputs = model.generate(
170
- inputs["input_ids"],
171
- temperature=0.2,
172
- top_p=0.95,
173
- max_new_tokens=200
174
- )
175
-
176
- input_len=len(inputs["input_ids"])
177
- print(tokenizer.decode(outputs[0][input_len:]))
 
1
+ fine_tuned_model = "ashioyajotham/results"
2
 
3
+ def generate_code(input_text):
4
+ # Use your fine-tuned model to generate code here
5
+ generated_code = fine_tuned_model.generate(input_text)
6
+ return generated_code
7
 
8
+ import gradio as gr
 
9
 
10
+ iface = gr.Interface(
11
+ fn=generate_code,
12
+ inputs="text",
13
+ outputs="text",
14
+ title="Code Generation App",
15
+ description="Generate code from text input."
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
16
  )
17
 
18
+ iface.launch()
19