greykingreys's picture
Update app.py
2119f0b verified
import numpy as np
import pandas as pd
import gradio as gr
import tensorflow as tf
import numpy as np
from transformers import pipeline
import joblib
from tensorflow.keras.preprocessing.sequence import pad_sequences
from transformers import pipeline
# Charger le modèle LSTM
model = tf.keras.models.load_model("lstm_model.h5")
# Charger le tokenizer utilisé pendant l'entraînement
tokenizer = joblib.load('tokenizer.joblib')
# Fonction de prédiction pour le lstm
def analyser_sentiment_lstm(tweet):
sequence = tokenizer.texts_to_sequences([tweet])
padded = pad_sequences(sequence)
prediction = model.predict(padded)[0]
sentiment = "Positif" if prediction[0] >= 0.5 else "Négatif"
return {sentiment: float(prediction[0]) if sentiment == "Positif" else 1 - float(prediction[0])}
def analyser_sentiment_camembert(tweet):
# charger le modèle
sentiment_pipeline = pipeline("sentiment-analysis", model="cmarkea/distilcamembert-base-sentiment")
# appliquer le modèle
result = sentiment_pipeline(tweet)[0]['label']
return result
# définir les blocks
demo = gr.Blocks(theme = gr.themes.Monochrome())
# Interface Gradio
interface1 = gr.Interface(
fn=analyser_sentiment_lstm,
inputs=gr.Textbox(lines=3, placeholder="Entrez un tweet en français ici..."),
outputs=gr.Label(num_top_classes=3),
title="Analyse de Sentiment de Tweets avec lstm",
description="Entrez un tweet en français pour obtenir son sentiment (positif, négatif)."
)
interface2 = gr.Interface(
fn = analyser_sentiment_camembert,
inputs=gr.Textbox(lines=3, placeholder="Entrez un tweet en français ici..."),
outputs=gr.Textbox(label='Output'),
title="Analyse de Sentiment de Tweets camembert",
description="Entrez un tweet en français pour obtenir son sentiment."
)
# faire un tabbing des interfaces
with demo:
gr.TabbedInterface([interface1, interface2], ['LSTM_SAM', 'CAMEMBERT_SAM'])
# lancer l'interface
demo.launch()