Spaces:
Build error
Build error
Commit ·
a7d7769
1
Parent(s): 4d2f946
Update app.py
Browse files
app.py
CHANGED
|
@@ -16,67 +16,22 @@ def separate_punc(text):
|
|
| 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 |
-
|
| 66 |
-
('clf', LogisticRegression()),
|
| 67 |
-
])
|
| 68 |
|
| 69 |
# Feed the training data through the pipeline
|
| 70 |
-
|
| 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'
|
|
@@ -89,7 +44,7 @@ def run(text):
|
|
| 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()
|
|
|
|
| 16 |
|
| 17 |
|
| 18 |
kw_model = KeyBERT(model='all-mpnet-base-v2')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
|
| 20 |
from sklearn.pipeline import Pipeline
|
| 21 |
from sklearn.feature_extraction.text import TfidfVectorizer
|
| 22 |
from sklearn.svm import LinearSVC
|
| 23 |
from sklearn.linear_model import LogisticRegression
|
| 24 |
|
| 25 |
+
|
|
|
|
|
|
|
| 26 |
|
| 27 |
# Feed the training data through the pipeline
|
| 28 |
+
|
| 29 |
|
| 30 |
|
| 31 |
def run(text):
|
| 32 |
s=''
|
| 33 |
separate_punc(text)
|
|
|
|
|
|
|
| 34 |
|
|
|
|
| 35 |
keywords = kw_model.extract_keywords(text, keyphrase_ngram_range=(1, 3), stop_words='english', highlight=False,)
|
| 36 |
keywords_list= list(dict(keywords).keys())
|
| 37 |
s=s+'\n'
|
|
|
|
| 44 |
# break
|
| 45 |
# else:
|
| 46 |
# print('Sorry That is all we can suggest')
|
| 47 |
+
return s
|
| 48 |
|
| 49 |
iface = gr.Interface(fn=run, inputs="text", outputs="text")
|
| 50 |
iface.launch()
|