Spaces:
Runtime error
Runtime error
Download app.py from Thilak118/Text_to_Image_Generation: direct link, hf CLI and curl.
- Browser
- Download file 1.13 kB
-
https://huggingface.co/spaces/Thilak118/Text_to_Image_Generation/resolve/main/app.py
- Command line
-
hf download hf://spaces/Thilak118/Text_to_Image_Generation/app.py
-
curl -L -o app.py https://huggingface.co/spaces/Thilak118/Text_to_Image_Generation/resolve/main/app.py
1.13 kB
| import torch | |
| from diffusers import StableDiffusionPipeline | |
| import gradio as gr | |
| # NSFW keyword filter | |
| NSFW_KEYWORDS = [ | |
| "nude", "naked", "sex", "porn", "erotic", "nsfw", | |
| "boobs", "breasts", "genital", "vagina", "penis" | |
| ] | |
| # Load pipeline on CPU | |
| pipe = StableDiffusionPipeline.from_pretrained( | |
| "runwayml/stable-diffusion-v1-5", | |
| use_safetensors=True | |
| ) | |
| pipe = pipe.to("cpu") | |
| pipe.safety_checker = lambda images, clip_input: (images, False) # Optional: you can remove this if you want the default safety checker | |
| # Function to check and generate image | |
| def generate_image(prompt): | |
| if any(word in prompt.lower() for word in NSFW_KEYWORDS): | |
| raise gr.Error("❌ NSFW content detected in prompt. Please try a different one.") | |
| image = pipe(prompt).images[0] | |
| return image | |
| # Gradio interface | |
| demo = gr.Interface( | |
| fn=generate_image, | |
| inputs=gr.Textbox(lines=1, label="Enter your prompt"), | |
| outputs=gr.Image(label="Generated Image"), | |
| title="🎨 Text-to-Image Generation", | |
| description="Enter a clean prompt to generate images using Stable Diffusion v1.5" | |
| ) | |
| demo.queue() | |
| demo.launch() | |