Spaces:
Mooo-kf
/

Mo-alaa commited on
Commit
06696c4
1 Parent(s): 8c4bc17

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +32 -37
app.py CHANGED
@@ -1,4 +1,4 @@
1
- from diffusers import StableDiffusionXLPipeline
2
  import torch
3
  from langchain.chains import LLMChain
4
  from langchain.llms import HuggingFaceHub
@@ -21,6 +21,7 @@ def save_ideas(ideas):
21
  json.dump(ideas, file)
22
 
23
  # Function to generate content
 
24
  def generate_content(topic):
25
  hub_llm = HuggingFaceHub(repo_id="HuggingFaceH4/zephyr-7b-beta")
26
  prompt = PromptTemplate(
@@ -52,51 +53,45 @@ def generate_content(topic):
52
 
53
  return content
54
 
55
- def make_pipe():
56
- pipe = StableDiffusionXLPipeline.from_pretrained("segmind/SSD-1B", torch_dtype=torch.float16, use_safetensors=True, variant="fp16")
57
- pipe.to("cuda")
58
- return pipe
59
-
60
- # generate image
61
- def generate_image(pipe,topic):
62
- prompt = f"A banner for a blog about{topic}" # Your prompt here
63
- neg_prompt = "ugly, blurry, poor quality" # Negative prompt here
64
- image = pipe(prompt=prompt, negative_prompt=neg_prompt).images[0]
65
- return image
66
-
67
 
68
 
69
- pipe = make_pipe()
70
- with st.spinner('Please Wait for the models to train...'):
71
- if pipe:
72
- # Streamlit app
73
- st.title("Blog Generator")
 
 
 
 
 
74
 
75
- # Input and button
76
- topic = st.text_input("Enter Title for the blog")
77
- button_clicked = st.button("Create blog!")
78
- st.subheader(topic)
79
- # Load existing ideas
80
- existing_ideas = load_ideas()
81
- st.sidebar.header("Previous Ideas:")
82
 
83
- # Display existing ideas in the sidebar
84
- keys = list(set([key for idea in existing_ideas for key in idea.keys()]))
85
- if topic in keys:
86
- index = keys.index(topic)
87
- selected_idea = st.sidebar.selectbox("Select Idea", keys, key="selectbox", index=index)
88
- # Display content for the selected idea
89
- selected_idea_from_list = next((idea for idea in existing_ideas if selected_idea in idea), None)
90
- st.markdown(selected_idea_from_list[selected_idea])
91
- else:
92
- index = 0
93
- st.success('')
94
 
 
 
 
 
 
 
 
95
 
 
 
 
 
 
 
 
 
 
 
96
  # Handle button click
97
  if button_clicked:
98
  # Generate content and update existing ideas
99
- image = generate_image(pipe,topic)
100
  st.image(image)
101
  content = generate_content(topic)
102
  existing_ideas.append({topic: content})
 
1
+ from diffusers import StableDiffusionPipeline
2
  import torch
3
  from langchain.chains import LLMChain
4
  from langchain.llms import HuggingFaceHub
 
21
  json.dump(ideas, file)
22
 
23
  # Function to generate content
24
+ @torch.no_grad()
25
  def generate_content(topic):
26
  hub_llm = HuggingFaceHub(repo_id="HuggingFaceH4/zephyr-7b-beta")
27
  prompt = PromptTemplate(
 
53
 
54
  return content
55
 
 
 
 
 
 
 
 
 
 
 
 
 
56
 
57
 
58
+ # generate image
59
+ @torch.no_grad()
60
+ def generate_image(topic):
61
+ model_id = "CompVis/stable-diffusion-v1-4"
62
+ device = "cuda"
63
+ pipe = StableDiffusionPipeline.from_pretrained(model_id, torch_dtype=torch.float16)
64
+ pipe = pipe.to(device)
65
+ prompt = f"blog banner about {topic}"
66
+ image = pipe(prompt).images[0]
67
+ return image
68
 
 
 
 
 
 
 
 
69
 
70
+ # Streamlit app
71
+ st.title("Blog Generator")
 
 
 
 
 
 
 
 
 
72
 
73
+ # Input and button
74
+ topic = st.text_input("Enter Title for the blog")
75
+ button_clicked = st.button("Create blog!")
76
+ st.subheader(topic)
77
+ # Load existing ideas
78
+ existing_ideas = load_ideas()
79
+ st.sidebar.header("Previous Ideas:")
80
 
81
+ # Display existing ideas in the sidebar
82
+ keys = list(set([key for idea in existing_ideas for key in idea.keys()]))
83
+ if topic in keys:
84
+ index = keys.index(topic)
85
+ selected_idea = st.sidebar.selectbox("Select Idea", keys, key="selectbox", index=index)
86
+ # Display content for the selected idea
87
+ selected_idea_from_list = next((idea for idea in existing_ideas if selected_idea in idea), None)
88
+ st.markdown(selected_idea_from_list[selected_idea])
89
+ else:
90
+ index = 0
91
  # Handle button click
92
  if button_clicked:
93
  # Generate content and update existing ideas
94
+ image = generate_image(topic)
95
  st.image(image)
96
  content = generate_content(topic)
97
  existing_ideas.append({topic: content})