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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +27 -31
app.py CHANGED
@@ -1,55 +1,51 @@
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()))
44
 
45
- # Display the selected style's prompt and negative prompt
46
  if style_name:
47
- selected_entry = styles_dict[style_name]
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)
 
1
  import streamlit as st
2
+ from diffusers import StableDiffusionPipeline
3
  import torch
 
4
 
5
  @st.cache_resource
6
+ def load_lightweight_pipeline():
 
7
  device = "cuda" if torch.cuda.is_available() else "cpu"
8
+ pipeline = StableDiffusionPipeline.from_pretrained(
9
+ "CompVis/stable-diffusion-v1-4",
10
+ torch_dtype=torch.float16 if device == "cuda" else torch.float32
11
+ ).to(device)
12
+ return pipeline
13
 
14
+ def generate_image(pipeline, prompt, negative_prompt):
 
15
  try:
16
+ image = pipeline(
17
+ prompt=prompt,
18
+ negative_prompt=negative_prompt,
19
+ num_inference_steps=25,
20
+ guidance_scale=7.5
21
+ ).images[0]
22
+ return image
23
  except Exception as e:
24
  st.error(f"Error generating image: {str(e)}")
25
  return None
26
 
 
27
  st.title("Image Generator")
28
+ st.markdown('<small>By Taizun</small>', unsafe_allow_html=True)
 
29
 
30
  prompt = st.text_input("Enter your prompt")
31
+ pipeline = load_lightweight_pipeline()
32
 
 
33
  styles_dict = {
34
+ "Neon Vibes": {"prompt": "neon lights in a futuristic city", "negative_prompt": "blurred, dull"},
35
+ "Retro Sci-Fi": {"prompt": "retro futuristic landscape", "negative_prompt": "realistic, modern"},
36
+ "Mystic Forest": {"prompt": "dark mystical forest with glowing lights", "negative_prompt": "sunny, bright"},
37
+ "Abstract Art": {"prompt": "abstract colorful shapes", "negative_prompt": "realistic, detailed"}
38
  }
39
 
 
40
  style_name = st.selectbox("Select a Style", options=list(styles_dict.keys()))
41
 
 
42
  if style_name:
43
+ selected_style = styles_dict[style_name]
44
+ style_prompt = selected_style["prompt"]
45
+ negative_prompt = selected_style["negative_prompt"]
46
 
47
  if st.button("Generate Image"):
48
  with st.spinner("Generating your image..."):
49
+ image = generate_image(pipeline, prompt + " " + style_prompt, negative_prompt)
50
+ if image:
51
+ st.image(image, caption="Generated Image", use_column_width=True)