MichalMlodawski commited on
Commit
ad0d620
1 Parent(s): c952ee6

Create README.md

Browse files
Files changed (1) hide show
  1. README.md +150 -0
README.md ADDED
@@ -0,0 +1,150 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ license: cc-by-nc-nd-4.0
3
+ language:
4
+ - en
5
+ ---
6
+
7
+
8
+ # ✨ Flan-T5 Negative Prompt Generator
9
+
10
+ ## 🌟 Model Overview
11
+
12
+ This model is a fine-tuned version of Google's Flan-T5 specifically designed for generating negative prompts for image generation. It takes a positive prompt as input and produces a corresponding negative prompt to guide image generation models away from unwanted elements.
13
+
14
+ ## 🔍 Model Details
15
+
16
+ - **Base Model:** Flan-T5 (Google)
17
+ - **Task:** Negative Prompt Generation
18
+ - **Language:** English (primarily, but can handle multilingual inputs)
19
+ - **License:** cc-by-nc-nd-4.0
20
+
21
+ ## 🎯 Use Cases
22
+
23
+ This model is particularly useful for:
24
+
25
+ 1. 🖼️ Enhancing AI image generation by providing tailored negative prompts
26
+ 2. 🛠️ Assisting artists and designers in refining their image generation process
27
+ 3. 🔒 Content moderation and safety in AI-generated visual content
28
+ 4. 🧪 Experimenting with prompt engineering techniques
29
+
30
+ ## 💡 How It Works
31
+
32
+ The model takes a positive image generation prompt as input and generates a corresponding negative prompt. This negative prompt can be used alongside the positive prompt in image generation models to avoid unwanted elements or styles in the generated image.
33
+
34
+ ## 🚀 Getting Started
35
+
36
+ To use the Flan-T5 Negative Prompt Generator, follow these steps:
37
+
38
+ ### Installation
39
+
40
+ ```bash
41
+ pip install transformers torch
42
+ ```
43
+
44
+ ### Usage
45
+
46
+ ```python
47
+ from transformers import T5Tokenizer, T5ForConditionalGeneration
48
+ import torch
49
+
50
+ # Load the model and tokenizer
51
+ model_path = "MichalMlodawski/negative-prompt-generator-large"
52
+ tokenizer = T5Tokenizer.from_pretrained(model_path)
53
+ model = T5ForConditionalGeneration.from_pretrained(model_path)
54
+
55
+ def generate_negative_prompt(prompt, min_length=10, length_penalty=2.0, num_beams=4):
56
+ input_ids = tokenizer(f"Generate negative prompt using this: {prompt}", return_tensors="pt").input_ids
57
+
58
+ # Calculate dynamic max_length
59
+ input_length = input_ids.shape[1]
60
+ max_length = min(input_length * 2, 512) # Adjust this formula as needed
61
+
62
+ outputs = model.generate(
63
+ input_ids,
64
+ max_length=max_length,
65
+ min_length=min_length,
66
+ length_penalty=length_penalty,
67
+ num_beams=num_beams,
68
+ early_stopping=True
69
+ )
70
+ return tokenizer.decode(outputs[0], skip_special_tokens=True)
71
+
72
+ # Example usage
73
+ positive_prompt = "A beautiful sunset over a calm ocean"
74
+ negative_prompt = generate_negative_prompt(positive_prompt)
75
+ print(f"Positive Prompt: {positive_prompt}")
76
+ print(f"Generated Negative Prompt: {negative_prompt}")
77
+ ```
78
+
79
+ ## 🎨 Examples
80
+
81
+ Here are a few examples of how the model generates negative prompts:
82
+
83
+ 1. **Positive Prompt:** "Generate negative prompt using this: A serene forest landscape with a clear stream"
84
+ **Negative Prompt:** "low res, bad anatomy, bad hands, text, error, missing fingers, extra digit, fewer digits, cropped, worst quality"
85
+
86
+ 2. **Positive Prompt:** "Generate negative prompt using this: side view, Surreal photograph of a figure of one male kneeling in a foggy forest."
87
+ **Negative Prompt:** "low res, bad anatomy, bad hands, text, error, missing fingers, extra digit, fewer digits, cropped, worst quality, low quality, normal quality, jpeg artifacts, signature, watermark, username, blurry"
88
+
89
+ 3. **Positive Prompt:** "Generate negative prompt using this: A photorealistic 4K image of a magical red crystal with sharp, jagged edges, dripping dark, crimson blood. The scene is dramatic with intense, focused lighting that highlights the intricate facets of the crystal. The blood appears thick, slowly flowing down from the crystal, creating a stark contrast against the sharp, glittering surface. The overall atmosphere is dark and intense, with a sense of power and mystery surrounding the crystal. Masterpiece quality, highly detailed."
90
+ **Negative Prompt:** "low res, text, error, cropped, worst quality, low quality, normal quality, jpeg artifacts, signature, watermark, username, blurry, artist name, censored, shirt"
91
+
92
+ ## 🛠️ Fine-tuning and Customization
93
+
94
+ If you want to fine-tune this model on your own dataset or customize it for specific use cases, you can use the Hugging Face Transformers library. Here's a basic example of how to start the fine-tuning process:
95
+
96
+ ```python
97
+ from transformers import T5ForConditionalGeneration, T5Tokenizer, Trainer, TrainingArguments
98
+
99
+ # Load the model and tokenizer
100
+ model = T5ForConditionalGeneration.from_pretrained("google/flan-t5-large")
101
+ tokenizer = T5Tokenizer.from_pretrained("google/flan-t5-large")
102
+
103
+ # Prepare your dataset
104
+ # ... (code to load and preprocess your dataset)
105
+
106
+ # Set up training arguments
107
+ training_args = TrainingArguments(
108
+ output_dir="./results",
109
+ num_train_epochs=3,
110
+ per_device_train_batch_size=8,
111
+ per_device_eval_batch_size=8,
112
+ warmup_steps=500,
113
+ weight_decay=0.01,
114
+ logging_dir="./logs",
115
+ )
116
+
117
+ # Create Trainer instance
118
+ trainer = Trainer(
119
+ model=model,
120
+ args=training_args,
121
+ train_dataset=train_dataset,
122
+ eval_dataset=eval_dataset,
123
+ tokenizer=tokenizer,
124
+ )
125
+
126
+ # Start training
127
+ trainer.train()
128
+ ```
129
+
130
+ ## 📊 Limitations
131
+
132
+ While this model is powerful for generating negative prompts, it's important to note its limitations:
133
+
134
+ - The model may sometimes generate inconsistent or contradictory negative prompts.
135
+ - Performance may vary depending on the complexity and specificity of the input prompt.
136
+ - The model's output is based on its training data and may reflect biases present in that data.
137
+
138
+
139
+ ## 📚 Citation
140
+
141
+ If you use this model in your research or project, please cite it as follows:
142
+
143
+ @misc{flant5_negative_prompt_generator,
144
+ author = { Michał Młodawski},
145
+ title = {Flan-T5 Negative Prompt Generator},
146
+ year = {2024},
147
+ howpublished = {\url{https://huggingface.co/MichalMlodawski/negative-prompt-generator-large}},
148
+ note = {Based on Google's Flan-T5 model},
149
+ keywords = {natural language processing, prompt engineering, image generation}
150
+ }