Fix: Remove Unicode escapes and simplify code for proper Gradio initialization
Browse files
app.py
CHANGED
|
@@ -10,24 +10,20 @@ model = None
|
|
| 10 |
tokenized_dataset = None
|
| 11 |
|
| 12 |
def load_resources():
|
| 13 |
-
"""Load dataset, tokenizer, and model on demand"""
|
| 14 |
global dataset, tokenizer, model, tokenized_dataset
|
| 15 |
|
| 16 |
if dataset is not None:
|
| 17 |
return
|
| 18 |
|
| 19 |
-
# Load the Arabic sentiment dataset
|
| 20 |
try:
|
| 21 |
dataset = load_dataset('arbml/Arabic_Sentiment_Twitter_Corpus')
|
| 22 |
except:
|
| 23 |
dataset = load_dataset('asas-ai/Arabic_Sentiment_Twitter_Corpus')
|
| 24 |
|
| 25 |
-
# Load tokenizer and model
|
| 26 |
tokenizer = AutoTokenizer.from_pretrained('distilbert-base-multilingual-cased')
|
| 27 |
model = AutoModelForSequenceClassification.from_pretrained('distilbert-base-multilingual-cased', num_labels=3)
|
| 28 |
|
| 29 |
def preprocess_function(examples):
|
| 30 |
-
"""Tokenize and preprocess examples"""
|
| 31 |
text_column = 'tweet' if 'tweet' in examples else 'text'
|
| 32 |
encoding = tokenizer(examples[text_column], truncation=True, padding='max_length', max_length=128)
|
| 33 |
|
|
@@ -38,11 +34,9 @@ def preprocess_function(examples):
|
|
| 38 |
return encoding
|
| 39 |
|
| 40 |
def train_model(epochs, batch_size, learning_rate):
|
| 41 |
-
"""Fine-tune DistilBERT on Arabic sentiment dataset (Saudi dialect)"""
|
| 42 |
try:
|
| 43 |
load_resources()
|
| 44 |
|
| 45 |
-
# Preprocess dataset if not already done
|
| 46 |
global tokenized_dataset
|
| 47 |
if tokenized_dataset is None:
|
| 48 |
tokenized_dataset = dataset.map(preprocess_function, batched=True, remove_columns=dataset['train'].column_names)
|
|
@@ -65,53 +59,45 @@ def train_model(epochs, batch_size, learning_rate):
|
|
| 65 |
eval_dataset=tokenized_dataset.get('validation', tokenized_dataset['train']),
|
| 66 |
)
|
| 67 |
|
| 68 |
-
# Start training
|
| 69 |
trainer.train()
|
| 70 |
|
| 71 |
-
return f"
|
| 72 |
except Exception as e:
|
| 73 |
-
return f"
|
| 74 |
|
| 75 |
-
# Create Gradio interface
|
| 76 |
with gr.Blocks(title="DistilBERT Arabic Sentiment Training") as demo:
|
| 77 |
-
gr.Markdown(""
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
-
|
| 84 |
-
-
|
| 85 |
-
- **\u0642\u0627\u0639\u062f\u0629 \u0627\u0644\u0628\u064a\u0627\u0646\u0627\u062a**: arbml/Arabic_Sentiment_Twitter_Corpus (58.8k \u0645\u062b\u0627\u0644)
|
| 86 |
-
- **\u0627\u0644\u0644\u063a\u0629**: \u0627\u0644\u0639\u0631\u0628\u064a\u0629 (\u0627\u0644\u0644\u0647\u062c\u0629 \u0627\u0644\u0633\u0639\u0648\u062f\u064a\u0629 \u0648\u0627\u0644\u062e\u0644\u064a\u062c\u064a\u0629)
|
| 87 |
-
""")
|
| 88 |
|
| 89 |
with gr.Row():
|
| 90 |
with gr.Column():
|
| 91 |
-
gr.Markdown("###
|
| 92 |
-
epochs = gr.Slider(minimum=1, maximum=10, value=3, step=1, label="
|
| 93 |
batch_size = gr.Slider(minimum=8, maximum=64, value=32, step=8, label="Batch Size")
|
| 94 |
learning_rate = gr.Slider(minimum=1e-5, maximum=1e-3, value=2e-5, step=1e-5, label="Learning Rate")
|
| 95 |
|
| 96 |
with gr.Column():
|
| 97 |
-
gr.Markdown("###
|
| 98 |
-
output_text = gr.Textbox(label="
|
| 99 |
|
| 100 |
-
train_button = gr.Button("
|
| 101 |
train_button.click(
|
| 102 |
fn=train_model,
|
| 103 |
inputs=[epochs, batch_size, learning_rate],
|
| 104 |
outputs=output_text
|
| 105 |
)
|
| 106 |
|
| 107 |
-
gr.Markdown(""
|
| 108 |
-
|
| 109 |
-
-
|
| 110 |
-
-
|
| 111 |
-
-
|
| 112 |
-
- **\u0645\u062e\u0631\u062c\u0627\u062a \u0627\u0644\u0646\u0645\u0648\u0630\u062c**: \u0645\u062d\u0641\u0648\u0638 \u0639\u0646\u062f ./results
|
| 113 |
-
- **\u0627\u0644\u0627\u0633\u062a\u062e\u062f\u0627\u0645**: \u0627\u0644\u0646\u0635\u0648\u0635 \u0627\u0644\u0639\u0631\u0628\u064a\u0629 \u0641\u0642\u0637
|
| 114 |
-
""")
|
| 115 |
|
| 116 |
if __name__ == "__main__":
|
| 117 |
demo.launch()
|
|
|
|
| 10 |
tokenized_dataset = None
|
| 11 |
|
| 12 |
def load_resources():
|
|
|
|
| 13 |
global dataset, tokenizer, model, tokenized_dataset
|
| 14 |
|
| 15 |
if dataset is not None:
|
| 16 |
return
|
| 17 |
|
|
|
|
| 18 |
try:
|
| 19 |
dataset = load_dataset('arbml/Arabic_Sentiment_Twitter_Corpus')
|
| 20 |
except:
|
| 21 |
dataset = load_dataset('asas-ai/Arabic_Sentiment_Twitter_Corpus')
|
| 22 |
|
|
|
|
| 23 |
tokenizer = AutoTokenizer.from_pretrained('distilbert-base-multilingual-cased')
|
| 24 |
model = AutoModelForSequenceClassification.from_pretrained('distilbert-base-multilingual-cased', num_labels=3)
|
| 25 |
|
| 26 |
def preprocess_function(examples):
|
|
|
|
| 27 |
text_column = 'tweet' if 'tweet' in examples else 'text'
|
| 28 |
encoding = tokenizer(examples[text_column], truncation=True, padding='max_length', max_length=128)
|
| 29 |
|
|
|
|
| 34 |
return encoding
|
| 35 |
|
| 36 |
def train_model(epochs, batch_size, learning_rate):
|
|
|
|
| 37 |
try:
|
| 38 |
load_resources()
|
| 39 |
|
|
|
|
| 40 |
global tokenized_dataset
|
| 41 |
if tokenized_dataset is None:
|
| 42 |
tokenized_dataset = dataset.map(preprocess_function, batched=True, remove_columns=dataset['train'].column_names)
|
|
|
|
| 59 |
eval_dataset=tokenized_dataset.get('validation', tokenized_dataset['train']),
|
| 60 |
)
|
| 61 |
|
|
|
|
| 62 |
trainer.train()
|
| 63 |
|
| 64 |
+
return f"Training completed successfully! Model saved in ./results"
|
| 65 |
except Exception as e:
|
| 66 |
+
return f"Error during training: {str(e)}"
|
| 67 |
|
|
|
|
| 68 |
with gr.Blocks(title="DistilBERT Arabic Sentiment Training") as demo:
|
| 69 |
+
gr.Markdown("# DistilBERT Arabic Sentiment Training")
|
| 70 |
+
gr.Markdown("Fine-tune DistilBERT on Arabic sentiment analysis (Saudi dialect)")
|
| 71 |
+
|
| 72 |
+
gr.Markdown("### Model Information:")
|
| 73 |
+
gr.Markdown("- **Base Model**: distilbert-base-multilingual-cased (67M parameters)")
|
| 74 |
+
gr.Markdown("- **Task**: Text Classification (Multilingual)")
|
| 75 |
+
gr.Markdown("- **Dataset**: arbml/Arabic_Sentiment_Twitter_Corpus (58.8k examples)")
|
| 76 |
+
gr.Markdown("- **Language**: Arabic (Saudi & Gulf dialects)")
|
|
|
|
|
|
|
|
|
|
| 77 |
|
| 78 |
with gr.Row():
|
| 79 |
with gr.Column():
|
| 80 |
+
gr.Markdown("### Training Settings")
|
| 81 |
+
epochs = gr.Slider(minimum=1, maximum=10, value=3, step=1, label="Epochs")
|
| 82 |
batch_size = gr.Slider(minimum=8, maximum=64, value=32, step=8, label="Batch Size")
|
| 83 |
learning_rate = gr.Slider(minimum=1e-5, maximum=1e-3, value=2e-5, step=1e-5, label="Learning Rate")
|
| 84 |
|
| 85 |
with gr.Column():
|
| 86 |
+
gr.Markdown("### Training Status")
|
| 87 |
+
output_text = gr.Textbox(label="Output", lines=10, interactive=False)
|
| 88 |
|
| 89 |
+
train_button = gr.Button("Start Training", variant="primary")
|
| 90 |
train_button.click(
|
| 91 |
fn=train_model,
|
| 92 |
inputs=[epochs, batch_size, learning_rate],
|
| 93 |
outputs=output_text
|
| 94 |
)
|
| 95 |
|
| 96 |
+
gr.Markdown("### Training Details:")
|
| 97 |
+
gr.Markdown("- **Hardware**: Free GPU (Hugging Face Spaces)")
|
| 98 |
+
gr.Markdown("- **Expected Time**: 5-10 minutes (GPU) or 15-20 minutes (CPU)")
|
| 99 |
+
gr.Markdown("- **Output Directory**: ./results")
|
| 100 |
+
gr.Markdown("- **Usage**: Arabic text only")
|
|
|
|
|
|
|
|
|
|
| 101 |
|
| 102 |
if __name__ == "__main__":
|
| 103 |
demo.launch()
|