huimanho's picture
Update app.py
5c0dc79 verified
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)