Thesebanana commited on
Commit
9fc1cf3
·
1 Parent(s): 386b3f5

add init app without context

Browse files
Files changed (2) hide show
  1. app.py +68 -0
  2. requirements.txt +4 -0
app.py ADDED
@@ -0,0 +1,68 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from transformers import BlenderbotTokenizer, BlenderbotForConditionalGeneration, Trainer, TrainingArguments
2
+ from datasets import load_dataset
3
+ import torch
4
+ import gradio as gr
5
+
6
+ # ✅ Charger le modèle et le tokenizer
7
+ model_name = "facebook/blenderbot-400M-distill"
8
+ tokenizer = BlenderbotTokenizer.from_pretrained(model_name)
9
+ model = BlenderbotForConditionalGeneration.from_pretrained(model_name)
10
+
11
+ # ✅ Charger le dataset "fka/awesome-chatgpt-prompts"
12
+ dataset = load_dataset("fka/awesome-chatgpt-prompts")
13
+
14
+ # ✅ Préparer les données en adaptant les colonnes disponibles
15
+ def preprocess_function(examples):
16
+ inputs = examples["act"]
17
+ targets = examples["prompt"]
18
+
19
+ # Tokenisation avec padding et tronquage
20
+ model_inputs = tokenizer(inputs, max_length=128, truncation=True, padding="max_length", return_tensors="pt")
21
+ labels = tokenizer(targets, max_length=128, truncation=True, padding="max_length", return_tensors="pt")
22
+
23
+ model_inputs["labels"] = labels["input_ids"]
24
+ return model_inputs
25
+
26
+ # Appliquer le prétraitement en retirant les colonnes inutiles
27
+ tokenized_dataset = dataset.map(preprocess_function, batched=True, remove_columns=["act", "prompt"])
28
+
29
+ # ✅ Vérifier si un fine-tuning est nécessaire
30
+ do_training = False # Change à True pour entraîner le modèle
31
+
32
+ if do_training:
33
+ training_args = TrainingArguments(
34
+ output_dir="./results",
35
+ evaluation_strategy="epoch",
36
+ learning_rate=5e-5,
37
+ per_device_train_batch_size=8,
38
+ per_device_eval_batch_size=8,
39
+ num_train_epochs=3,
40
+ weight_decay=0.01,
41
+ save_total_limit=2,
42
+ push_to_hub=False,
43
+ )
44
+
45
+ trainer = Trainer(
46
+ model=model,
47
+ args=training_args,
48
+ train_dataset=tokenized_dataset["train"],
49
+ eval_dataset=tokenized_dataset["test"],
50
+ )
51
+
52
+ trainer.train()
53
+
54
+ # ✅ Pipeline d'inférence pour le chatbot
55
+ def chatbot_response(user_input, history=None): # Accepte l'historique pour éviter l'erreur
56
+ inputs = tokenizer(user_input, return_tensors="pt", max_length=128, truncation=True)
57
+ with torch.no_grad():
58
+ output = model.generate(**inputs, max_length=128, num_beams=5, early_stopping=True)
59
+ return tokenizer.decode(output[0], skip_special_tokens=True)
60
+
61
+ # ✅ Interface Gradio pour dialoguer avec le chatbot
62
+ chatbot = gr.ChatInterface(
63
+ fn=chatbot_response,
64
+ title="Chatbot Service Après-Vente",
65
+ description="Posez vos questions sur le service après-vente et obtenez des réponses instantanées.",
66
+ )
67
+
68
+ chatbot.launch()
requirements.txt ADDED
@@ -0,0 +1,4 @@
 
 
 
 
 
1
+ transformers
2
+ gradio
3
+ datasets
4
+ torch