Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,13 +1,17 @@
|
|
| 1 |
pip install diffusers transformers accelerate
|
| 2 |
pip install gradio
|
|
|
|
| 3 |
import torch
|
| 4 |
from diffusers import StableDiffusionPipeline
|
| 5 |
-
import matplotlib.pyplot as plt
|
| 6 |
import gradio as gr
|
| 7 |
-
# List of NSFW prompt keywords
|
| 8 |
-
NSFW_KEYWORDS = ["nude", "naked", "sex", "porn", "erotic", "nsfw", "boobs", "breasts", "genital", "vagina", "penis"]
|
| 9 |
|
| 10 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
MODELS = {
|
| 12 |
"Stable Diffusion v1.5": "runwayml/stable-diffusion-v1-5",
|
| 13 |
"Dreamlike Diffusion": "dreamlike-art/dreamlike-diffusion-1.0",
|
|
@@ -15,6 +19,15 @@ MODELS = {
|
|
| 15 |
"Waifu Diffusion (Anime Style)": "hakurei/waifu-diffusion"
|
| 16 |
}
|
| 17 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
loaded_pipelines = {}
|
| 19 |
|
| 20 |
def get_pipeline(model_name):
|
|
@@ -32,7 +45,6 @@ def generate_image(prompt, model_name):
|
|
| 32 |
pipe = get_pipeline(model_name)
|
| 33 |
result = pipe(prompt)
|
| 34 |
|
| 35 |
-
# Check output safety
|
| 36 |
if result.nsfw_content_detected and any(result.nsfw_content_detected):
|
| 37 |
raise gr.Error("❌ Sorry, the generated image was flagged as NSFW. Try a different prompt.")
|
| 38 |
|
|
@@ -41,10 +53,12 @@ def generate_image(prompt, model_name):
|
|
| 41 |
def generate_and_return_image(prompt, model_name):
|
| 42 |
if any(word in prompt.lower() for word in NSFW_KEYWORDS):
|
| 43 |
raise gr.Error("❌ Sorry, I can't generate images with NSFW content.")
|
| 44 |
-
|
| 45 |
image = generate_image(prompt, model_name)
|
| 46 |
image.save("generated.png")
|
| 47 |
return image
|
|
|
|
|
|
|
| 48 |
with gr.Blocks(theme=gr.themes.Soft(primary_hue="sky", secondary_hue="blue")) as demo:
|
| 49 |
gr.Markdown("## 🎨 Text-to-Image Generator")
|
| 50 |
|
|
@@ -67,4 +81,4 @@ with gr.Blocks(theme=gr.themes.Soft(primary_hue="sky", secondary_hue="blue")) as
|
|
| 67 |
generate_button.click(fn=generate_and_return_image, inputs=[prompt, model_choice], outputs=image_output)
|
| 68 |
download_button.click(fn=lambda: "generated.png", outputs=gr.File())
|
| 69 |
|
| 70 |
-
demo.launch()
|
|
|
|
| 1 |
pip install diffusers transformers accelerate
|
| 2 |
pip install gradio
|
| 3 |
+
|
| 4 |
import torch
|
| 5 |
from diffusers import StableDiffusionPipeline
|
|
|
|
| 6 |
import gradio as gr
|
|
|
|
|
|
|
| 7 |
|
| 8 |
+
# NSFW keywords filter list
|
| 9 |
+
NSFW_KEYWORDS = [
|
| 10 |
+
"nude", "naked", "sex", "porn", "erotic", "nsfw",
|
| 11 |
+
"boobs", "breasts", "genital", "vagina", "penis"
|
| 12 |
+
]
|
| 13 |
+
|
| 14 |
+
# Available models
|
| 15 |
MODELS = {
|
| 16 |
"Stable Diffusion v1.5": "runwayml/stable-diffusion-v1-5",
|
| 17 |
"Dreamlike Diffusion": "dreamlike-art/dreamlike-diffusion-1.0",
|
|
|
|
| 19 |
"Waifu Diffusion (Anime Style)": "hakurei/waifu-diffusion"
|
| 20 |
}
|
| 21 |
|
| 22 |
+
# Example prompts
|
| 23 |
+
example_prompts = [
|
| 24 |
+
"A futuristic city at sunset in cyberpunk style",
|
| 25 |
+
"A cute dog astronaut floating in space",
|
| 26 |
+
"A dreamy landscape with flying islands",
|
| 27 |
+
"An ancient temple in a deep jungle",
|
| 28 |
+
|
| 29 |
+
]
|
| 30 |
+
|
| 31 |
loaded_pipelines = {}
|
| 32 |
|
| 33 |
def get_pipeline(model_name):
|
|
|
|
| 45 |
pipe = get_pipeline(model_name)
|
| 46 |
result = pipe(prompt)
|
| 47 |
|
|
|
|
| 48 |
if result.nsfw_content_detected and any(result.nsfw_content_detected):
|
| 49 |
raise gr.Error("❌ Sorry, the generated image was flagged as NSFW. Try a different prompt.")
|
| 50 |
|
|
|
|
| 53 |
def generate_and_return_image(prompt, model_name):
|
| 54 |
if any(word in prompt.lower() for word in NSFW_KEYWORDS):
|
| 55 |
raise gr.Error("❌ Sorry, I can't generate images with NSFW content.")
|
| 56 |
+
|
| 57 |
image = generate_image(prompt, model_name)
|
| 58 |
image.save("generated.png")
|
| 59 |
return image
|
| 60 |
+
|
| 61 |
+
# Gradio Interface
|
| 62 |
with gr.Blocks(theme=gr.themes.Soft(primary_hue="sky", secondary_hue="blue")) as demo:
|
| 63 |
gr.Markdown("## 🎨 Text-to-Image Generator")
|
| 64 |
|
|
|
|
| 81 |
generate_button.click(fn=generate_and_return_image, inputs=[prompt, model_choice], outputs=image_output)
|
| 82 |
download_button.click(fn=lambda: "generated.png", outputs=gr.File())
|
| 83 |
|
| 84 |
+
demo.launch()
|