huimanho's picture
Create app.py
41ad2dc verified
raw
history blame
1.49 kB
from flask import Flask, request, render_template
import pandas as pd
import spacy
from transformers import pipeline
# Initialize Flask app
app = Flask(__name__)
# Load spaCy model for preprocessing
nlp = spacy.load("en_core_web_sm")
# Load Hugging Face pipelines
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")
# Function to preprocess text
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)
# 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)