pradeep6kumar2024 commited on
Commit
7c5ef3c
·
1 Parent(s): 098b68e

Gradio app for textual inversion

Browse files
Files changed (3) hide show
  1. README.md +49 -14
  2. app.py +120 -0
  3. requirements.txt +7 -0
README.md CHANGED
@@ -1,14 +1,49 @@
1
- ---
2
- title: SD Textual Inversion Embeds
3
- emoji: 😻
4
- colorFrom: yellow
5
- colorTo: pink
6
- sdk: gradio
7
- sdk_version: 5.20.0
8
- app_file: app.py
9
- pinned: false
10
- license: mit
11
- short_description: Compare 5 different Embeds Style
12
- ---
13
-
14
- Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Style-Guided Image Generation with Purple Enhancement
2
+
3
+ This Space demonstrates the use of various textual inversion style embeddings with Stable Diffusion, combined with a custom purple color enhancement technique.
4
+
5
+ ## Features
6
+
7
+ - **Multiple Style Options**: Choose from 5 different artistic styles:
8
+ - Glitch Core (`001glitch-core`)
9
+ - Roth Style (`2814-roth`)
10
+ - Night Style (`4tnght`)
11
+ - 80s Anime (`80s-anime-ai`)
12
+ - Anime AI Being (`80s-anime-ai-being`)
13
+
14
+ - **Purple Guidance**: Optional color enhancement that adds purple tones to the generated images
15
+ - **Customizable Parameters**:
16
+ - Adjustable seed for reproducibility
17
+ - Control over purple guidance strength
18
+ - Custom prompt input
19
+
20
+ ## How to Use
21
+
22
+ 1. Enter your prompt in the text box
23
+ 2. Select a style from the radio buttons
24
+ 3. (Optional) Adjust the seed number for different variations
25
+ 4. (Optional) Enable purple guidance and adjust its strength
26
+ 5. Click "Submit" to generate the image
27
+
28
+ ## Examples
29
+
30
+ The app includes several example combinations that you can try:
31
+ - Mountain landscape with glitch effect
32
+ - Magical forest in 80s anime style
33
+ - Cyberpunk city with night style
34
+
35
+ ## Technical Details
36
+
37
+ This application uses:
38
+ - Stable Diffusion v1.4 as the base model
39
+ - Textual Inversion embeddings from the Hugging Face Hub
40
+ - Custom purple color guidance implementation
41
+ - Gradio for the user interface
42
+
43
+ ## Credits
44
+
45
+ Style embeddings are from the [SD Concepts Library](https://huggingface.co/sd-concepts-library) on Hugging Face.
46
+
47
+ ## License
48
+
49
+ This project is released under the MIT License. The used models and embeddings maintain their original licenses.
app.py ADDED
@@ -0,0 +1,120 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import torch
3
+ from diffusers import StableDiffusionPipeline
4
+ from huggingface_hub import hf_hub_download
5
+ import numpy as np
6
+ from PIL import Image
7
+ import os
8
+
9
+ # Suppress symlink warnings
10
+ os.environ['HF_HUB_DISABLE_SYMLINKS_WARNING'] = "1"
11
+
12
+ # Define styles
13
+ styles = {
14
+ "glitch": {
15
+ "concept_url": "sd-concepts-library/001glitch-core",
16
+ "seed": 42,
17
+ "token": "<glitch-core>"
18
+ },
19
+ "roth": {
20
+ "concept_url": "sd-concepts-library/2814-roth",
21
+ "seed": 123,
22
+ "token": "<2814-roth>"
23
+ },
24
+ "night": {
25
+ "concept_url": "sd-concepts-library/4tnght",
26
+ "seed": 456,
27
+ "token": "<4tnght>"
28
+ },
29
+ "anime80s": {
30
+ "concept_url": "sd-concepts-library/80s-anime-ai",
31
+ "seed": 789,
32
+ "token": "<80s-anime>"
33
+ },
34
+ "animeai": {
35
+ "concept_url": "sd-concepts-library/80s-anime-ai-being",
36
+ "seed": 1024,
37
+ "token": "<80s-anime-being>"
38
+ }
39
+ }
40
+
41
+ def load_pipeline():
42
+ """Load and prepare the pipeline with all style embeddings"""
43
+ pipe = StableDiffusionPipeline.from_pretrained(
44
+ "CompVis/stable-diffusion-v1-4",
45
+ torch_dtype=torch.float16
46
+ ).to("cuda")
47
+
48
+ # Load all embeddings
49
+ for style_info in styles.values():
50
+ embedding_path = hf_hub_download(
51
+ repo_id=style_info["concept_url"],
52
+ filename="learned_embeds.bin",
53
+ repo_type="model"
54
+ )
55
+ pipe.load_textual_inversion(embedding_path)
56
+
57
+ return pipe
58
+
59
+ def apply_purple_guidance(image, strength=0.5):
60
+ """Apply purple guidance to an image"""
61
+ img_array = np.array(image).astype(float)
62
+ purple_mask = (img_array[:,:,0] > 100) & (img_array[:,:,2] > 100)
63
+ img_array[purple_mask] = img_array[purple_mask] * (1 - strength) + np.array([128, 0, 128]) * strength
64
+ return Image.fromarray(np.uint8(img_array.clip(0, 255)))
65
+
66
+ def generate_image(prompt, style, seed, apply_guidance, guidance_strength=0.5):
67
+ """Generate an image with selected style and optional purple guidance"""
68
+ if style not in styles:
69
+ return None
70
+
71
+ # Get style info
72
+ style_info = styles[style]
73
+
74
+ # Prepare generator
75
+ generator = torch.Generator("cuda").manual_seed(int(seed))
76
+
77
+ # Create styled prompt
78
+ styled_prompt = f"{prompt} {style_info['token']}"
79
+
80
+ # Generate image
81
+ image = pipe(
82
+ styled_prompt,
83
+ generator=generator,
84
+ guidance_scale=7.5,
85
+ num_inference_steps=50
86
+ ).images[0]
87
+
88
+ # Apply purple guidance if requested
89
+ if apply_guidance:
90
+ image = apply_purple_guidance(image, guidance_strength)
91
+
92
+ return image
93
+
94
+ # Initialize the pipeline globally
95
+ print("Loading pipeline and embeddings...")
96
+ pipe = load_pipeline()
97
+
98
+ # Create the Gradio interface
99
+ demo = gr.Interface(
100
+ fn=generate_image,
101
+ inputs=[
102
+ gr.Textbox(label="Prompt", value="A serene mountain landscape with a lake at sunset"),
103
+ gr.Radio(choices=list(styles.keys()), label="Style", value="glitch"),
104
+ gr.Number(label="Seed", value=42),
105
+ gr.Checkbox(label="Apply Purple Guidance", value=False),
106
+ gr.Slider(minimum=0.0, maximum=1.0, value=0.5, label="Purple Guidance Strength")
107
+ ],
108
+ outputs=gr.Image(label="Generated Image"),
109
+ title="Style-Guided Image Generation with Purple Enhancement",
110
+ description="""Generate images in different styles with optional purple color guidance.
111
+ Choose a style, enter a prompt, and optionally apply purple color enhancement.""",
112
+ examples=[
113
+ ["A serene mountain landscape with a lake at sunset", "glitch", 42, True, 0.5],
114
+ ["A magical forest at twilight", "anime80s", 789, True, 0.7],
115
+ ["A cyberpunk city at night", "night", 456, False, 0.5],
116
+ ]
117
+ )
118
+
119
+ if __name__ == "__main__":
120
+ demo.launch()
requirements.txt ADDED
@@ -0,0 +1,7 @@
 
 
 
 
 
 
 
 
1
+ torch
2
+ diffusers
3
+ transformers
4
+ gradio
5
+ numpy
6
+ Pillow
7
+ accelerate