lu2000luk commited on
Commit
809f9c2
1 Parent(s): c0916c9

Upload 8 files

Browse files
Files changed (4) hide show
  1. Ruttoni_AI/pytorch_model.txt +1 -0
  2. aka.py +22 -0
  3. csv_preprocess.py +10 -0
  4. train.py +84 -0
Ruttoni_AI/pytorch_model.txt ADDED
@@ -0,0 +1 @@
 
 
1
+ The model is avalable at: https://huggingface.co/lu2000luk/RuttoniAI/resolve/main/pytorch_model.bin
aka.py ADDED
@@ -0,0 +1,22 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from transformers import T5Tokenizer, T5ForConditionalGeneration
2
+ import torch
3
+ import colorama
4
+ from colorama import Fore, Back, Style
5
+
6
+ colorama.init()
7
+
8
+ # Load the trained model for inference
9
+ model = T5ForConditionalGeneration.from_pretrained("./Ruttoni_AI")
10
+ tokenizer = T5Tokenizer.from_pretrained("google/flan-t5-base")
11
+
12
+ # Generate a summary using the trained model
13
+ def generate_summary(input_text):
14
+ input_ids = tokenizer.encode(input_text, return_tensors='pt')
15
+ outputs = model.generate(input_ids)
16
+ summary = tokenizer.decode(outputs[0], skip_special_tokens=True)
17
+ return summary
18
+
19
+ # Example usage
20
+ input_text = "Who is pesce beddo?"
21
+ summary = generate_summary(input_text)
22
+ print(Back.GREEN + "Answer: " + summary)
csv_preprocess.py ADDED
@@ -0,0 +1,10 @@
 
 
 
 
 
 
 
 
 
 
 
1
+ import pandas as pd
2
+
3
+ # Read the CSV file
4
+ data = pd.read_csv('ruttoniaitrain1.csv')
5
+
6
+ # Rename columns
7
+ data = data.rename(columns={'Quest': 'text', 'Answer': 'target'})
8
+
9
+ # Save the preprocessed data
10
+ data.to_csv('ruttoniaitrain_preprocessed.csv', index=False)
train.py ADDED
@@ -0,0 +1,84 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from transformers import T5Tokenizer, T5ForConditionalGeneration
2
+ from transformers import DataCollatorWithPadding
3
+ from datasets import Dataset
4
+ import pandas as pd
5
+ import torch
6
+ from sklearn.model_selection import train_test_split
7
+
8
+ # Load the CSV file
9
+ df = pd.read_csv("ruttoniaitrain1.csv")
10
+
11
+ # Rename the columns
12
+ df = df.rename(columns={"Quest": "text", "Answer": "target"})
13
+
14
+ # Convert the DataFrame to a Hugging Face Dataset
15
+ train_df, val_df = train_test_split(df, test_size=0.2, random_state=42)
16
+ train_dataset = Dataset.from_pandas(train_df)
17
+ val_dataset = Dataset.from_pandas(val_df)
18
+
19
+ print("CSV Processed and loaded!")
20
+ # Initialize the tokenizer and model
21
+ tokenizer = T5Tokenizer.from_pretrained("google/flan-t5-base")
22
+ model = T5ForConditionalGeneration.from_pretrained("google/flan-t5-base")
23
+
24
+ print("Model Loaded!")
25
+ # Tokenize and format the data
26
+ def preprocess_function(examples):
27
+ inputs = tokenizer(
28
+ examples['text'],
29
+ truncation=True,
30
+ padding='longest',
31
+ max_length=512
32
+ )
33
+ targets = tokenizer(
34
+ examples['target'],
35
+ truncation=True,
36
+ padding='longest',
37
+ max_length=32
38
+ )
39
+
40
+ examples['input_ids'] = inputs['input_ids']
41
+ examples['attention_mask'] = inputs['attention_mask']
42
+ examples['labels'] = targets['input_ids']
43
+
44
+ return examples
45
+
46
+ train_dataset = train_dataset.map(preprocess_function, batched=True)
47
+
48
+ training_args = {
49
+ 'output_dir': './Ruttoni_AI',
50
+ 'num_train_epochs': 3,
51
+ 'per_device_train_batch_size': 4,
52
+ 'save_steps': 500,
53
+ 'save_total_limit': 2,
54
+ 'logging_steps': 100,
55
+ 'evaluation_strategy': 'steps',
56
+ 'eval_steps': 500,
57
+ 'logging_dir': './logs',
58
+ 'overwrite_output_dir': True,
59
+ 'warmup_steps': 500,
60
+ 'learning_rate': 1e-4,
61
+ 'report_to': 'none'
62
+ }
63
+
64
+ print("Arguments and functions initialized!")
65
+
66
+ data_collator = DataCollatorWithPadding(tokenizer=tokenizer)
67
+
68
+ from transformers import Trainer, TrainingArguments
69
+
70
+ trainer = Trainer(
71
+ model=model,
72
+ args=TrainingArguments(**training_args),
73
+ data_collator=data_collator,
74
+ train_dataset=train_dataset,
75
+ eval_dataset=val_dataset,
76
+ )
77
+
78
+ print("Training...")
79
+
80
+ trainer.train()
81
+
82
+ print("Saving...")
83
+
84
+ trainer.save_model("./Ruttoni_AI")