greykingreys commited on
Commit
5e65233
·
verified ·
1 Parent(s): de9ee38

Create app.py

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