File size: 1,881 Bytes
cf230b2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
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()