File size: 3,997 Bytes
06696c4
35bafe7
f5a396a
 
 
3776948
58009b8
ac462f6
717fa5f
2bd2884
717fa5f
 
 
 
 
 
 
 
 
 
 
 
 
2bd2884
3776948
 
 
6cf25d0
 
 
 
 
 
 
 
 
 
 
717fa5f
6cf25d0
 
 
 
 
 
 
 
 
717fa5f
6cf25d0
 
 
 
 
717fa5f
6cf25d0
be64c97
2bd2884
 
06696c4
3776948
 
 
 
06696c4
 
3776948
 
 
 
 
 
 
 
 
 
35bafe7
3776948
58009b8
3776948
35bafe7
06696c4
 
35bafe7
06696c4
 
 
 
 
 
2bd2884
06696c4
 
 
 
8f9f452
 
06696c4
f942032
8f9f452
373573b
06696c4
 
6cf25d0
 
 
3776948
 
 
8f9f452
6cf25d0
 
 
8f9f452
3776948
 
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
from diffusers import StableDiffusionPipeline
import torch
from langchain.chains import LLMChain
from langchain.llms import HuggingFaceHub
from langchain.prompts import PromptTemplate
import requests
import base64
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)

# Function to generate content
@torch.no_grad()
def generate_content(topic):
    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",
    ]

    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

import io
from PIL import Image
# Function to generate an image using the pre-created or newly created pipeline
@torch.no_grad()
def generate_image(topic):
  API_URL = "https://api-inference.huggingface.co/models/runwayml/stable-diffusion-v1-5"
  headers = {"Authorization": "Bearer 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"}

  def query(payload):
    response = requests.post(API_URL, headers=headers, json=payload)
    return response.content
  image_bytes = query({
    "inputs": f"A blog banner about {topic}",
  })
  # You can access the image with PIL.Image for example

  image = Image.open(io.BytesIO(image_bytes))
  image.save(f"{topic}.png")
  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!")
# 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.subheader(selected_idea)
    st.image(selected_idea_from_list[selected_idea]["image_path"])
    st.markdown(selected_idea_from_list[selected_idea]["content"])
else:
    index = 0
# Handle button click
if button_clicked:
    # Generate content and update existing ideas
    content, image = generate_content(topic),generate_image(topic)
    if image:
      image_path = f"{topic}.png"
    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.image(image)
    st.markdown(content)