|
from flask import Flask, request, render_template |
|
import pandas as pd |
|
import spacy |
|
from transformers import pipeline |
|
|
|
|
|
app = Flask(__name__) |
|
|
|
|
|
nlp = spacy.load("en_core_web_sm") |
|
|
|
|
|
sentiment_pipeline = pipeline("sentiment-analysis", model="distilbert-base-uncased-finetuned-sst-2-english") |
|
ner_pipeline = pipeline("ner", model="dbmdz/bert-large-cased-finetuned-conll03-english", aggregation_strategy="simple") |
|
|
|
|
|
def preprocess_text(text): |
|
doc = nlp(text) |
|
tokens = [token.lemma_.lower() for token in doc if not token.is_stop and not token.is_punct] |
|
return ' '.join(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) |
|
|
|
|
|
sentiment_result = sentiment_pipeline(cleaned_comments)[0] |
|
|
|
|
|
entities_result = ner_pipeline(cleaned_comments) |
|
|
|
|
|
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) |