blog / app.py
Mo-alaa's picture
Update app.py
717fa5f
raw
history blame
2.06 kB
%%writefile app.py
from langchain.chains import LLMChain
from langchain.llms import HuggingFaceHub
from langchain.prompts import PromptTemplate
import streamlit as st
import json
# Load existing ideas from a file
def load_ideas():
try:
with open("ideas.json", "r") as file:
ideas = json.load(file)
except FileNotFoundError:
ideas = []
return ideas
# Save ideas to a file
def save_ideas(ideas):
with open("ideas.json", "w") as file:
json.dump(ideas, file)
topic = st.text_input("Enter Topic for the bog")
button_clicked = st.button("Create blog!")
existing_ideas = load_ideas()
st.sidebar.header("Previous Ideas:")
selected_idea = st.sidebar.selectbox("Select Idea", existing_ideas, key="selectbox")
st.markdown(selected_idea)
if button_clicked:
hub_llm = HuggingFaceHub(repo_id ="HuggingFaceH4/zephyr-7b-beta")
prompt = PromptTemplate(
input_variables = ['keyword'],
template = """
Write a comprehensive article about {keyword} covering the following aspects:
Introduction, History and Background, Key Concepts and Terminology, Use Cases and Applications, Benefits and Drawbacks, Future Outlook, Conclusion
Ensure that the article is well-structured, informative, and at least 1500 words long. Use SEO best practices for content optimization.
""")
hub_chain = LLMChain(prompt=prompt,llm = hub_llm,verbose=True)
content = hub_chain.run(topic)
subheadings = [
"Introduction:",
"History and Background:",
"Key Concepts and Terminology:",
"Use Cases and Applications:",
"Benefits and Drawbacks:",
"Future Outlook:",
"Conclusion:",
]
organized_content = ""
for subheading in subheadings:
if subheading in content:
content = content.replace(subheading,"## "+subheading+"\n")
existing_ideas.append({keyword:content})
save_ideas(existing_ideas)
selected_idea = st.sidebar.selectbox("Select Idea", existing_ideas, key="selectbox")
st.markdown(selected_idea)