Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,57 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 |
+
# 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 |
+
# Charger le modèle LSTM
|
| 26 |
+
model = tf.keras.models.load_model("lstm_model.h5")
|
| 27 |
+
|
| 28 |
+
# Charger le tokenizer utilisé pendant l'entraînement
|
| 29 |
+
tokenizer = joblib.load('tokenizer.joblib')
|
| 30 |
+
|
| 31 |
+
# définir les blocks
|
| 32 |
+
demo = gr.Blocks(theme='SebastianBravo/simci_css')
|
| 33 |
+
|
| 34 |
+
# Interface Gradio
|
| 35 |
+
interface1 = gr.Interface(
|
| 36 |
+
fn=analyser_sentiment_lstm,
|
| 37 |
+
inputs=gr.Textbox(lines=3, placeholder="Entrez un tweet en français ici..."),
|
| 38 |
+
outputs=gr.Label(num_top_classes=2),
|
| 39 |
+
title="Analyse de Sentiment de Tweets avec lstm",
|
| 40 |
+
description="Entrez un tweet en français pour obtenir son sentiment (positif, négatif)."
|
| 41 |
+
)
|
| 42 |
+
|
| 43 |
+
interface2 = gr.Interface(
|
| 44 |
+
fn = analyser_sentiment_camembert,
|
| 45 |
+
inputs=gr.Textbox(lines=3, placeholder="Entrez un tweet en français ici..."),
|
| 46 |
+
outputs=gr.Textbox(label='Output'),
|
| 47 |
+
title="Analyse de Sentiment de Tweets avec camembert",
|
| 48 |
+
description="Entrez un tweet en français pour obtenir son sentiment."
|
| 49 |
+
)
|
| 50 |
+
|
| 51 |
+
|
| 52 |
+
# faire un tabbing des interfaces
|
| 53 |
+
with demo:
|
| 54 |
+
gr.TabbedInterface([interface1, interface2], ['LSTM_SAM', 'CAMEMBERT_SAM'])
|
| 55 |
+
|
| 56 |
+
# lancer l'interface
|
| 57 |
+
demo.launch()
|