baillodev's picture
Create app.py
cf230b2 verified
import gradio as gr
import tensorflow as tf
import numpy as np
import joblib
from tensorflow.keras.preprocessing.sequence import pad_sequences
from transformers import pipeline
# 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
# 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')
# définir les blocks
demo = gr.Blocks(theme = 'earneleh/paris')
# 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",
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",
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()