Spaces:
Runtime error
Runtime error
Download app.py from sillyguy21/genova-ai: direct link, hf CLI and curl.
- Browser
- Download file 3.11 kB
-
https://huggingface.co/spaces/sillyguy21/genova-ai/resolve/main/app.py
- Command line
-
hf download hf://spaces/sillyguy21/genova-ai/app.py
-
curl -L -o app.py https://huggingface.co/spaces/sillyguy21/genova-ai/resolve/main/app.py
3.11 kB
| import gradio as gr | |
| import torch | |
| import random | |
| from huggingface_hub import HfApi | |
| from diffusers import DiffusionPipeline | |
| MODEL_ID = "SyntheticMDProductions/ADiM_1.0_Image_Model" | |
| MAX_SEED = 2**31 - 1 | |
| device = "cuda" if torch.cuda.is_available() else "cpu" | |
| dtype = torch.float16 if device == "cuda" else torch.float32 | |
| pipe = None | |
| load_error = None | |
| def load_model(): | |
| global pipe, load_error | |
| try: | |
| api = HfApi() | |
| files = api.list_repo_files(MODEL_ID) | |
| print("Files found in model repository:") | |
| for filename in files: | |
| print(" -", filename) | |
| # A standard Diffusers pipeline needs model_index.json. | |
| if "model_index.json" not in files: | |
| raise RuntimeError( | |
| "This repository does not contain model_index.json. " | |
| "It is not packaged as a standard Diffusers pipeline. " | |
| "The model's custom PyTorch architecture and loading " | |
| "instructions are needed before it can generate images." | |
| ) | |
| pipe = DiffusionPipeline.from_pretrained( | |
| MODEL_ID, | |
| torch_dtype=dtype | |
| ) | |
| pipe.to(device) | |
| except Exception as error: | |
| load_error = str(error) | |
| print("MODEL LOADING ERROR:", load_error) | |
| load_model() | |
| def generate(prompt, seed, randomize_seed): | |
| if not prompt or not prompt.strip(): | |
| raise gr.Error("Please enter a prompt.") | |
| if load_error: | |
| raise gr.Error( | |
| "The model could not be loaded:\n\n" + load_error | |
| ) | |
| if pipe is None: | |
| raise gr.Error("The model has not loaded.") | |
| if randomize_seed: | |
| seed = random.randint(0, MAX_SEED) | |
| generator = torch.Generator(device=device).manual_seed(int(seed)) | |
| try: | |
| with torch.inference_mode(): | |
| result = pipe( | |
| prompt=prompt, | |
| generator=generator | |
| ) | |
| return result.images[0], int(seed) | |
| except Exception as error: | |
| raise gr.Error("Image generation failed: " + str(error)) | |
| with gr.Blocks(title="ADiM 1.0 Image Generator") as demo: | |
| gr.Markdown("# ADiM 1.0 Image Generator") | |
| gr.Markdown("Experimental text-to-image model") | |
| prompt = gr.Textbox( | |
| label="Prompt", | |
| placeholder="Describe an image...", | |
| lines=3 | |
| ) | |
| with gr.Row(): | |
| seed = gr.Number( | |
| label="Seed", | |
| value=0, | |
| precision=0 | |
| ) | |
| randomize_seed = gr.Checkbox( | |
| label="Randomize seed", | |
| value=True | |
| ) | |
| generate_button = gr.Button( | |
| "Generate Image", | |
| variant="primary" | |
| ) | |
| output_image = gr.Image(label="Generated Image") | |
| output_seed = gr.Number(label="Used Seed", precision=0) | |
| generate_button.click( | |
| fn=generate, | |
| inputs=[prompt, seed, randomize_seed], | |
| outputs=[output_image, output_seed] | |
| ) | |
| prompt.submit( | |
| fn=generate, | |
| inputs=[prompt, seed, randomize_seed], | |
| outputs=[output_image, output_seed] | |
| ) | |
| if __name__ == "__main__": | |
| demo.launch() | |