Spaces:
Mooo-kf
/

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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +25 -16
app.py CHANGED
@@ -20,9 +20,13 @@ def save_ideas(ideas):
20
  with open("ideas.json", "w") as file:
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(
28
  input_variables=['keyword'],
@@ -33,6 +37,16 @@ def generate_content(topic):
33
  """
34
  )
35
  hub_chain = LLMChain(prompt=prompt, llm=hub_llm, verbose=True)
 
 
 
 
 
 
 
 
 
 
36
  content = hub_chain.run(topic)
37
 
38
  subheadings = [
@@ -58,10 +72,6 @@ def generate_content(topic):
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
@@ -82,22 +92,21 @@ st.sidebar.header("Previous Ideas:")
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})
98
  save_ideas(existing_ideas)
99
- st.experimental_rerun()
100
  # Update keys and selected idea in the sidebar
101
  keys = list(set([key for idea in existing_ideas for key in idea.keys()]))
102
- selected_idea = st.sidebar.selectbox("Select Idea", keys, key="selectbox", index=keys.index(topic))
103
  st.markdown(content)
 
 
20
  with open("ideas.json", "w") as file:
21
  json.dump(ideas, file)
22
 
23
+ # Create the pipeline and langchain model
24
+ def load_models():
25
+ with torch.no_grad():
26
+ model_id = "CompVis/stable-diffusion-v1-4"
27
+ device = "cuda"
28
+ pipe = StableDiffusionPipeline.from_pretrained(model_id, torch_dtype=torch.float16)
29
+ pipe = pipe.to(device)
30
  hub_llm = HuggingFaceHub(repo_id="HuggingFaceH4/zephyr-7b-beta")
31
  prompt = PromptTemplate(
32
  input_variables=['keyword'],
 
37
  """
38
  )
39
  hub_chain = LLMChain(prompt=prompt, llm=hub_llm, verbose=True)
40
+ return hub_chain,pipe
41
+
42
+ # Wait for the models to be created
43
+ with st.spinner("Creating generation pipelines. Please Wait:)..."):
44
+ hub_chain,pipe = load_models()
45
+
46
+
47
+ # Function to generate content
48
+ @torch.no_grad()
49
+ def generate_content(topic):
50
  content = hub_chain.run(topic)
51
 
52
  subheadings = [
 
72
  # generate image
73
  @torch.no_grad()
74
  def generate_image(topic):
 
 
 
 
75
  prompt = f"blog banner about {topic}"
76
  image = pipe(prompt).images[0]
77
  return image
 
92
  keys = list(set([key for idea in existing_ideas for key in idea.keys()]))
93
  if topic in keys:
94
  index = keys.index(topic)
95
+ selected_idea = st.sidebar.selectbox("Select Idea", keys, key=f"selectbox{topic}", index=index)
96
+ # Display content and image for the selected idea
97
  selected_idea_from_list = next((idea for idea in existing_ideas if selected_idea in idea), None)
98
+ st.markdown(selected_idea_from_list[selected_idea]["content"])
99
+ st.image(selected_idea_from_list[selected_idea]["image_path"])
100
  else:
101
  index = 0
102
  # Handle button click
103
  if button_clicked:
104
  # Generate content and update existing ideas
105
+ content, image_path = generate_content(topic),generate_image(topic)
106
+ existing_ideas.append({topic: {"content": content, "image_path": image_path}})
 
 
107
  save_ideas(existing_ideas)
 
108
  # Update keys and selected idea in the sidebar
109
  keys = list(set([key for idea in existing_ideas for key in idea.keys()]))
110
+ selected_idea = st.sidebar.selectbox("Select Idea", keys, key=f"selectbox{topic}", index=keys.index(topic))
111
  st.markdown(content)
112
+ st.image(image_path)