Mahmoud-Dev commited on
Commit
8741847
·
verified ·
1 Parent(s): 0ab941f

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +95 -0
app.py ADDED
@@ -0,0 +1,95 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import torch
3
+ from datasets import load_dataset
4
+ from transformers import DistilBertTokenizer, DistilBertForSequenceClassification, Trainer, TrainingArguments
5
+ import numpy as np
6
+
7
+ # Load the sentiment dataset
8
+ dataset = load_dataset('k1tub/sentiment_dataset')
9
+ print(f"Dataset loaded with {len(dataset['train'])} training examples")
10
+
11
+ # Load tokenizer and model
12
+ tokenizer = DistilBertTokenizer.from_pretrained('distilbert-base-uncased')
13
+ model = DistilBertForSequenceClassification.from_pretrained('distilbert-base-uncased', num_labels=3)
14
+
15
+ def preprocess_function(examples):
16
+ # Tokenize the text
17
+ encoding = tokenizer(examples['text'], truncation=True, padding='max_length', max_length=128)
18
+ # Map label to indices
19
+ encoding['labels'] = examples['label']
20
+ return encoding
21
+
22
+ # Preprocess the dataset
23
+ tokenized_dataset = dataset.map(preprocess_function, batched=True, remove_columns=['text'])
24
+
25
+ def train_model(epochs, batch_size, learning_rate):
26
+ """Fine-tune DistilBERT on the sentiment dataset"""
27
+ try:
28
+ training_args = TrainingArguments(
29
+ output_dir='./results',
30
+ num_train_epochs=int(epochs),
31
+ per_device_train_batch_size=int(batch_size),
32
+ per_device_eval_batch_size=int(batch_size),
33
+ learning_rate=float(learning_rate),
34
+ weight_decay=0.01,
35
+ save_strategy='epoch',
36
+ logging_steps=50,
37
+ )
38
+
39
+ trainer = Trainer(
40
+ model=model,
41
+ args=training_args,
42
+ train_dataset=tokenized_dataset['train'],
43
+ eval_dataset=tokenized_dataset['validation'] if 'validation' in tokenized_dataset else tokenized_dataset['train'],
44
+ )
45
+
46
+ # Start training
47
+ trainer.train()
48
+
49
+ return "\u270d✅ Training completed successfully!\n" + \
50
+ f"Model saved to ./results\nFinal learning rate: {learning_rate}\nEpochs: {epochs}\nBatch size: {batch_size}"
51
+ except Exception as e:
52
+ return f"❌ Error during training: {str(e)}"
53
+
54
+ # Create Gradio interface
55
+ with gr.Blocks(title="DistilBERT Sentiment Training") as demo:
56
+ gr.Markdown("""
57
+ # 🚀 DistilBERT Sentiment Analysis Training
58
+
59
+ Fine-tune **DistilBERT** model on the **k1tub/sentiment_dataset** (290k examples)
60
+
61
+ ### Model Info:
62
+ - **Base Model**: distilbert-base-uncased (67M parameters)
63
+ - **Task**: Text Classification (Sentiment Analysis)
64
+ - **Dataset**: k1tub/sentiment_dataset
65
+ - **Framework**: Hugging Face Transformers
66
+ """)
67
+
68
+ with gr.Row():
69
+ with gr.Column():
70
+ gr.Markdown("### Training Configuration")
71
+ epochs = gr.Slider(minimum=1, maximum=10, value=3, step=1, label="Number of Epochs")
72
+ batch_size = gr.Slider(minimum=8, maximum=64, value=32, step=8, label="Batch Size")
73
+ learning_rate = gr.Slider(minimum=1e-5, maximum=1e-3, value=2e-5, step=1e-5, label="Learning Rate")
74
+
75
+ with gr.Column():
76
+ gr.Markdown("### Training Status")
77
+ output_text = gr.Textbox(label="Output", lines=10, interactive=False)
78
+
79
+ train_button = gr.Button("🔥 Start Training", variant="primary", scale=2)
80
+ train_button.click(
81
+ fn=train_model,
82
+ inputs=[epochs, batch_size, learning_rate],
83
+ outputs=output_text
84
+ )
85
+
86
+ gr.Markdown("""
87
+ ### Training Details:
88
+ - **Free Hardware**: CPU Basic on Hugging Face Spaces
89
+ - **Training Time**: Depends on dataset size and hardware
90
+ - **Model Output**: Saved to ./results folder
91
+ - **Inference**: Can be deployed as a separate Space
92
+ """)
93
+
94
+ if __name__ == "__main__":
95
+ demo.launch()