alex-abb commited on
Commit
1a67d0c
1 Parent(s): c4d980d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +36 -2
app.py CHANGED
@@ -5,6 +5,7 @@ from transformers import TFAutoModelForSequenceClassification, AutoTokenizer
5
 
6
  model = TFAutoModelForSequenceClassification.from_pretrained("bert-base-uncased")
7
  tokenizer = AutoTokenizer.from_pretrained("bert-base-uncased")
 
8
 
9
  ds = load_dataset("stanfordnlp/sst2")
10
 
@@ -18,6 +19,39 @@ def encode(examples):
18
  sst2_dataset = sst2_dataset.map(encode, batched=True)
19
  sst2_dataset = sst2_dataset.map(lambda examples: {"labels": examples["label"]}, batched=True)
20
 
21
-
22
-
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
23
 
 
5
 
6
  model = TFAutoModelForSequenceClassification.from_pretrained("bert-base-uncased")
7
  tokenizer = AutoTokenizer.from_pretrained("bert-base-uncased")
8
+ model = BertForSequenceClassification.from_pretrained(model_name, num_labels=2) # 2 classes : positif et négatif
9
 
10
  ds = load_dataset("stanfordnlp/sst2")
11
 
 
19
  sst2_dataset = sst2_dataset.map(encode, batched=True)
20
  sst2_dataset = sst2_dataset.map(lambda examples: {"labels": examples["label"]}, batched=True)
21
 
22
+ training_args = TrainingArguments(
23
+ per_device_train_batch_size=8,
24
+ evaluation_strategy="epoch",
25
+ logging_dir="./logs",
26
+ output_dir="./results",
27
+ num_train_epochs=3,
28
+ )
29
+
30
+
31
+ trainer = Trainer(
32
+ model=model,
33
+ args=training_args,
34
+ train_dataset=encoded_dataset["train"],
35
+ eval_dataset=encoded_dataset["test"],
36
+ )
37
+
38
+ import os
39
+ if not os.path.exists("./fine_tuned_model"):
40
+ trainer.train()
41
+ # Sauvegarder le modèle fine-tuné et le tokenizer
42
+ model.save_pretrained("./fine_tuned_model")
43
+ tokenizer.save_pretrained("./fine_tuned_model")
44
+ else:
45
+ # Charger le modèle fine-tuné
46
+ model = BertForSequenceClassification.from_pretrained("./fine_tuned_model")
47
+ tokenizer = BertTokenizer.from_pretrained("./fine_tuned_model")
48
+
49
+
50
+ sentiment_analysis = pipeline("sentiment-analysis", model=model, tokenizer=tokenizer)
51
+
52
+ def generate_response(message):
53
+ result = sentiment_analysis(message)[0]
54
+ return f"Label: {result['label']}, Score: {result['score']}"
55
+
56
+ gr.ChatInterface(fn=generate_response).launch()
57