from diffusers import StableDiffusionPipeline import torch 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) # Create the pipeline and langchain model def load_models(): with torch.no_grad(): model_id = "CompVis/stable-diffusion-v1-4" device = "cuda" pipe = StableDiffusionPipeline.from_pretrained(model_id, torch_dtype=torch.float16) pipe = pipe.to(device) 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) return hub_chain,pipe # Wait for the models to be created with st.spinner("Creating generation pipelines. Please Wait:)..."): hub_chain,pipe = load_models() # Function to generate content @torch.no_grad() def generate_content(topic): content = hub_chain.run(topic) subheadings = [ "Introduction", "History and Background", "Key Concepts and Terminology", "Use Cases and Applications", "Benefits and Drawbacks", "Future Outlook", "Conclusion", ] for subheading in subheadings: if (subheading + ":") in content: content = content.replace(subheading + ":", "## " + subheading + "\n") elif subheading in content: content = content.replace(subheading, "## " + subheading + "\n") return content # generate image @torch.no_grad() def generate_image(topic): prompt = f"blog banner about {topic}" image = pipe(prompt).images[0] return image # Streamlit app st.title("Blog Generator") # Input and button topic = st.text_input("Enter Title for the blog") button_clicked = st.button("Create blog!") st.subheader(topic) # Load existing ideas existing_ideas = load_ideas() st.sidebar.header("Previous Ideas:") # Display existing ideas in the sidebar keys = list(set([key for idea in existing_ideas for key in idea.keys()])) if topic in keys: index = keys.index(topic) selected_idea = st.sidebar.selectbox("Select Idea", keys, key=f"selectbox{topic}", index=index) # Display content and image for the selected idea selected_idea_from_list = next((idea for idea in existing_ideas if selected_idea in idea), None) st.markdown(selected_idea_from_list[selected_idea]["content"]) st.image(selected_idea_from_list[selected_idea]["image_path"]) else: index = 0 # Handle button click if button_clicked: # Generate content and update existing ideas content, image_path = generate_content(topic),generate_image(topic) existing_ideas.append({topic: {"content": content, "image_path": image_path}}) save_ideas(existing_ideas) # Update keys and selected idea in the sidebar keys = list(set([key for idea in existing_ideas for key in idea.keys()])) selected_idea = st.sidebar.selectbox("Select Idea", keys, key=f"selectbox{topic}", index=keys.index(topic)) st.markdown(content) st.image(image_path)