File size: 1,929 Bytes
4084f84
942aa27
 
 
 
514bed2
942aa27
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
import re
import gradio as gr
import pandas as pd
import tensorflow as tf
from nltk.stem import WordNetLemmatizer
from nltk.corpus import stopwords
import nltk


# Download necessary NLTK data
nltk.download('stopwords')
nltk.download('wordnet')
nltk.download('omw-1.4')
nltk.download('averaged_perceptron_tagger')
from nltk.corpus import wordnet

lemmatizer= WordNetLemmatizer()

def get_wordnet_pos(word):
    """Map POS tag to first character lemmatize() accepts."""
    tag = nltk.pos_tag([word])[0][1][0].upper()
    tag_dict = {
        'J': wordnet.ADJ,
        'N': wordnet.NOUN,
        'V': wordnet.VERB,
        'R': wordnet.ADV
    }
    return tag_dict.get(tag, wordnet.NOUN)

def lemmatize_content(content):
    lemmatized_content = re.sub('[^a-zA-Z]', ' ', content)
    lemmatized_content = lemmatized_content.lower()
    lemmatized_content = lemmatized_content.split()
    lemmatized_content = [
        lemmatizer.lemmatize(word, get_wordnet_pos(word))
        for word in lemmatized_content
        if word not in stopwords.words('english')
    ]
    lemmatized_content = ' '.join(lemmatized_content)
    return lemmatized_content

#load the model
model = tf.keras.models.load_model('twitter_sentiment_analysis_epoch4.h5')

loaded_model = tf.keras.models.load_model('vectorizer_model')
loaded_vectorizer = loaded_model.layers[0]

def score_comment(comment):
    # Preprocess the comment
    comment = lemmatize_content(comment)
    # Vectorize the input comment
    vectorized_comment = loaded_vectorizer([comment])
    # Predict using the loaded model
    result = model.predict(vectorized_comment)

    # Generate the output text based on predictions
    text = ''
    if result<0.5:
       text= 'Negative'
    else:
       text = 'Positive'
    return text

interface = gr.Interface(
    fn=score_comment,
    inputs=gr.Textbox(lines=2, placeholder='Tweet to score'),
    outputs='text'
)

interface.launch()