Spaces:
Sleeping
Sleeping
# -*- coding: utf-8 -*- | |
"""Blog Title Recommendation System.ipynb | |
Automatically generated by Colaboratory. | |
Original file is located at | |
https://colab.research.google.com/drive/1uW25ri8AzKp9Y_YrzXbxMb2Vmi8Guel4 | |
### Hello There! | |
#### Welcome to my project :) | |
# Blog Title Recommendation System | |
This model lets you feed your blog post into the system and it recommends a suitable title, subtitle, and keywords for you. | |
Also, You can regenerate recommendations if the first suggestions are not really to your fancy :) | |
So let's get started! | |
""" | |
# pip install tensorflow streamlit | |
# Import necessary libraries | |
import tensorflow as tf | |
import pandas as pd | |
import numpy as np | |
import streamlit as st | |
from tensorflow.keras.preprocessing.text import Tokenizer | |
from tensorflow.keras.preprocessing.sequence import pad_sequences | |
from tensorflow.keras.models import load_model | |
# Load data | |
blog_content_df = pd.read_csv('blog_content.csv') | |
blog_metadata_df = pd.read_csv('blog_metadata.csv') | |
# Merge data | |
blog_df = pd.merge(blog_content_df, blog_metadata_df, on='blog_id') | |
# Tokenize text data | |
tokenizer = Tokenizer(num_words=10000, oov_token='<OOV>') | |
tokenizer.fit_on_texts(blog_df['blog_body']) | |
sequences = tokenizer.texts_to_sequences(blog_df['blog_body']) | |
padded_sequences = pad_sequences(sequences, maxlen=200) | |
# Create a dictionary to map category names to category IDs | |
category_mapping = {'Medium Partner Program': 0, 'Artificial Intelligence': 1, 'Deep Learning': 2} # Add more categories as needed | |
blog_df['category_id'] = blog_df['blog_category'].map(category_mapping) | |
# Define your model architecture (a simple example) | |
model = tf.keras.Sequential([ | |
tf.keras.layers.Embedding(input_dim=10000, output_dim=128, input_length=200), | |
tf.keras.layers.Flatten(), | |
tf.keras.layers.Dense(128, activation='relu'), | |
tf.keras.layers.Dense(64, activation='relu'), | |
tf.keras.layers.Dense(5, activation='softmax') | |
]) | |
model.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['accuracy']) | |
# Train the model (you may need more advanced architectures and hyperparameter tuning) | |
model.fit(padded_sequences, blog_df['category_id'], epochs=5, validation_split=0.2) | |
# Save the trained model to a file | |
model.save('blogsalot.h5') | |
# Load the trained model | |
model = load_model('blogsalot.h5') | |
# Streamlit app | |
st.title("Blog Recommendation System") | |
# User input | |
user_input = st.text_area("Enter your blog content:") | |
user_category = st.selectbox("Select the main category:", ['Food', 'Science', 'Finance']) | |
# Recommendation options | |
recommendation_type = st.radio("Choose a recommendation type:", ['Title', 'Subtitle', 'Keyword Tags']) | |
if st.button("Generate Recommendation"): | |
# Preprocess user input | |
user_input_sequence = tokenizer.texts_to_sequences([user_input]) | |
user_input_padded = pad_sequences(user_input_sequence, maxlen=200) | |
# Make a prediction | |
prediction = model.predict(user_input_padded) | |
if recommendation_type == 'Title': | |
# Select the recommended title based on the predicted category | |
recommended_title = blog_metadata_df[blog_metadata_df['category'] == user_category]['title'].iloc[0] | |
st.success(f"Recommended Title: {recommended_title}") | |
elif recommendation_type == 'Subtitle': | |
# Select the recommended subtitle based on the predicted category | |
recommended_subtitle = blog_metadata_df[blog_metadata_df['category'] == user_category]['subtitle'].iloc[0] | |
st.success(f"Recommended Subtitle: {recommended_subtitle}") | |
elif recommendation_type == 'Keyword Tags': | |
# Select the recommended keyword tags based on the predicted category | |
recommended_keywords = blog_metadata_df[blog_metadata_df['category'] == user_category][ | |
['blog_keyword1', 'blog_keyword2', 'blog_keyword3', 'blog_keyword4', 'blog_keyword5'] | |
].iloc[0] | |
st.success(f"Recommended Keyword Tags: {', '.join(recommended_keywords)}") |