adi2606 commited on
Commit
76f48d2
β€’
1 Parent(s): 38f7b83

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +42 -29
app.py CHANGED
@@ -1,44 +1,57 @@
1
  import streamlit as st
2
- from diffusers import DiffusionPipeline
3
  import torch
4
- import os
 
 
 
 
 
 
 
5
 
6
- # Load Diffusion model
7
  @st.cache_resource
8
- def load_model():
9
- model_id = "xinsir/controlnet-union-sdxl-1.0"
10
- device = "cuda" if torch.cuda.is_available() else "cpu"
11
- pipe = DiffusionPipeline.from_pretrained(model_id, torch_dtype=torch.float16 if device == "cuda" else torch.float32)
 
 
 
 
 
 
12
  pipe.to(device)
13
  return pipe
14
 
15
- # Load the model
16
- pipe = load_model()
17
-
18
- # Ensure output directory exists
19
- output_dir = "./outputs_generated"
20
- os.makedirs(output_dir, exist_ok=True)
 
 
 
 
 
 
 
 
21
 
22
- # Load custom CSS
23
- with open("styles.css") as f:
24
- st.markdown(f"<style>{f.read()}</style>", unsafe_allow_html=True)
25
-
26
- # Streamlit app
27
  st.markdown("<div class='header'>✨ Generate Your Custom GitHub Profile Picture! ✨</div>", unsafe_allow_html=True)
28
- st.markdown("<div class='content'>Create an anime-style GitHub profile picture that reflects your personality and passion for coding. πŸš€πŸ‘¨β€πŸ’»</div>", unsafe_allow_html=True)
29
 
30
- # User input for the text prompt
31
- user_prompt = st.text_area("Describe your GitHub profile picture:",
32
  "Create an anime-style GitHub profile picture for a boy. The character should have a friendly and approachable expression, embodying a sense of curiosity and enthusiasm, reflecting the qualities of a passionate coder. Incorporate elements that suggest technology or coding, such as a pair of stylish glasses, a laptop, or a background with subtle code or tech motifs. Use vibrant and appealing colors to make the profile picture stand out and convey a sense of creativity and innovation.")
33
 
 
 
 
 
 
34
  if st.button("Generate Image"):
35
  with st.spinner('Generating image...'):
36
- images = pipe([user_prompt], num_inference_steps=50)["images"]
37
-
38
- for i, image in enumerate(images):
39
- file_path = os.path.join(output_dir, f"aditya_profile_pic_{i}.png")
40
- image.save(file_path)
41
- st.image(file_path, caption=f"Generated Image {i+1}")
42
- st.success(f"Saved image to {file_path}")
43
-
44
  st.balloons()
 
1
  import streamlit as st
 
2
  import torch
3
+ import numpy as np
4
+ from diffusers import DiffusionPipeline
5
+ from transformers import pipeline
6
+
7
+ text_pipe = pipeline('text-generation', model='daspartho/prompt-extend')
8
+
9
+ def extend_prompt(prompt):
10
+ return text_pipe(prompt + ',', num_return_sequences=1)[0]["generated_text"]
11
 
 
12
  @st.cache_resource
13
+ def load_pipeline(use_cuda):
14
+ device = "cuda" if use_cuda and torch.cuda.is_available() else "cpu"
15
+ pipe = DiffusionPipeline.from_pretrained(
16
+ "stabilityai/sdxl-turbo",
17
+ torch_dtype=torch.float16 if device == "cuda" else torch.float32,
18
+ variant="fp16" if device == "cuda" else None,
19
+ use_safetensors=True
20
+ )
21
+ if device == "cuda":
22
+ pipe.enable_xformers_memory_efficient_attention()
23
  pipe.to(device)
24
  return pipe
25
 
26
+ def generate_image(prompt, use_details, steps, seed, use_cuda):
27
+ pipe = load_pipeline(use_cuda)
28
+ generator = torch.manual_seed(seed) if seed != 0 else np.random.seed(0)
29
+ extended_prompt = extend_prompt(prompt) if use_details else prompt
30
+ image = pipe(prompt=extended_prompt, generator=generator, num_inference_steps=steps, guidance_scale=0.0).images[0]
31
+ return image, extended_prompt
32
+
33
+ st.markdown("""
34
+ <style>
35
+ body {background-color: #E8F5E9;}
36
+ .header {font-size: 2.5em; color: #2E7D32; text-align: center; margin-top: 20px;}
37
+ .subheader {font-size: 1.2em; color: #4CAF50; text-align: center; margin-bottom: 30px;}
38
+ </style>
39
+ """, unsafe_allow_html=True)
40
 
 
 
 
 
 
41
  st.markdown("<div class='header'>✨ Generate Your Custom GitHub Profile Picture! ✨</div>", unsafe_allow_html=True)
42
+ st.markdown("<div class='subheader'>Create an anime-style GitHub profile picture that reflects your personality and passion for coding. πŸš€πŸ‘¨β€πŸ’»</div>", unsafe_allow_html=True)
43
 
44
+ input_text = st.text_area("Describe your GitHub profile picture:",
 
45
  "Create an anime-style GitHub profile picture for a boy. The character should have a friendly and approachable expression, embodying a sense of curiosity and enthusiasm, reflecting the qualities of a passionate coder. Incorporate elements that suggest technology or coding, such as a pair of stylish glasses, a laptop, or a background with subtle code or tech motifs. Use vibrant and appealing colors to make the profile picture stand out and convey a sense of creativity and innovation.")
46
 
47
+ details_checkbox = st.checkbox("Generate Details?", value=True)
48
+ steps_slider = st.slider("Number of Iterations", min_value=1, max_value=5, value=2, step=1)
49
+ seed_slider = st.slider("Seed", min_value=0, max_value=999999999999999999, value=398231747038484200, step=1)
50
+ cuda_checkbox = st.checkbox("Use CUDA?", value=False)
51
+
52
  if st.button("Generate Image"):
53
  with st.spinner('Generating image...'):
54
+ image, extended_prompt = generate_image(input_text, details_checkbox, steps_slider, seed_slider, cuda_checkbox)
55
+ st.image(image, caption="Generated Image")
56
+ st.text(f"Extended Prompt: {extended_prompt}")
 
 
 
 
 
57
  st.balloons()