Dougaya commited on
Commit
92434c3
·
verified ·
1 Parent(s): 9ad4c9e

Update app.py

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