Spaces:
Build error
Build error
Commit ·
b790f42
1
Parent(s): a3cb048
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,95 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import spacy
|
| 2 |
+
nlp = spacy.load("en_core_web_sm")
|
| 3 |
+
from keybert import KeyBERT
|
| 4 |
+
import numpy as np
|
| 5 |
+
import pandas as pd
|
| 6 |
+
import os
|
| 7 |
+
import re
|
| 8 |
+
import json
|
| 9 |
+
import seaborn as sns
|
| 10 |
+
import gradio as gr
|
| 11 |
+
|
| 12 |
+
|
| 13 |
+
|
| 14 |
+
def separate_punc(text):
|
| 15 |
+
return [token.text.lower() for token in nlp(text) if token.text not in '\n\n \n\n\n!"-#$%&()--.*+,-/:;<=>?@[\\]^_`{|}~\t\n ']
|
| 16 |
+
|
| 17 |
+
|
| 18 |
+
kw_model = KeyBERT(model='all-mpnet-base-v2')
|
| 19 |
+
with open('News_Category_Dataset_v3.json','r') as f:
|
| 20 |
+
jdata = f.read()
|
| 21 |
+
|
| 22 |
+
jdata2 = [json.loads(line) for line in jdata.split('\n') if line]
|
| 23 |
+
df = pd.DataFrame.from_records(jdata2)
|
| 24 |
+
df.head()
|
| 25 |
+
|
| 26 |
+
|
| 27 |
+
selected_cat=['POLITICS', 'ENTERTAINMENT', 'TRAVEL', 'STYLE & BEAUTY','HEALTHY LIVING','BUSINESS','SPORTS']
|
| 28 |
+
data=df[['category','short_description']][df['category'].isin(selected_cat)].reset_index(drop=True)
|
| 29 |
+
display(data['category'].value_counts())
|
| 30 |
+
|
| 31 |
+
|
| 32 |
+
POLITICS = data[data.category == 'POLITICS']
|
| 33 |
+
ENTERTAINMENT = data[data.category == 'ENTERTAINMENT']
|
| 34 |
+
TRAVEL=data[data.category == 'TRAVEL']
|
| 35 |
+
STYLE_BEAUTY=data[data.category == 'STYLE & BEAUTY']
|
| 36 |
+
HEALTHY_LIVING=data[data.category == 'HEALTHY LIVING']
|
| 37 |
+
BUSINESS=data[data.category == 'BUSINESS']
|
| 38 |
+
SPORTS=data[data.category == 'SPORTS']
|
| 39 |
+
|
| 40 |
+
|
| 41 |
+
POLITICS_sample = POLITICS.sample(n=5077)
|
| 42 |
+
ENTERTAINMENT_sample = ENTERTAINMENT.sample(n=5077)
|
| 43 |
+
TRAVEL_sample = TRAVEL.sample(n=5077)
|
| 44 |
+
STYLE_BEAUTY_sample = STYLE_BEAUTY.sample(n=5077)
|
| 45 |
+
HEALTHY_LIVING_sample = HEALTHY_LIVING.sample(n=5077)
|
| 46 |
+
BUSINESS_sample = BUSINESS.sample(n=5077)
|
| 47 |
+
SPORTS_sample = SPORTS.sample(n=5077)
|
| 48 |
+
|
| 49 |
+
data = pd.concat([POLITICS_sample, ENTERTAINMENT_sample,TRAVEL_sample,STYLE_BEAUTY_sample,HEALTHY_LIVING_sample,BUSINESS_sample,SPORTS_sample], axis=0)
|
| 50 |
+
|
| 51 |
+
|
| 52 |
+
from sklearn.model_selection import train_test_split
|
| 53 |
+
|
| 54 |
+
X = data['short_description']
|
| 55 |
+
y = data['category']
|
| 56 |
+
|
| 57 |
+
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.33, random_state=42)
|
| 58 |
+
|
| 59 |
+
|
| 60 |
+
from sklearn.pipeline import Pipeline
|
| 61 |
+
from sklearn.feature_extraction.text import TfidfVectorizer
|
| 62 |
+
from sklearn.svm import LinearSVC
|
| 63 |
+
from sklearn.linear_model import LogisticRegression
|
| 64 |
+
|
| 65 |
+
text_clf = Pipeline([('tfidf', TfidfVectorizer()),
|
| 66 |
+
('clf', LogisticRegression()),
|
| 67 |
+
])
|
| 68 |
+
|
| 69 |
+
# Feed the training data through the pipeline
|
| 70 |
+
text_clf.fit(X_train, y_train)
|
| 71 |
+
|
| 72 |
+
|
| 73 |
+
def run(text):
|
| 74 |
+
s=''
|
| 75 |
+
separate_punc(text)
|
| 76 |
+
x=[text]
|
| 77 |
+
y=mod.predict(x)
|
| 78 |
+
|
| 79 |
+
s=s+'This article belongs to: ' + "\033[1m" + y[0] + "\033[0m" + ' category'
|
| 80 |
+
keywords = kw_model.extract_keywords(text, keyphrase_ngram_range=(1, 3), stop_words='english', highlight=False,)
|
| 81 |
+
keywords_list= list(dict(keywords).keys())
|
| 82 |
+
s=s+'\n'
|
| 83 |
+
for i in range (len(keywords_list)):
|
| 84 |
+
s=s+'We suggest ' + "\033[1m" + keywords_list[i] + "\033[0m" + ' as a topic name for the given article '
|
| 85 |
+
# if i<=len(keywords_list):
|
| 86 |
+
# print('Would you like another suggestion?')
|
| 87 |
+
# f=input()
|
| 88 |
+
# if f=='No':
|
| 89 |
+
# break
|
| 90 |
+
# else:
|
| 91 |
+
# print('Sorry That is all we can suggest')
|
| 92 |
+
|
| 93 |
+
|
| 94 |
+
iface = gr.Interface(fn=run, inputs="text", outputs="text")
|
| 95 |
+
iface.launch()
|