diallo62-gs commited on
Commit
9daa392
·
verified ·
1 Parent(s): 94b8e41

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +59 -0
app.py ADDED
@@ -0,0 +1,59 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import tensorflow as tf
3
+ import numpy as np
4
+ import joblib
5
+ from tensorflow.keras.preprocessing.sequence import pad_sequences
6
+ from transformers import pipeline
7
+ # importer le tikonizer
8
+ #tikonizer = joblib.load('tokenizer.joblib')
9
+ # Fonction de prédiction pour le lstm
10
+ def analyser_sentiment_lstm(tweet):
11
+ sequence = tokenizer.texts_to_sequences([tweet])
12
+ padded = pad_sequences(sequence)
13
+ prediction = model.predict(padded)[0]
14
+
15
+ sentiment = "Positif" if prediction[0] >= 0.5 else "Négatif"
16
+ return {sentiment: float(prediction[0]) if sentiment == "Positif" else 1 - float(prediction[0])}
17
+
18
+ def analyser_sentiment_camembert(tweet):
19
+ # charger le modèle
20
+ sentiment_pipeline = pipeline("sentiment-analysis", model="cmarkea/distilcamembert-base-sentiment")
21
+
22
+ # appliquer le modèle
23
+ result = sentiment_pipeline(tweet)[0]['label']
24
+ return result
25
+
26
+
27
+ # Charger le modèle LSTM
28
+ model = tf.keras.models.load_model("Sentiment.h5")
29
+
30
+ # Charger le tokenizer utilisé pendant l'entraînement
31
+ tokenizer = joblib.load('tokenizer.joblib')
32
+
33
+ # définir les blocks
34
+ demo = gr.Blocks(theme='JohnSmith9982/small_and_pretty')
35
+
36
+ # Interface Gradio
37
+ interface1 = gr.Interface(
38
+ fn=analyser_sentiment_lstm,
39
+ inputs=gr.Textbox(lines=3, placeholder="Entrez un tweet en français ici..."),
40
+ outputs=gr.Label(num_top_classes=2),
41
+ title="Analyse de Sentiment de Tweets avec lstm ",
42
+ description="Entrez un tweet en français pour obtenir son sentiment (positif, négatif)."
43
+ )
44
+
45
+ interface2 = gr.Interface(
46
+ fn = analyser_sentiment_camembert,
47
+ inputs=gr.Textbox(lines=3, placeholder="Entrez un tweet en français ici..."),
48
+ outputs=gr.Textbox(label='Output'),
49
+ title="Analyse de Sentiment de Tweets avec camember ",
50
+ description="Entrez un tweet en français pour obtenir son sentiment."
51
+ )
52
+
53
+
54
+ # faire un tabbing des interfaces
55
+ with demo:
56
+ gr.TabbedInterface([interface1, interface2], ['LSTM_SAM', 'CAMEMBERT_SAM'])
57
+
58
+ # lancer l'interface
59
+ demo.launch()