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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +25 -23
app.py CHANGED
@@ -3,6 +3,7 @@ import torch
3
  from langchain.chains import LLMChain
4
  from langchain.llms import HuggingFaceHub
5
  from langchain.prompts import PromptTemplate
 
6
  import streamlit as st
7
  import json
8
 
@@ -20,13 +21,9 @@ def save_ideas(ideas):
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,16 +34,6 @@ def load_models():
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 = [
@@ -70,12 +57,25 @@ def generate_content(topic):
70
 
71
 
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
 
 
 
 
 
 
 
78
 
 
 
79
 
80
  # Streamlit app
81
  st.title("Blog Generator")
@@ -102,11 +102,13 @@ else:
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)
 
3
  from langchain.chains import LLMChain
4
  from langchain.llms import HuggingFaceHub
5
  from langchain.prompts import PromptTemplate
6
+ import requests
7
  import streamlit as st
8
  import json
9
 
 
21
  with open("ideas.json", "w") as file:
22
  json.dump(ideas, file)
23
 
24
+ # Function to generate content
25
+ @torch.no_grad()
26
+ def generate_content(topic):
 
 
 
 
27
  hub_llm = HuggingFaceHub(repo_id="HuggingFaceH4/zephyr-7b-beta")
28
  prompt = PromptTemplate(
29
  input_variables=['keyword'],
 
34
  """
35
  )
36
  hub_chain = LLMChain(prompt=prompt, llm=hub_llm, verbose=True)
 
 
 
 
 
 
 
 
 
 
37
  content = hub_chain.run(topic)
38
 
39
  subheadings = [
 
57
 
58
 
59
  # generate image
60
+
61
+ import io
62
+ from PIL import Image
63
+ # Function to generate an image using the pre-created or newly created pipeline
64
  @torch.no_grad()
65
  def generate_image(topic):
66
+ API_URL = "https://api-inference.huggingface.co/models/runwayml/stable-diffusion-v1-5"
67
+ headers = {"Authorization": "Bearer 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"}
68
+
69
+ def query(payload):
70
+ response = requests.post(API_URL, headers=headers, json=payload)
71
+ return response.content
72
+ image_bytes = query({
73
+ "inputs": f"A blog banner about {topic}",
74
+ })
75
+ # You can access the image with PIL.Image for example
76
 
77
+ image = Image.open(io.BytesIO(image_bytes))
78
+ return image
79
 
80
  # Streamlit app
81
  st.title("Blog Generator")
 
102
  # Handle button click
103
  if button_clicked:
104
  # Generate content and update existing ideas
105
+ content, image = generate_content(topic),generate_image(topic)
106
+ if image:
107
+ image_path = f"{topic}.png"
108
  existing_ideas.append({topic: {"content": content, "image_path": image_path}})
109
  save_ideas(existing_ideas)
110
  # Update keys and selected idea in the sidebar
111
  keys = list(set([key for idea in existing_ideas for key in idea.keys()]))
112
  selected_idea = st.sidebar.selectbox("Select Idea", keys, key=f"selectbox{topic}", index=keys.index(topic))
113
+ st.image(image)
114
+ st.markdown(content)