Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,56 +1,48 @@
|
|
| 1 |
-
#
|
| 2 |
-
import
|
| 3 |
-
import customtkinter as ctk
|
| 4 |
-
|
| 5 |
-
# Machine Learning libraries
|
| 6 |
import torch
|
| 7 |
from torch import autocast
|
| 8 |
from diffusers import StableDiffusionPipeline
|
| 9 |
|
| 10 |
-
# Libraries for processing image
|
| 11 |
-
from PIL import
|
| 12 |
|
| 13 |
-
#
|
| 14 |
from authtoken import auth_token
|
| 15 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
|
| 17 |
-
# Create
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
|
| 24 |
-
# Create input box on the user interface
|
| 25 |
-
|
| 26 |
-
prompt.
|
| 27 |
|
| 28 |
# Create a placeholder to show the generated image
|
| 29 |
-
img_placeholder =
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
# Display the generated image on the user interface
|
| 48 |
-
img = ImageTk.PhotoImage(image)
|
| 49 |
-
img_placeholder.configure(image=img)
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
trigger = ctk.CTkButton(height=40, width=120, text_font=("Arial", 15), text_color="black", fg_color="white", command=generate)
|
| 53 |
-
trigger.configure(text="Generate")
|
| 54 |
-
trigger.place(x=206, y=60)
|
| 55 |
-
|
| 56 |
-
app.mainloop()
|
|
|
|
| 1 |
+
# Streamlit and Machine Learning libraries
|
| 2 |
+
import streamlit as st
|
|
|
|
|
|
|
|
|
|
| 3 |
import torch
|
| 4 |
from torch import autocast
|
| 5 |
from diffusers import StableDiffusionPipeline
|
| 6 |
|
| 7 |
+
# Libraries for processing image
|
| 8 |
+
from PIL import Image
|
| 9 |
|
| 10 |
+
# Private modules
|
| 11 |
from authtoken import auth_token
|
| 12 |
|
| 13 |
+
# Download stable diffusion model from Hugging Face
|
| 14 |
+
modelid = "CompVis/stable-diffusion-v1-4"
|
| 15 |
+
device = "cuda"
|
| 16 |
+
stable_diffusion_model = StableDiffusionPipeline.from_pretrained(modelid, revision="fp16", torch_dtype=torch.float16, use_auth_token=auth_token)
|
| 17 |
+
stable_diffusion_model.to(device)
|
| 18 |
|
| 19 |
+
# Create a Streamlit app
|
| 20 |
+
st.set_page_config(
|
| 21 |
+
page_title="Text to Image App",
|
| 22 |
+
page_icon="🖼️",
|
| 23 |
+
layout="centered",
|
| 24 |
+
)
|
| 25 |
|
| 26 |
+
# Create input box on the user interface
|
| 27 |
+
st.write("# Text to Image app")
|
| 28 |
+
prompt = st.text_area("Enter your text here:", height=10, max_chars=200)
|
| 29 |
|
| 30 |
# Create a placeholder to show the generated image
|
| 31 |
+
img_placeholder = st.empty()
|
| 32 |
+
|
| 33 |
+
# Generate image from text
|
| 34 |
+
def generate_image():
|
| 35 |
+
if prompt:
|
| 36 |
+
st.write("Generating image...")
|
| 37 |
+
try:
|
| 38 |
+
with autocast(device):
|
| 39 |
+
image = stable_diffusion_model(prompt, guidance_scale=8.5)["sample"][0]
|
| 40 |
+
|
| 41 |
+
# Display the generated image on the user interface
|
| 42 |
+
st.image(image, caption="Generated Image", use_column_width=True)
|
| 43 |
+
except Exception as e:
|
| 44 |
+
st.error(f"Error generating the image: {str(e)}")
|
| 45 |
+
|
| 46 |
+
# Create a button to trigger image generation
|
| 47 |
+
if st.button("Generate Image"):
|
| 48 |
+
generate_image()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|