Taizun commited on
Commit
1ec76d9
·
verified ·
1 Parent(s): 05811a6

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +32 -94
app.py CHANGED
@@ -1,104 +1,43 @@
1
- import torch
2
  import streamlit as st
3
- from diffusers import DiffusionPipeline
 
4
 
5
- # Cache resource to load the pipeline
6
  @st.cache_resource
7
- def load_pipeline():
 
8
  device = "cuda" if torch.cuda.is_available() else "cpu"
9
- pipe = DiffusionPipeline.from_pretrained("stabilityai/stable-diffusion-xl-base-1.0",
10
- torch_dtype=torch.float16 if device == "cuda" else torch.float32,
11
- use_safetensors=True,
12
- variant="fp16" if device == "cuda" else None)
13
- return pipe
14
 
15
- # Image generation function
16
- def image_generation(pipe, prompt, negative_prompt):
17
  try:
18
- image = pipe(
19
- prompt=prompt,
20
- negative_prompt="blurred, ugly, watermark, low resolution" + negative_prompt,
21
- num_inference_steps=12,
22
- guidance_scale=9.0
23
- ).images[0]
24
- return image
25
  except Exception as e:
26
  st.error(f"Error generating image: {str(e)}")
27
  return None
28
 
29
- # Define the table as a list of dictionaries with more artistic styles
30
- table = [
31
- {
32
- "name": "sai-neonpunk",
33
- "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",
34
- "negative_prompt": "painting, drawing, illustration, glitch, deformed, mutated, cross-eyed, ugly, disfigured"
35
- },
36
- {
37
- "name": "futuristic-retro cyberpunk",
38
- "prompt": "retro cyberpunk. 80's inspired, synthwave, neon, vibrant, detailed, retro futurism",
39
- "negative_prompt": "modern, desaturated, black and white, realism, low contrast"
40
- },
41
- {
42
- "name": "Dark Fantasy",
43
- "prompt": "Dark Fantasy Art, dark, moody, dark fantasy style",
44
- "negative_prompt": "ugly, deformed, noisy, blurry, low contrast, bright, sunny"
45
- },
46
- {
47
- "name": "Double Exposure",
48
- "prompt": "Double Exposure Style, double image ghost effect, image combination, double exposure style",
49
- "negative_prompt": "ugly, deformed, noisy, blurry, low contrast"
50
- },
51
- {
52
- "name": "Vintage Retro",
53
- "prompt": "vintage retro style, old-school, warm colors, nostalgic, retro textures, faded colors, cozy atmosphere, worn, classic film",
54
- "negative_prompt": "modern, bright, high definition, sharp"
55
- },
56
- {
57
- "name": "Sci-Fi Futuristic",
58
- "prompt": "Sci-Fi futuristic, cybernetic, sleek metallic designs, neon, artificial intelligence, high-tech, future cities, robots, alien landscapes",
59
- "negative_prompt": "low-tech, blurry, old-fashioned, dull, dark"
60
- },
61
- {
62
- "name": "Surrealism",
63
- "prompt": "Surrealism, dreamlike, abstract, bizarre, imaginative, fantastical, magical realism, melting objects, floating landscapes",
64
- "negative_prompt": "realistic, plain, dull, conventional"
65
- },
66
- {
67
- "name": "Pop Art",
68
- "prompt": "Pop Art, bold colors, comic book style, strong lines, graphic, playful, contemporary, inspired by Andy Warhol and Roy Lichtenstein",
69
- "negative_prompt": "minimalistic, muted colors, traditional, abstract"
70
- },
71
- {
72
- "name": "Gothic Horror",
73
- "prompt": "Gothic horror, dark, eerie, vintage horror, Victorian architecture, spooky ambiance, haunted, gothic castles, fog, grim landscapes",
74
- "negative_prompt": "light, cheerful, happy, bright"
75
- },
76
- {
77
- "name": "Minimalism",
78
- "prompt": "Minimalism, clean lines, simple forms, neutral tones, white space, simple color palette, elegant, modern design",
79
- "negative_prompt": "cluttered, bright, complex, busy"
80
- },
81
- {
82
- "name": "Steampunk",
83
- "prompt": "Steampunk, Victorian industrial, brass gears, steam-powered machinery, fantasy airships, retro-futuristic, adventure, mechanical details",
84
- "negative_prompt": "futuristic, modern, minimalistic, digital"
85
- }
86
- ]
87
-
88
- # Convert to dictionary for easy lookup
89
- styles_dict = {entry["name"]: entry for entry in table}
90
-
91
- # Streamlit UI
92
- st.title("Image Generation")
93
-
94
- # Display credit after the title in smaller font
95
- st.markdown("<h3 style='font-size: 18px;'>Made by Taizun</h3>", unsafe_allow_html=True)
96
 
97
- # User input for the custom prompt
98
- prompt = st.text_input("Enter your Prompt")
99
 
100
- # Load the pre-trained model
101
- pipeline = load_pipeline()
 
 
 
 
 
102
 
103
  # Dropdown for selecting a style
104
  style_name = st.selectbox("Select a Style", options=list(styles_dict.keys()))
@@ -109,9 +48,8 @@ if style_name:
109
  selected_style_prompt = selected_entry["prompt"]
110
  selected_style_negative_prompt = selected_entry["negative_prompt"]
111
 
112
- # Button to trigger image generation
113
  if st.button("Generate Image"):
114
- with st.spinner("Generating your awesome image..."):
115
- image = image_generation(pipeline, prompt + selected_style_prompt, selected_style_negative_prompt)
116
- if image:
117
- st.image(image)
 
 
1
  import streamlit as st
2
+ import torch
3
+ from transformers import CLIPProcessor, CLIPTextModel # Using CLIP as an example of a lighter model for image generation
4
 
 
5
  @st.cache_resource
6
+ def load_lightweight_model():
7
+ """Load a lightweight image generation model."""
8
  device = "cuda" if torch.cuda.is_available() else "cpu"
9
+ model_name = "CompVis/ldm-text2im-large-256" # A known lightweight diffusion model
10
+ model = CLIPTextModel.from_pretrained(model_name).to(device)
11
+ processor = CLIPProcessor.from_pretrained(model_name)
12
+ return model, processor
 
13
 
14
+ def generate_image(model, processor, prompt, negative_prompt):
15
+ """Generate an image using the lightweight model."""
16
  try:
17
+ inputs = processor(text=prompt, return_tensors="pt").to(model.device)
18
+ outputs = model.generate(inputs['input_ids'], max_length=50)
19
+ # Here we would simulate or replace this with the actual image output from the lightweight model
20
+ dummy_image = "https://via.placeholder.com/256" # Placeholder image link for demonstration
21
+ return dummy_image
 
 
22
  except Exception as e:
23
  st.error(f"Error generating image: {str(e)}")
24
  return None
25
 
26
+ # Streamlit app setup
27
+ st.title("Image Generator")
28
+ st.markdown("### By Taizun", unsafe_allow_html=True)
29
+ st.markdown('<small>Effortlessly create stunning images with our lightweight generator.</small>', unsafe_allow_html=True)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
30
 
31
+ prompt = st.text_input("Enter your prompt")
32
+ model, processor = load_lightweight_model()
33
 
34
+ # Style table (Example styles)
35
+ styles_dict = {
36
+ "Neon Vibes": {"prompt": "neon lights", "negative_prompt": "blurry, low quality"},
37
+ "Retro Sci-Fi": {"prompt": "retro futuristic scene", "negative_prompt": "modern, dull"},
38
+ "Mystic Forest": {"prompt": "dark forest with mystical lights", "negative_prompt": "bright, plain"},
39
+ "Abstract Art": {"prompt": "abstract shapes and colors", "negative_prompt": "realistic, boring"},
40
+ }
41
 
42
  # Dropdown for selecting a style
43
  style_name = st.selectbox("Select a Style", options=list(styles_dict.keys()))
 
48
  selected_style_prompt = selected_entry["prompt"]
49
  selected_style_negative_prompt = selected_entry["negative_prompt"]
50
 
 
51
  if st.button("Generate Image"):
52
+ with st.spinner("Generating your image..."):
53
+ result = generate_image(model, processor, prompt + " " + selected_style_prompt, selected_style_negative_prompt)
54
+ if result:
55
+ st.image(result, caption="Generated Image", use_column_width=True)