Spaces:
Sleeping
Sleeping
Download app.py from Harshavarhini/realistic-vision-api: direct link, hf CLI and curl.
- Browser
- Download file 1.42 kB
-
https://huggingface.co/spaces/Harshavarhini/realistic-vision-api/resolve/ad5485e087ebca0aef98e834e1870105da9a958e/app.py
- Command line
-
hf download hf://spaces/Harshavarhini/realistic-vision-api@ad5485e087ebca0aef98e834e1870105da9a958e/app.py
-
curl -L -o app.py https://huggingface.co/spaces/Harshavarhini/realistic-vision-api/resolve/ad5485e087ebca0aef98e834e1870105da9a958e/app.py
1.42 kB
| import os | |
| import torch | |
| from diffusers import StableDiffusionPipeline | |
| from huggingface_hub import hf_hub_download | |
| import gradio as gr | |
| # ---------------- CONFIG ---------------- | |
| MODEL_REPO = "Harshavarhini/realistic-vision-v6" | |
| MODEL_FILE = "realisticVisionV60B1_v51HyperVAE.safetensors" | |
| # ---------------- DOWNLOAD MODEL ---------------- | |
| model_path = hf_hub_download( | |
| repo_id=MODEL_REPO, | |
| filename=MODEL_FILE # 🔥 REQUIRED (fixes 401 error) | |
| ) | |
| # ---------------- DEVICE SETUP ---------------- | |
| device = "cuda" if torch.cuda.is_available() else "cpu" | |
| # ---------------- LOAD PIPELINE ---------------- | |
| pipe = StableDiffusionPipeline.from_single_file( | |
| model_path, | |
| torch_dtype=torch.float16 if device == "cuda" else torch.float32, | |
| low_cpu_mem_usage=True | |
| ) | |
| pipe = pipe.to(device) | |
| # 🔥 Memory optimization (VERY IMPORTANT for Spaces) | |
| pipe.enable_attention_slicing() | |
| # ---------------- GENERATION FUNCTION ---------------- | |
| def generate(prompt): | |
| image = pipe( | |
| prompt, | |
| num_inference_steps=30, | |
| guidance_scale=7.5 | |
| ).images[0] | |
| return image | |
| # ---------------- GRADIO UI ---------------- | |
| demo = gr.Interface( | |
| fn=generate, | |
| inputs=gr.Textbox(label="Enter Prompt"), | |
| outputs=gr.Image(type="pil"), | |
| title="Realistic Vision v6 API", | |
| description="Generate realistic images using Stable Diffusion" | |
| ) | |
| # ---------------- LAUNCH ---------------- | |
| demo.launch() |