jn1xia commited on
Commit
b4d9a27
·
verified ·
1 Parent(s): 4f93e85

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +56 -29
app.py CHANGED
@@ -1,65 +1,91 @@
 
1
  import streamlit as st
2
  from PIL import Image
3
  import requests
4
  from io import BytesIO
5
  # import tensorflow as tf
6
-
 
 
 
 
 
 
 
 
 
 
 
 
7
  # Function to display an example image
8
  def display_example_image(url):
9
  response = requests.get(url)
10
  img = Image.open(BytesIO(response.content))
11
  st.image(img, caption='Generated Image', use_column_width=True)
12
-
 
 
 
 
 
 
 
 
 
13
  # Placeholder function for generating images (replace this with your actual generative AI code)
14
  def generate_images(prompt, num_images=3):
15
  # This is a placeholder function. Replace it with your actual image generation code.
16
  # Here, we are just returning the same example image multiple times for demonstration.
17
- image_url = "https://wpmedia.roomsketcher.com/content/uploads/2022/01/06145940/What-is-a-floor-plan-with-dimensions.png" # Replace with a valid image URL
18
  response = requests.get(image_url)
19
  img = Image.open(BytesIO(response.content))
 
20
  return [img] * num_images
21
-
22
- name = "Ilham"
23
  title_center = """
24
  <style>
25
  .title{
26
  text-align: center
27
  }
28
  </style>
29
-
30
  """
31
  # Title of the app
32
  st.markdown(title_center, unsafe_allow_html=True)
33
-
34
  title_container = """
35
  <h1 class="title">AutoFloor</h1>
36
  """
37
-
38
  st.markdown(title_container, unsafe_allow_html=True)
39
  # Text input for user prompt
40
  user_prompt = st.text_input("Enter your prompt here:")
41
-
42
  # file= st.file_uploader ("Unggah file Gambar", type = ["jpg", "png"])
43
-
44
  # model = tf.keras.models.load_model('L00005_HL512_bagus.h5')
45
-
46
  st.markdown("""
47
  <style>.element-container:has(#button-after) + div button {
48
  margin: 0 auto;
49
  display: block;
50
  }</style>""", unsafe_allow_html=True)
51
-
52
-
53
  st.markdown('<span id="button-after"></span>', unsafe_allow_html=True)
54
  # Generate and display images in a 3x3 grid
55
  if st.button('Generate Images', type="primary"):
56
  if user_prompt:
57
  st.write(f"Prompt: {user_prompt}")
58
- image_url = "https://wpmedia.roomsketcher.com/content/uploads/2022/01/06145940/What-is-a-floor-plan-with-dimensions.png" # Replace with a valid image URL
59
  # Generate images based on the user's prompt
60
- generated_images = generate_images(user_prompt, num_images=3)
61
-
62
-
 
 
 
63
  html_code = f"""
64
  <!DOCTYPE html>
65
  <html lang="en">
@@ -76,7 +102,7 @@ if st.button('Generate Images', type="primary"):
76
  </head>
77
  <body>
78
  <div id="image-container" style="text-align: center;">
79
- <img id="zoomable-image" class="zoomable-image" src="{image_url}" alt="Zoomable Image" style="max-width: 100%; height: auto;">
80
  </div>
81
  <script>
82
  document.addEventListener('DOMContentLoaded', (event) => {{
@@ -87,7 +113,7 @@ if st.button('Generate Images', type="primary"):
87
  bounds: false,
88
  boundsPadding: 0.1
89
  }});
90
-
91
  image.addEventListener('click', () => {{
92
  const currentTransform = image.style.transform;
93
  if (currentTransform.includes('matrix')) {{
@@ -96,38 +122,38 @@ if st.button('Generate Images', type="primary"):
96
  panzoomInstance.zoomAbs(image.width / 2, image.height / 2, 3);
97
  }}
98
  }});
99
-
100
  image.addEventListener('dblclick', () => {{
101
  const xys = panzoomInstance.getTransform()
102
- if(xys.scale > 1) {{
103
  const fScale = 1 - xys.scale
104
  const fixeX = xys.x / fScale
105
  const fixeY = xys.y / fScale
106
  panzoomInstance.smoothZoomAbs(fixeX, fixeY, 1)
107
- }} else {{
108
  panzoomInstance.moveBy(-xys.x, -xys.y, true)
109
  panzoomInstance.smoothZoomAbs(xys.x, xys.y, 1)
110
  }}
111
- panzoomInstance.moveTo(0, 0)
112
- panzoomInstance.zoomAbs(0, 0, 1)
113
  }});
114
  }});
115
  </script>
116
  </body>
117
  </html>
118
  """
119
-
120
  # # Embed the HTML and JavaScript into the Streamlit app
121
  # st.components.v1.html(html_code, height=300)
122
-
123
-
124
  # Display images in a 3x3 grid
125
  cols = st.columns(3)
126
  for i in range(3):
127
  # for j in range(3):
128
  # with cols[j]:
129
  # st.image(generated_images[i * 3 + j], use_column_width=True)
130
-
131
  # Display the image with zoom effect
132
  # container_style = """
133
  # <div class="container">
@@ -137,4 +163,5 @@ if st.button('Generate Images', type="primary"):
137
  # st.markdown(container_style, unsafe_allow_html=True)
138
  st.components.v1.html(html_code, height=600)
139
  else:
140
- st.write("Please enter a prompt.")
 
 
1
+ [3:45 PM] Widyanto, Achmad
2
  import streamlit as st
3
  from PIL import Image
4
  import requests
5
  from io import BytesIO
6
  # import tensorflow as tf
7
+
8
+ import streamlit as st
9
+ from diffusers import StableDiffusionPipeline
10
+ import torch
11
+
12
+ #model_id = "CompVis/stable-diffusion-v1-4"
13
+ #pipe = StableDiffusionPipeline.from_pretrained(model_id)
14
+
15
+ # Ensure the model is using the CPU
16
+ #pipe = pipe.to("cpu")
17
+
18
+ image_html = ""
19
+
20
  # Function to display an example image
21
  def display_example_image(url):
22
  response = requests.get(url)
23
  img = Image.open(BytesIO(response.content))
24
  st.image(img, caption='Generated Image', use_column_width=True)
25
+
26
+ #function to generate AI based images using Huggingface Diffusers
27
+ def generate_images_using_huggingface_diffusers(text):
28
+ # pipe = StableDiffusionPipeline.from_pretrained("sd-dreambooth-library/cat-toy", torch_dtype=torch.float16)
29
+ pipe = StableDiffusionPipeline.from_pretrained("CompVis/stable-diffusion-v1-4", torch_dtype=torch.float32)
30
+ pipe = pipe.to("cpu")
31
+ prompt = text
32
+ image = pipe(prompt).images[0]
33
+ return image
34
+
35
  # Placeholder function for generating images (replace this with your actual generative AI code)
36
  def generate_images(prompt, num_images=3):
37
  # This is a placeholder function. Replace it with your actual image generation code.
38
  # Here, we are just returning the same example image multiple times for demonstration.
39
+ image_url = pipe(prompt, num_images_per_prompt=num_samples, num_inference_steps=50, guidance_scale=7.5).images # Replace with a valid image URL
40
  response = requests.get(image_url)
41
  img = Image.open(BytesIO(response.content))
42
+ image_html = image_url
43
  return [img] * num_images
44
+
45
+
46
  title_center = """
47
  <style>
48
  .title{
49
  text-align: center
50
  }
51
  </style>
52
+
53
  """
54
  # Title of the app
55
  st.markdown(title_center, unsafe_allow_html=True)
56
+
57
  title_container = """
58
  <h1 class="title">AutoFloor</h1>
59
  """
60
+
61
  st.markdown(title_container, unsafe_allow_html=True)
62
  # Text input for user prompt
63
  user_prompt = st.text_input("Enter your prompt here:")
64
+
65
  # file= st.file_uploader ("Unggah file Gambar", type = ["jpg", "png"])
66
+
67
  # model = tf.keras.models.load_model('L00005_HL512_bagus.h5')
68
+
69
  st.markdown("""
70
  <style>.element-container:has(#button-after) + div button {
71
  margin: 0 auto;
72
  display: block;
73
  }</style>""", unsafe_allow_html=True)
74
+
75
+
76
  st.markdown('<span id="button-after"></span>', unsafe_allow_html=True)
77
  # Generate and display images in a 3x3 grid
78
  if st.button('Generate Images', type="primary"):
79
  if user_prompt:
80
  st.write(f"Prompt: {user_prompt}")
81
+ #image_url = "https://wpmedia.roomsketcher.com/content/uploads/2022/01/06145940/What-is-a-floor-plan-with-dimensions.png" # Replace with a valid image URL
82
  # Generate images based on the user's prompt
83
+ #generated_images = generate_images(user_prompt, num_images=3)
84
+ image_output = generate_images_using_huggingface_diffusers(user_prompt)
85
+ st.info("Generating image.....")
86
+ st.success("Image Generated Successfully")
87
+ st.image(image_output, caption="Generated by Huggingface Diffusers")
88
+
89
  html_code = f"""
90
  <!DOCTYPE html>
91
  <html lang="en">
 
102
  </head>
103
  <body>
104
  <div id="image-container" style="text-align: center;">
105
+ <img id="zoomable-image" class="zoomable-image" src="{{image_url}}" alt="Zoomable Image" style="max-width: 100%; height: auto;">
106
  </div>
107
  <script>
108
  document.addEventListener('DOMContentLoaded', (event) => {{
 
113
  bounds: false,
114
  boundsPadding: 0.1
115
  }});
116
+
117
  image.addEventListener('click', () => {{
118
  const currentTransform = image.style.transform;
119
  if (currentTransform.includes('matrix')) {{
 
122
  panzoomInstance.zoomAbs(image.width / 2, image.height / 2, 3);
123
  }}
124
  }});
125
+
126
  image.addEventListener('dblclick', () => {{
127
  const xys = panzoomInstance.getTransform()
128
+ if(xys.scale > 1) {{
129
  const fScale = 1 - xys.scale
130
  const fixeX = xys.x / fScale
131
  const fixeY = xys.y / fScale
132
  panzoomInstance.smoothZoomAbs(fixeX, fixeY, 1)
133
+ }} else {{
134
  panzoomInstance.moveBy(-xys.x, -xys.y, true)
135
  panzoomInstance.smoothZoomAbs(xys.x, xys.y, 1)
136
  }}
137
+ panzoomInstance.moveTo(0, 0)
138
+ panzoomInstance.zoomAbs(0, 0, 1)
139
  }});
140
  }});
141
  </script>
142
  </body>
143
  </html>
144
  """
145
+
146
  # # Embed the HTML and JavaScript into the Streamlit app
147
  # st.components.v1.html(html_code, height=300)
148
+
149
+
150
  # Display images in a 3x3 grid
151
  cols = st.columns(3)
152
  for i in range(3):
153
  # for j in range(3):
154
  # with cols[j]:
155
  # st.image(generated_images[i * 3 + j], use_column_width=True)
156
+
157
  # Display the image with zoom effect
158
  # container_style = """
159
  # <div class="container">
 
163
  # st.markdown(container_style, unsafe_allow_html=True)
164
  st.components.v1.html(html_code, height=600)
165
  else:
166
+ st.write("Please enter a prompt.")
167
+