File size: 1,868 Bytes
41ad2dc
 
cb771e6
 
 
 
41ad2dc
 
5c0dc79
fc3808f
41ad2dc
 
 
cb771e6
 
 
 
 
 
 
 
41ad2dc
 
400a4ed
41ad2dc
 
 
 
cb771e6
 
 
 
 
41ad2dc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from flask import Flask, request, render_template
import pandas as pd
import nltk
from nltk.tokenize import word_tokenize
from nltk.corpus import stopwords
from nltk.stem import WordNetLemmatizer
from transformers import pipeline



# Initialize Flask app
app = Flask(__name__)

# Download NLTK resources
nltk.download('punkt')
nltk.download('stopwords')
nltk.download('wordnet')

# Initialize NLTK components
lemmatizer = WordNetLemmatizer()
stop_words = set(stopwords.words('english'))

# Load Hugging Face pipelines
sentiment_pipeline = pipeline("sentiment-analysis", model="distilbert/distilbert-base-uncased-finetuned-sst-2-english")
ner_pipeline = pipeline("ner", model="dbmdz/bert-large-cased-finetuned-conll03-english", aggregation_strategy="simple")

# Function to preprocess text
def preprocess_text(text):
    # Tokenize
    tokens = word_tokenize(text)
    # Remove stop words and lemmatize
    cleaned_tokens = [lemmatizer.lemmatize(token.lower()) for token in tokens if token.isalpha() and token.lower() not in stop_words]
    return ' '.join(cleaned_tokens)

@app.route('/')
def home():
    return render_template('index.html')

@app.route('/analyze', methods=['POST'])
def analyze():
    if request.method == 'POST':
        comments = request.form['comments']
        cleaned_comments = preprocess_text(comments)

        # Analyze sentiment
        sentiment_result = sentiment_pipeline(cleaned_comments)[0]

        # Analyze entities
        entities_result = ner_pipeline(cleaned_comments)

        # Prepare results for rendering
        result = {
            'original_comment': comments,
            'cleaned_comment': cleaned_comments,
            'sentiment': sentiment_result,
            'entities': entities_result
        }

        return render_template('result.html', result=result)

if __name__ == '__main__':
    app.run(debug=True)