GenAILearniverse commited on
Commit
2617c83
1 Parent(s): a89cbdf

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +93 -0
app.py ADDED
@@ -0,0 +1,93 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from diffusers import DiffusionPipeline
2
+ import torch
3
+ import streamlit as st
4
+
5
+ # pipe = DiffusionPipeline.from_pretrained("stabilityai/stable-diffusion-xl-base-1.0")
6
+ # pipe = DiffusionPipeline.from_pretrained("stabilityai/stable-diffusion-xl-refiner-1.0")
7
+
8
+ # sdxl_base_model_path = ("../Models/models--stabilityai--stable-diffusion-xl-base-1.0/snapshots"
9
+ # "/462165984030d82259a11f4367a4eed129e94a7b")
10
+ #
11
+ # sdxl_refiner_model_path = ("../Models/models--stabilityai--stable-diffusion-xl-refiner-1.0/snapshots/"
12
+ # "5d4cfe854c9a9a87939ff3653551c2b3c99a4356")
13
+ @st.cache_resource
14
+ def load_pipeline():
15
+
16
+ # pipe = DiffusionPipeline.from_pretrained("stabilityai/stable-diffusion-xl-refiner-1.0",
17
+ # torch_dtype=torch.float16 if device == "cuda" else torch.float32,
18
+ # use_safetensors=True,
19
+ # variant="fp16" if device =="cuda" else None)
20
+ device = "cuda" if torch.cuda.is_available() else "cpu"
21
+ pipe = DiffusionPipeline.from_pretrained("stabilityai/stable-diffusion-xl-base-1.0",
22
+ torch_dtype=torch.float16 if device == "cuda" else torch.float32,
23
+ use_safetensors=True,
24
+ variant="fp16" if device == "cuda" else None)
25
+ # if device == "cuda":
26
+ # pipe.to(device)
27
+ # else:
28
+ # pipe.enable_model_cpu_offload()
29
+ return pipe
30
+
31
+ def image_generation(pipe, prompt, negative_prompt):
32
+ try:
33
+ image = pipe(
34
+ prompt = prompt,
35
+ negative_prompt = "blurred, ugly, watermark, low resolution" + negative_prompt,
36
+ num_inference_steps= 20,
37
+ guidance_scale=9.0
38
+ ).images[0]
39
+ return image
40
+ except Exception as e:
41
+ st.error(f"Error generating image: {str(e)}")
42
+ return None
43
+
44
+
45
+ import streamlit as st
46
+
47
+ # Define the table as a list of dictionaries with the provided data
48
+ table = [
49
+ {
50
+ "name": "sai-neonpunk",
51
+ "prompt": "neonpunk style . cyberpunk, vaporwave, neon, vibes, vibrant, stunningly beautiful, crisp, detailed, sleek, ultramodern, magenta highlights, dark purple shadows, high contrast, cinematic, ultra detailed, intricate, professional",
52
+ "negative_prompt": "painting, drawing, illustration, glitch, deformed, mutated, cross-eyed, ugly, disfigured"
53
+ },
54
+ {
55
+ "name": "futuristic-retro cyberpunk",
56
+ "prompt": "retro cyberpunk. 80's inspired, synthwave, neon, vibrant, detailed, retro futurism",
57
+ "negative_prompt": "modern, desaturated, black and white, realism, low contrast"
58
+ },
59
+ {
60
+ "name": "Dark Fantasy",
61
+ "prompt": "Dark Fantasy Art, dark, moody, dark fantasy style",
62
+ "negative_prompt": "ugly, deformed, noisy, blurry, low contrast, bright, sunny"
63
+ },
64
+ {
65
+ "name": "Double Exposure",
66
+ "prompt": "Double Exposure Style, double image ghost effect, image combination, double exposure style",
67
+ "negative_prompt": "ugly, deformed, noisy, blurry, low contrast"
68
+ }
69
+ ]
70
+
71
+ # Convert the list of dictionaries to a dictionary with 'name' as key for easy lookup
72
+ styles_dict = {entry["name"]: entry for entry in table}
73
+
74
+
75
+
76
+
77
+ st.title("Application 11: @GenAiLearniverse Image Generation using SD XL")
78
+ prompt = st.text_input("Enter your Prompt", value="A futuristic superhero cat")
79
+
80
+ pipeline = load_pipeline()
81
+ # Dropdown for selecting a style
82
+ style_name = st.selectbox("Select a Style", options=list(styles_dict.keys()))
83
+
84
+ # Display the selected style's prompt and negative prompt
85
+ if style_name:
86
+ selected_entry = styles_dict[style_name]
87
+ selected_style_prompt = selected_entry["prompt"];
88
+ selected_style_negative_prompt = selected_entry["negative_prompt"]
89
+ if st.button("Generate Awesome Image"):
90
+ with st.spinner("Generating your awesome image..."):
91
+ image =image_generation(pipeline,prompt + selected_style_prompt, selected_style_negative_prompt)
92
+ if image:
93
+ st.image(image)