sufya12 commited on
Commit
2c693fe
·
verified ·
1 Parent(s): 653cf6d

Rename test_local.py to fine_tune.py

Browse files
Files changed (2) hide show
  1. fine_tune.py +91 -0
  2. test_local.py +0 -20
fine_tune.py ADDED
@@ -0,0 +1,91 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Fine-tuning on Hugging Face Notebook
2
+ # This runs in a HF notebook with GPU
3
+
4
+ # Install requirements
5
+ !pip install transformers datasets pandas scikit-learn accelerate
6
+
7
+ import pandas as pd
8
+ from transformers import (
9
+ AutoTokenizer,
10
+ AutoModelForSeq2SeqLM,
11
+ DataCollatorForSeq2Seq,
12
+ Seq2SeqTrainingArguments,
13
+ Seq2SeqTrainer
14
+ )
15
+ from datasets import Dataset
16
+ from sklearn.model_selection import train_test_split
17
+
18
+ # Load your dataset
19
+ df = pd.read_csv('Data_1.csv')
20
+ print(f"Dataset loaded: {len(df)} rows")
21
+
22
+ # Data preprocessing
23
+ df = df.dropna()
24
+ train_df, val_df = train_test_split(df, test_size=0.2, random_state=42)
25
+
26
+ print(f"Training samples: {len(train_df)}")
27
+ print(f"Validation samples: {len(val_df)}")
28
+
29
+ # Load model and tokenizer
30
+ model_name = "Helsinki-NLP/opus-mt-en-fr"
31
+ tokenizer = AutoTokenizer.from_pretrained(model_name)
32
+ model = AutoModelForSeq2SeqLM.from_pretrained(model_name)
33
+
34
+ # Preprocessing function
35
+ def preprocess_function(examples):
36
+ inputs = []
37
+ for i in range(len(examples['year'])):
38
+ input_text = f"Generate French title: Year: {examples['year'][i]} City: {examples['city'][i]} Event: {examples['original_title'][i]}"
39
+ inputs.append(input_text)
40
+
41
+ targets = examples['target_title']
42
+
43
+ model_inputs = tokenizer(inputs, max_length=128, truncation=True, padding=True)
44
+ with tokenizer.as_target_tokenizer():
45
+ labels = tokenizer(targets, max_length=128, truncation=True, padding=True)
46
+
47
+ model_inputs["labels"] = labels["input_ids"]
48
+ return model_inputs
49
+
50
+ # Prepare datasets
51
+ train_dataset = Dataset.from_pandas(train_df)
52
+ val_dataset = Dataset.from_pandas(val_df)
53
+
54
+ tokenized_train = train_dataset.map(preprocess_function, batched=True)
55
+ tokenized_val = val_dataset.map(preprocess_function, batched=True)
56
+
57
+ # Training arguments
58
+ training_args = Seq2SeqTrainingArguments(
59
+ output_dir="./french-title-model",
60
+ evaluation_strategy="epoch",
61
+ learning_rate=5e-5,
62
+ per_device_train_batch_size=8,
63
+ per_device_eval_batch_size=8,
64
+ weight_decay=0.01,
65
+ save_total_limit=3,
66
+ num_train_epochs=10,
67
+ predict_with_generate=True,
68
+ push_to_hub=True,
69
+ hub_model_id="sufya12/french-title-model", # Change to your username
70
+ )
71
+
72
+ # Data collator
73
+ data_collator = DataCollatorForSeq2Seq(tokenizer, model=model)
74
+
75
+ # Trainer
76
+ trainer = Seq2SeqTrainer(
77
+ model=model,
78
+ args=training_args,
79
+ train_dataset=tokenized_train,
80
+ eval_dataset=tokenized_val,
81
+ tokenizer=tokenizer,
82
+ data_collator=data_collator,
83
+ )
84
+
85
+ # Start training
86
+ print("Starting training...")
87
+ trainer.train()
88
+
89
+ # Save and push to Hub
90
+ trainer.push_to_hub()
91
+ print("Training completed and model pushed to Hugging Face Hub!")
test_local.py DELETED
@@ -1,20 +0,0 @@
1
- import requests
2
- import json
3
-
4
- def test_local_api():
5
- url = "http://localhost:7860/api/predict/"
6
-
7
- test_data = {
8
- "data": ["2025", "Paris", "Jazz Festival in the City Park", 0.8, 50]
9
- }
10
-
11
- try:
12
- response = requests.post(url, json=test_data)
13
- print("Status Code:", response.status_code)
14
- print("Response:", response.json())
15
- except Exception as e:
16
- print("Error:", e)
17
- print("Make sure your app is running on port 7860!")
18
-
19
- if __name__ == "__main__":
20
- test_local_api()