Tulsigida commited on
Commit
fc7e66d
·
1 Parent(s): dd3c060

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +37 -45
app.py CHANGED
@@ -1,56 +1,48 @@
1
- # Libraries for building GUI
2
- import tkinter as tk
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 ImageTk
12
 
13
- # private modules
14
  from authtoken import auth_token
15
 
 
 
 
 
 
16
 
17
- # Create app user interface
18
- app = tk.Tk()
19
- app.geometry("532x632")
20
- app.title("Text to Image app")
21
- app.configure(bg='black')
22
- ctk.set_appearance_mode("dark")
23
 
24
- # Create input box on the user interface
25
- prompt = ctk.CTkEntry(height=40, width=512, text_font=("Arial", 15), text_color="white", fg_color="black")
26
- prompt.place(x=10, y=10)
27
 
28
  # Create a placeholder to show the generated image
29
- img_placeholder = ctk.CTkLabel(height=512, width=512, text="")
30
- img_placeholder.place(x=10, y=110)
31
-
32
- # Download stable diffusion model from hugging face
33
- modelid = "CompVis/stable-diffusion-v1-4"
34
- device = "cuda"
35
- stable_diffusion_model = StableDiffusionPipeline.from_pretrained(modelid, revision="fp16", torch_dtype=torch.float16, use_auth_token=auth_token)
36
- stable_diffusion_model.to(device)
37
-
38
- # Generate image from test
39
- def generate():
40
- """ This function generate image from a text with stable diffusion"""
41
- with autocast(device):
42
- image = stable_diffusion_model(prompt.get(), guidance_scale=8.5)["sample"][0]
43
-
44
- # Save the generated image
45
- image.save('generatedimage.png')
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()