|
|
import streamlit as st |
|
|
from PIL import Image |
|
|
import requests |
|
|
from io import BytesIO |
|
|
|
|
|
|
|
|
import streamlit as st |
|
|
from diffusers import StableDiffusionPipeline |
|
|
from diffusers import DiffusionPipeline |
|
|
import torch |
|
|
from accelerate import Accelerator |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
image_html = "" |
|
|
accelerator = Accelerator() |
|
|
|
|
|
|
|
|
def display_example_image(url): |
|
|
response = requests.get(url) |
|
|
img = Image.open(BytesIO(response.content)) |
|
|
st.image(img, caption='Generated Image', use_column_width=True) |
|
|
|
|
|
|
|
|
def generate_images_using_huggingface_diffusers(text): |
|
|
|
|
|
from diffusers import DiffusionPipeline |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
pipe = DiffusionPipeline.from_pretrained("CompVis/stable-diffusion-v1-4") |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
pipe = pipe.to(accelerator.device) |
|
|
prompt = text |
|
|
image = pipe(prompt,num_images_per_prompt=3) |
|
|
return image |
|
|
|
|
|
|
|
|
def generate_images(prompt, num_images=3): |
|
|
|
|
|
|
|
|
image_url = pipe(prompt, num_images_per_prompt=num_samples, num_inference_steps=50, guidance_scale=7.5).images |
|
|
response = requests.get(image_url) |
|
|
img = Image.open(BytesIO(response.content)) |
|
|
image_html = image_url |
|
|
return [img] * num_images |
|
|
|
|
|
|
|
|
title_center = """ |
|
|
<style> |
|
|
.title{ |
|
|
text-align: center |
|
|
} |
|
|
</style> |
|
|
|
|
|
""" |
|
|
|
|
|
st.markdown(title_center, unsafe_allow_html=True) |
|
|
|
|
|
title_container = """ |
|
|
<h1 class="title">AutoFloor</h1> |
|
|
""" |
|
|
|
|
|
st.markdown(title_container, unsafe_allow_html=True) |
|
|
|
|
|
user_prompt = st.text_input("Enter your prompt here:") |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
st.markdown(""" |
|
|
<style>.element-container:has(#button-after) + div button { |
|
|
margin: 0 auto; |
|
|
display: block; |
|
|
}</style>""", unsafe_allow_html=True) |
|
|
|
|
|
|
|
|
st.markdown('<span id="button-after"></span>', unsafe_allow_html=True) |
|
|
|
|
|
if st.button('Generate Images', type="primary"): |
|
|
if user_prompt: |
|
|
st.write(f"Prompt: {user_prompt}") |
|
|
|
|
|
|
|
|
|
|
|
image_output = generate_images_using_huggingface_diffusers(user_prompt) |
|
|
st.info("Generating image.....") |
|
|
st.success("Image Generated Successfully") |
|
|
st.image(image_output, caption="Generated by Huggingface Diffusers") |
|
|
|
|
|
html_code = f""" |
|
|
<!DOCTYPE html> |
|
|
<html lang="en"> |
|
|
<head> |
|
|
<meta charset="UTF-8"> |
|
|
<meta name="viewport" content="width=device-width, initial-scale=1.0"> |
|
|
<style> |
|
|
.zoomable-image {{ |
|
|
transition: transform 0.3s ease; |
|
|
cursor: pointer; |
|
|
}} |
|
|
</style> |
|
|
<script src="https://unpkg.com/[email protected]/dist/panzoom.min.js"></script> |
|
|
</head> |
|
|
<body> |
|
|
<div id="image-container" style="text-align: center;"> |
|
|
<img id="zoomable-image" class="zoomable-image" src="{{image_url}}" alt="Zoomable Image" style="max-width: 100%; height: auto;"> |
|
|
</div> |
|
|
<script> |
|
|
document.addEventListener('DOMContentLoaded', (event) => {{ |
|
|
const image = document.getElementById('zoomable-image'); |
|
|
const panzoomInstance = panzoom(image, {{ |
|
|
maxZoom: 3, |
|
|
minZoom: 1, |
|
|
bounds: false, |
|
|
boundsPadding: 0.1 |
|
|
}}); |
|
|
|
|
|
image.addEventListener('click', () => {{ |
|
|
const currentTransform = image.style.transform; |
|
|
if (currentTransform.includes('matrix')) {{ |
|
|
panzoomInstance.zoomAbs(0, 0, 1); |
|
|
}} else {{ |
|
|
panzoomInstance.zoomAbs(image.width / 2, image.height / 2, 3); |
|
|
}} |
|
|
}}); |
|
|
|
|
|
image.addEventListener('dblclick', () => {{ |
|
|
const xys = panzoomInstance.getTransform() |
|
|
if(xys.scale > 1) {{ |
|
|
const fScale = 1 - xys.scale |
|
|
const fixeX = xys.x / fScale |
|
|
const fixeY = xys.y / fScale |
|
|
panzoomInstance.smoothZoomAbs(fixeX, fixeY, 1) |
|
|
}} else {{ |
|
|
panzoomInstance.moveBy(-xys.x, -xys.y, true) |
|
|
panzoomInstance.smoothZoomAbs(xys.x, xys.y, 1) |
|
|
}} |
|
|
panzoomInstance.moveTo(0, 0) |
|
|
panzoomInstance.zoomAbs(0, 0, 1) |
|
|
}}); |
|
|
}}); |
|
|
</script> |
|
|
</body> |
|
|
</html> |
|
|
""" |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
cols = st.columns(3) |
|
|
for i in range(3): |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
st.components.v1.html(html_code, height=600) |
|
|
else: |
|
|
st.write("Please enter a prompt.") |
|
|
|