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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
from dotenv import load_dotenv
|
| 3 |
+
import os
|
| 4 |
+
from langchain.llms import OpenAI
|
| 5 |
+
from langchain.chat_models import ChatOpenAI
|
| 6 |
+
from langchain.schema import AIMessage, HumanMessage, SystemMessage
|
| 7 |
+
from langchain.prompts.chat import (
|
| 8 |
+
ChatMessagePromptTemplate,
|
| 9 |
+
SystemMessagePromptTemplate,
|
| 10 |
+
AIMessagePromptTemplate,
|
| 11 |
+
HumanMessagePromptTemplate
|
| 12 |
+
)
|
| 13 |
+
from langchain.callbacks import get_openai_callback
|
| 14 |
+
|
| 15 |
+
# scrapper
|
| 16 |
+
from langchain.document_loaders import AsyncChromiumLoader
|
| 17 |
+
from langchain.document_transformers import BeautifulSoupTransformer
|
| 18 |
+
import requests
|
| 19 |
+
|
| 20 |
+
#data
|
| 21 |
+
import pandas as pd
|
| 22 |
+
import json
|
| 23 |
+
from pandas import json_normalize
|
| 24 |
+
|
| 25 |
+
def rewrite(text, language='Bahasa Indonesia'):
|
| 26 |
+
llm = ChatOpenAI(model='gpt-3.5-turbo-16k', temperature=0)
|
| 27 |
+
message = [
|
| 28 |
+
SystemMessage(content='Your are expert blogger'),
|
| 29 |
+
HumanMessage(content=f'write the following text into 1000 words blog article, use language: {language}, use casual tone. and give clickbait title. TEXT: {text}')
|
| 30 |
+
]
|
| 31 |
+
|
| 32 |
+
with get_openai_callback() as cost:
|
| 33 |
+
output = llm(message).content
|
| 34 |
+
|
| 35 |
+
return output, cost
|
| 36 |
+
|
| 37 |
+
def extract_seo_keywords(text):
|
| 38 |
+
llm = ChatOpenAI(model='gpt-3.5-turbo-16k', temperature=0)
|
| 39 |
+
message = [
|
| 40 |
+
SystemMessage(content='Your are an SEO expert'),
|
| 41 |
+
HumanMessage(content=f'indentify 10 SEO keywords in this text. order by the most relevance keyword. output format: comma separated value. example output: keyword,another keyword,some keyword. TEXT: {text}')
|
| 42 |
+
]
|
| 43 |
+
|
| 44 |
+
with get_openai_callback() as cost:
|
| 45 |
+
keywords = llm(message).content.split(',')
|
| 46 |
+
array_keywords = [i.lstrip() for i in keywords]
|
| 47 |
+
|
| 48 |
+
return array_keywords, cost
|
| 49 |
+
|
| 50 |
+
def summarize(text, language='Bahasa Indonesia'):
|
| 51 |
+
llm = ChatOpenAI(model='gpt-3.5-turbo-16k', temperature=0)
|
| 52 |
+
message = [
|
| 53 |
+
SystemMessage(content='Your are expert copywriter'),
|
| 54 |
+
HumanMessage(content=f'Extract main idea the following article and summarize into 10 bullet point or more. Use language: {language}. TEXT: {text}')
|
| 55 |
+
]
|
| 56 |
+
|
| 57 |
+
with get_openai_callback() as cost:
|
| 58 |
+
summary = llm(message).content
|
| 59 |
+
return summary, cost
|
| 60 |
+
|
| 61 |
+
def scrap(url):
|
| 62 |
+
scraper = AsyncChromiumLoader(url)
|
| 63 |
+
html = scraper.load()
|
| 64 |
+
|
| 65 |
+
extractor = BeautifulSoupTransformer()
|
| 66 |
+
content = extractor.transform_documents(html, tags_to_extract=['h1', 'h2', 'h3', 'h4','h5', 'h6','p'])
|
| 67 |
+
print(content[0].page_content)
|
| 68 |
+
return content[0].page_content
|
| 69 |
+
|
| 70 |
+
def get_keywords_data(keywords):
|
| 71 |
+
my_data = {
|
| 72 |
+
'currency': 'USD',
|
| 73 |
+
'dataSource': 'gkp',
|
| 74 |
+
'kw[]': keywords
|
| 75 |
+
}
|
| 76 |
+
|
| 77 |
+
kw_api_key = os.environ.get('KEYWORD_API')
|
| 78 |
+
auth = f'Bearer {kw_api_key}'
|
| 79 |
+
|
| 80 |
+
my_headers = {
|
| 81 |
+
'Accept': 'application/json',
|
| 82 |
+
'Authorization': auth
|
| 83 |
+
}
|
| 84 |
+
response = requests.post('https://api.keywordseverywhere.com/v1/get_keyword_data', data=my_data, headers=my_headers)
|
| 85 |
+
if response.status_code == 200:
|
| 86 |
+
data = body=response.content.decode('utf-8')
|
| 87 |
+
return data
|
| 88 |
+
else:
|
| 89 |
+
st.write(body=response.content.decode('utf-8'))
|
| 90 |
+
|
| 91 |
+
def main():
|
| 92 |
+
|
| 93 |
+
load_dotenv()
|
| 94 |
+
|
| 95 |
+
st.subheader("Summarize, Extract Keywords, Rewrite")
|
| 96 |
+
|
| 97 |
+
url = st.text_input("Enter URL, start with http:// or https://")
|
| 98 |
+
options = ['Bahasa Indonesia', 'English', 'Japanese', 'France', 'Germany', 'Dutch']
|
| 99 |
+
language = st.selectbox("Output language", options=options)
|
| 100 |
+
submit = st.button("submit")
|
| 101 |
+
content = None
|
| 102 |
+
|
| 103 |
+
if url and submit:
|
| 104 |
+
|
| 105 |
+
|
| 106 |
+
with st.spinner(f'Processing: {url}'):
|
| 107 |
+
content = scrap([url])
|
| 108 |
+
|
| 109 |
+
if content is not None:
|
| 110 |
+
|
| 111 |
+
tab1, tab2, = st.tabs(["Summary", "Rewritten content"])
|
| 112 |
+
|
| 113 |
+
with tab1:
|
| 114 |
+
|
| 115 |
+
with st.spinner('summarizing content..'):
|
| 116 |
+
st.subheader('Summary')
|
| 117 |
+
summary, cost = summarize(content, language)
|
| 118 |
+
st.write(summary)
|
| 119 |
+
st.write(cost)
|
| 120 |
+
|
| 121 |
+
with st.spinner('extracing keywords..'):
|
| 122 |
+
st.subheader('SEO Keywords ')
|
| 123 |
+
keywords, cost = extract_seo_keywords(content)
|
| 124 |
+
|
| 125 |
+
#st.write(keywords)
|
| 126 |
+
st.write(cost)
|
| 127 |
+
|
| 128 |
+
data = get_keywords_data(keywords)
|
| 129 |
+
|
| 130 |
+
# Use json_normalize() to convert JSON to DataFrame
|
| 131 |
+
dict= json.loads(data)
|
| 132 |
+
|
| 133 |
+
# Initialize empty lists to store data
|
| 134 |
+
vol_list = []
|
| 135 |
+
cpc_value_list = []
|
| 136 |
+
keyword_list = []
|
| 137 |
+
competition_list = []
|
| 138 |
+
trend_value_list = []
|
| 139 |
+
|
| 140 |
+
# Iterate through the data and extract relevant information
|
| 141 |
+
for item in dict["data"]:
|
| 142 |
+
vol_list.append(item["vol"])
|
| 143 |
+
cpc_value_list.append(float(item["cpc"]["value"]))
|
| 144 |
+
keyword_list.append(item["keyword"])
|
| 145 |
+
competition_list.append(item["competition"])
|
| 146 |
+
trend_values = [trend["value"] for trend in item["trend"]]
|
| 147 |
+
trend_value_list.append(trend_values)
|
| 148 |
+
|
| 149 |
+
# Create a DataFrame
|
| 150 |
+
df = pd.DataFrame({
|
| 151 |
+
'Keyword': keyword_list,
|
| 152 |
+
'Vol': vol_list,
|
| 153 |
+
'CPC': cpc_value_list,
|
| 154 |
+
'Competition': competition_list,
|
| 155 |
+
'Trend (last 12 months)': trend_value_list
|
| 156 |
+
})
|
| 157 |
+
|
| 158 |
+
# Display the DataFrame
|
| 159 |
+
st.dataframe(
|
| 160 |
+
df,
|
| 161 |
+
column_config={
|
| 162 |
+
"CPC": st.column_config.NumberColumn(
|
| 163 |
+
format="$%f",
|
| 164 |
+
),
|
| 165 |
+
"Trend (last 12 months)": st.column_config.LineChartColumn(y_min=0)
|
| 166 |
+
},
|
| 167 |
+
hide_index=True,
|
| 168 |
+
width=800
|
| 169 |
+
)
|
| 170 |
+
|
| 171 |
+
with st.expander("see extracted content"):
|
| 172 |
+
st.write(content)
|
| 173 |
+
|
| 174 |
+
with tab2:
|
| 175 |
+
st.subheader("Rewrite content")
|
| 176 |
+
|
| 177 |
+
rewrite_button = st.button("Rewrite!")
|
| 178 |
+
|
| 179 |
+
if rewrite_button:
|
| 180 |
+
with st.spinner('rewriting content..'):
|
| 181 |
+
st.subheader('rewritten content')
|
| 182 |
+
output, cost = rewrite(summary, language)
|
| 183 |
+
|
| 184 |
+
st.write(output)
|
| 185 |
+
st.write(cost)
|
| 186 |
+
|
| 187 |
+
|
| 188 |
+
if __name__ == "__main__":
|
| 189 |
+
main()
|
| 190 |
+
|
| 191 |
+
|
| 192 |
+
|
| 193 |
+
|
requirements.txt
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
openai
|
| 2 |
+
langchain
|
| 3 |
+
python-dotenv
|
| 4 |
+
tiktoken
|
| 5 |
+
streamlit
|