Spaces:
Sleeping
Sleeping
| 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 | |
| # importer le tikonizer | |
| #tikonizer = 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 | |
| # Charger le modèle LSTM | |
| model = tf.keras.models.load_model("Sentiment.h5") | |
| # Charger le tokenizer utilisé pendant l'entraînement | |
| tokenizer = joblib.load('tokenizer.joblib') | |
| # définir les blocks | |
| demo = gr.Blocks(theme='JohnSmith9982/small_and_pretty') | |
| # 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=2), | |
| 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 avec camember ", | |
| 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() |