File size: 3,542 Bytes
06e1f20
 
c1dcf2e
c2634bf
 
e21d665
313c1bf
c1dcf2e
313c1bf
4173b83
06e1f20
3b2c313
0fb5afb
3b2c313
 
 
 
 
06e1f20
 
 
 
 
c16758a
06e1f20
3b2c313
06e1f20
 
4173b83
a8b001c
 
 
 
 
 
c2634bf
a8b001c
 
 
 
 
 
 
06e1f20
e16ff1d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
313c1bf
 
 
 
 
 
 
 
c6e8b16
313c1bf
c6e8b16
4a010be
5b1bf43
 
 
e16ff1d
 
 
 
5b1bf43
e16ff1d
c6e8b16
 
5b1bf43
e16ff1d
c1a74e5
5b1bf43
 
c6e8b16
4a010be
06e1f20
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
310f369
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
import gradio as gr
import torch
import nltk
# from nltk.corpus import stopwords
# from nltk.stem import WordNetLemmatizer
from nltk.tokenize import word_tokenize
from nltk.corpus import stopwords
nltk.download('punkt')
nltk.download('stopwords')

from transformers import pipeline, AutoTokenizer, AutoModelForSequenceClassification, AutoModelForTokenClassification, AutoConfig

# Load your model and tokenizer from local paths using SafeTensors
model_NER = AutoModelForTokenClassification.from_pretrained("./NER_model")
tokenizer_NER = AutoTokenizer.from_pretrained("./tokenizer_NER")

model_path = './model'
tokenizer_path = './tokenizer'

model_sentiment = AutoModelForSequenceClassification.from_pretrained(model_path)
tokenizer_sentiment = AutoTokenizer.from_pretrained(tokenizer_path)

clf_sentiment = pipeline("text-classification", model=model_sentiment, tokenizer=tokenizer_sentiment)
clf_ner = pipeline("ner", model=model_NER, tokenizer=tokenizer_NER)

config = AutoConfig.from_pretrained("./NER_model")
label_list = config.id2label


# def preprocess(text):
#     stop_words = set(stopwords.words('english'))
#     lemmatizer = WordNetLemmatizer()
#     tokens = word_tokenize(text)
#     processed = [lemmatizer.lemmatize(word.lower()) for word in tokens if word.isalpha() and word not in stop_words]
#     return ' '.join(processed)

def analyze_sentiment(text):
    # text = preprocess(text)
    predictions = clf_sentiment(text)
    label_mapping = {'LABEL_0': 'Negative', 'LABEL_1': 'Neutral', 'LABEL_2': 'Positive'}
    predicted_label = label_mapping[predictions[0]['label']]
    confidence = f"{predictions[0]['score'] * 100:.2f}%"
    return f"Predicted Label: {predicted_label}\nConfidence: {confidence}"

LABEL = ['O',
 'B-storage',
 'I-storage',
 'B-performance',
 'I-performance',
 'B-battery',
 'I-battery',
 'B-camera',
 'I-camera',
 'B-charging',
 'I-charging',
 'B-screen',
 'I-screen',
 'B-audio',
 'I-audio',
 'B-feature',
 'I-feature',
 'B-connection',
 'I-connection',
 'B-button',
 'I-button']

def preprocess(text):
    if not isinstance(text, str):
        return ""
    stop_words = set(stopwords.words('english'))
    tokens = word_tokenize(text)
    processed = [word.lower() for word in tokens if word.isalnum() and word not in stop_words]
    return ' '.join(processed)

def analyze_ner(text):
    text = preprocess(text)
    predictions = clf_ner(text)
    print(predictions)  # Debugging statement to see the structure of the output
    keys = []
    values = []

    for i, label in enumerate(LABEL):
        keys.append(f'LABEL_{i}')
        values.append(label)


    label_mapping = { k:v for (k,v) in zip(keys, values) }
    results = []
    for pred in predictions:
        result = f"""Word: {pred['word']}
        Predicted Tag: {label_mapping[pred['entity']]}
        Confidence Probability: {pred['score'] * 100:.2f}%\n"""
        results.append(result)

    return "\n".join(results)

def analyze_text(text):
    sentiment_result = analyze_sentiment(text)
    ner_result = analyze_ner(text)
    return sentiment_result, ner_result

interface = gr.Interface(
    fn=analyze_text,
    inputs=gr.Textbox(lines=2, placeholder="Enter text here..."),
    outputs=[
        gr.Textbox(label="Sentiment Analysis Result"),
        gr.Textbox(label="Named Entity Recognition Result")
    ],
    title="Text Analysis",
    description="This interface allows you to perform Sentiment Analysis and Named Entity Recognition (NER) on text."
)

if __name__ == "__main__":
    interface.launch(share=True)