mehdiIA commited on
Commit
b24aef9
1 Parent(s): 8bf6a83

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +112 -0
app.py ADDED
@@ -0,0 +1,112 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from transformers import pipeline, BertTokenizer, BertForSequenceClassification
3
+
4
+ # Charger le modèle zéro-shot de Hugging Face
5
+ zero_shot_classifier = pipeline("zero-shot-classification", model="facebook/bart-large-mnli")
6
+
7
+ # Charger le modèle few-shot à partir du modèle sauvegardé
8
+ tokenizer = BertTokenizer.from_pretrained('./animal_offense_model')
9
+ model = BertForSequenceClassification.from_pretrained('./animal_offense_model')
10
+ few_shot_classifier = pipeline("text-classification", model=model, tokenizer=tokenizer)
11
+
12
+ # Fonction pour classifier le texte avec le modèle zéro-shot
13
+ def classify_with_zero_shot(input_text):
14
+ candidate_labels = [
15
+ "non-offensive, it's cute! 😇",
16
+ "very slightly offensive, but not a big deal! 😅",
17
+ "slightly offensive, just a little! 🤏",
18
+ "a bit offensive, ouch! 🤭",
19
+ "moderately offensive, getting there! 😬",
20
+ "fairly offensive, watch out! 🚨",
21
+ "offensive, that's a no-no! 🚫",
22
+ "very offensive, you really shouldn't say that! 😳",
23
+ "extremely offensive, seriously? 😡",
24
+ "totally unacceptable and offensive, you are crazy! 🤯"
25
+ ]
26
+
27
+ result = zero_shot_classifier(input_text, candidate_labels)
28
+ labels_scores = dict(zip(result["labels"], result["scores"]))
29
+ return labels_scores
30
+
31
+ # Fonction pour classifier le texte avec votre propre modèle few-shot
32
+ def classify_with_few_shot(input_text):
33
+ result = few_shot_classifier(input_text)
34
+ label = result[0]["label"]
35
+ score = result[0]["score"]
36
+
37
+ # Ajuster la logique pour garantir l'interprétation correcte des labels
38
+ if label == "LABEL_0":
39
+ return {"non-offensive, it's cute! 😇": score, "offensive": 1 - score}
40
+ elif label == "LABEL_1":
41
+ return {"offensive": score, "non-offensive, it's cute! 😇": 1 - score}
42
+ else:
43
+ return {"unknown": 1.0} # Pour des fins de débogage si un label inattendu est trouvé
44
+
45
+ # Fonction principale pour sélectionner le modèle
46
+ def classify_text(input_text, model_choice):
47
+ if model_choice == "Zero-Shot Model":
48
+ return classify_with_zero_shot(input_text)
49
+ elif model_choice == "Few-Shot Model":
50
+ return classify_with_few_shot(input_text)
51
+ else:
52
+ return "Please select a valid model."
53
+
54
+ # Liste de phrases exemples (chaque sous-liste est [texte])
55
+ example_phrases = [
56
+ ["Your dog is the cutest ever!"],
57
+ ["I think your cat needs to lose some weight."],
58
+ ["Why would anyone like such an ugly fish?"],
59
+ ["Oh no, saying that about a rabbit is not okay at all!"],
60
+ ["That’s a bit harsh on a parrot."],
61
+ ["You should be more gentle when talking about horses."],
62
+ ["This kitten is just too adorable!"],
63
+ ["Wow, calling a bird annoying is really offensive!"],
64
+ ["That’s a lovely compliment for a hamster!"],
65
+ ["Saying that a dog smells bad is quite rude!"]
66
+ ]
67
+
68
+ # Créer une interface Gradio Blocks pour plus de flexibilité
69
+ with gr.Blocks() as iface:
70
+ gr.Markdown("# Animal Offense Detector")
71
+
72
+ with gr.Column():
73
+ gr.Markdown("## Enter Your Text Below:")
74
+ text_input = gr.Textbox(lines=5, placeholder="Enter your text here...")
75
+ model_choice = gr.Radio(choices=["Zero-Shot Model", "Few-Shot Model"], label="Choose Model")
76
+ label_output = gr.Label(label="Labels and Scores")
77
+
78
+ gr.Interface(fn=classify_text, inputs=[text_input, model_choice], outputs=label_output)
79
+
80
+ gr.Examples(examples=example_phrases, inputs=[text_input])
81
+
82
+ # Ajouter de l'espacement et une taille de police plus grande pour la documentation
83
+ gr.Markdown("""
84
+ <div style="margin-top: 40px; font-size: 18px;">
85
+
86
+ #### Documentation for `Animal Offense Detector`
87
+
88
+ This script classifies text to determine the level of offense towards animals using two natural language processing models from Hugging Face. Users can choose between a zero-shot model and a few-shot model to evaluate the input text.
89
+
90
+ #### Libraries Used
91
+ - **gradio**: Used to create web-based user interfaces for Python functions.
92
+ - **transformers**: Provides machine learning models for natural language processing tasks.
93
+
94
+ #### Features
95
+ 1. **Zero-Shot Model Classification**:
96
+ - Uses `facebook/bart-large-mnli` to classify text based on several predefined labels.
97
+ - This model can understand and classify text without needing specific training for each task.
98
+
99
+ 2. **Few-Shot Model Classification**:
100
+ - Uses a custom-trained BERT model to evaluate text as "non-offensive" or "offensive".
101
+ - This model provides a quick and accurate classification based on the training data.
102
+
103
+ 3. **Model Selection**:
104
+ - The interface allows the user to choose between the zero-shot and few-shot models to classify the text.
105
+
106
+ 4. **Example Phrases**:
107
+ - Provides example phrases that the user can select to test the models. Each example is designed to test different levels of potential offense.
108
+ </div>
109
+ """)
110
+
111
+ if __name__ == "__main__":
112
+ iface.launch(share=True)