AmaarAli/NewsTopicModeling
0
1from flask import Flask,request,render_template,jsonify2import pandas as pd3import numpy as np4import matplotlib.pyplot as plt5import seaborn as sns6import nltk7nltk.data.path.append('./nltk_data')8from nltk.corpus import stopwords9from nltk.tokenize import word_tokenize10import joblib11import string12from flask_cors import CORS13 14 15app = Flask(__name__)16 17CORS(app)18 19try:20 model = joblib.load('NMF_Model.joblib')21 vectorizer = joblib.load('Vectorizer.joblib')22 print('Model and vectorizer were loaded')23 predicted_ready = True24 feature_names = vectorizer.get_feature_names_out()25except Exception as e:26 print(f'Error occured while loadin the model :- {e}')27 vectorizer = None28 model = None29 feature_names = None30 prediction_ready = False31 32 33def lowercasing(txt):34 return txt.lower()35 36def remove_stopwords(txt):37 stop_words = set(stopwords.words('english'))38 try:39 words = word_tokenize(txt)40 except Exception as e:41 print(f'Error occured while removing stopwords :- {e}')42 return ""43 44 cleaned_words = [i.lower() for i in words if i.isalpha() and i.lower() not in stop_words]45 return " ".join(cleaned_words)46 47def remove_numbers(txt):48 return "".join([i for i in txt if not i.isdigit()])49 50def remove_punctuation(txt):51 return txt.translate(str.maketrans('','',string.punctuation))52 53 54def preprocess_txt(text):55 text = lowercasing(text)56 text = remove_numbers(text)57 text = remove_punctuation(text)58 text = text.replace('\n','').replace('\r','')59 text = remove_stopwords(text)60 return text61 62 63topic_names = {64 0: "Business & Finance",65 1: "Politics",66 2: "Sports (Rugby & General)",67 3: "Film & Awards",68 4: "Economics & Growth",69 5: "Government & Law",70 6: "Olympics & Athletics",71 7: "Energy & Oil",72 8: "Technology & Software",73 9: "Mobile & Digital Media"74}75 76def get_top_words(topic_id,num_words=300):77 if not predicted_ready:78 return []79 80 topic_weights = model.components_[topic_id]81 top_word_indices = topic_weights.argsort()[:-num_words -1:-1]82 83 top_words = []84 for i in top_word_indices:85 word = feature_names[i]86 weight = float(topic_weights[i])87 top_words.append([word,weight])88 return top_words89 90 91 92 93@app.route('/')94def index():95 return render_template('index.html')96 97@app.route('/predict',methods=['POST'])98def predict():99 if model is None or vectorizer is None:100 return jsonify({'error' : 'Model not loaded. Please check server logs'})101 102 data = request.get_json()103 if not data or 'text' not in data:104 return jsonify({'error' : 'No Text field provided, please provide it'})105 106 input_text = data['text']107 print('Preprocessed before :-', input_text)108 preprocessed_text = preprocess_txt(input_text)109 print('Preprocessed After :-',preprocessed_text)110 111 112 if not preprocessed_text.strip():113 return jsonify({114 'predicted_topic' : 'N/A',115 'topic_id' : -1,116 'confidence' : 0.00,117 'message' : 'Input text was filtered out during preprocessing. Try a longer, more descriptive text.'118 })119 120 121 vectorized_text = vectorizer.transform([preprocessed_text])122 topic_distribution = model.transform(vectorized_text)123 124 dominant_topic_id = topic_distribution.argmax()125 topic_distribution_probability = topic_distribution.max()126 127 predicted_topic_name = topic_names.get(dominant_topic_id,'Unknown Id')128 129 top_words = get_top_words(dominant_topic_id)130 131 response = {132 'predicted_topic' : predicted_topic_name,133 'topic_id' : int(dominant_topic_id),134 'confidence' : float(topic_distribution_probability),135 'top_words' : top_words136 }137 138 return jsonify(response)139 140 141if __name__ == '__main__':142 app.run(debug=True,host='0.0.0.0')143 144 145 146 147 148 149 150 