File size: 2,607 Bytes
673a948
 
 
 
 
 
 
 
 
 
 
 
 
 
99c7afa
 
 
 
 
 
673a948
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
import os
import streamlit as st

from dotenv import dotenv_values

from langchain.prompts import PromptTemplate
from langchain.chains import RetrievalQA, LLMChain
from langchain.document_loaders import TextLoader
from langchain.embeddings import HuggingFaceEmbeddings
from langchain.llms import HuggingFaceEndpoint
from langchain.text_splitter import CharacterTextSplitter
from langchain.vectorstores import Chroma

env_vars = dotenv_values(".env")
LOCAL=False
if not LOCAL:
    env_vars = {
        "HUGGINGFACE_API_TOKEN": st.secrets["HUGGINGFACE_API_TOKEN"],
        "HUGGINGFACE_MODEL": st.secrets["HUGGINGFACE_MODEL"]
    }

# prepare Falcon Huggingface API
llm = HuggingFaceEndpoint(
            endpoint_url= f"https://api-inference.huggingface.co/models/{env_vars['HUGGINGFACE_MODEL']}",
            huggingfacehub_api_token=env_vars["HUGGINGFACE_API_TOKEN"],
            task="text-generation",
            model_kwargs = {
                "min_length": 8192,
                "max_length":8192,
                "temperature":0.9,
                "max_new_tokens":4000,
                "num_return_sequences":1
            }
        )


keyword_prompt_template = "What are 10 important keywords related too: {word}? Only return a list of words and do not include any duplicates."
keyword_chain = LLMChain(
    llm=llm,
    prompt=PromptTemplate.from_template(keyword_prompt_template),
)

outline_prompt_template = "Create an outline for an article about: {word}."
outline_chain = LLMChain(
    llm=llm,
    prompt=PromptTemplate.from_template(outline_prompt_template),
)

article_prompt_template = """
Act as an expert SEO Writer.
Write a well crafted article using the given outline as a guide.
Use the relavant keywords you are provided.
Apply EEAT principles and SEO best practices.
It is important that the content is at least 1500 words.
Be sure to include section headers.
OUTLINE: {outline}
KEYWORDS: {keywords}
"""
article_chain = LLMChain(
    llm=llm,
    prompt=PromptTemplate.from_template(article_prompt_template),
)   

st.title("Blog Writer")
keyword = st.text_input("Input the keyword you wish to write about")

if keyword:
    with st.spinner("Writing..."):
        st.write(f"Writing about: {keyword}")
        with st.expander("Keywords"):
            keywords = keyword_chain.run(keyword)
            st.write(keywords)
        with st.expander("Outline"):
            outline = outline_chain.run(keyword)
            st.write(outline)
        with st.expander("Article"):
            article = article_chain.run(outline=outline, keywords=keywords)
            st.write(article)