Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,68 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import numpy as np
|
| 3 |
+
from PIL import Image
|
| 4 |
+
from io import BytesIO
|
| 5 |
+
import requests
|
| 6 |
+
import json
|
| 7 |
+
|
| 8 |
+
# List of available models
|
| 9 |
+
models = [
|
| 10 |
+
"HHM29/finetuning_dream_fin",
|
| 11 |
+
"KappaNeuro/needlepoint",
|
| 12 |
+
"Norod78/ClaymationX_LoRA",
|
| 13 |
+
"KappaNeuro/movie-poster",
|
| 14 |
+
"digiplay/MixTape_RocknRoll_v3punk_bake_fp16",
|
| 15 |
+
"digiplay/BeautifulFantasyRealMix_diffusers",
|
| 16 |
+
"Yntec/pineappleAnimeMix",
|
| 17 |
+
"Yntec/DucHaiten-Retro-Diffusers",
|
| 18 |
+
"joachimsallstrom/aether-pixel-lora-for-sdxl",
|
| 19 |
+
"runwayml/stable-diffusion-v1-5",
|
| 20 |
+
"stabilityai/stable-diffusion-xl-base-1.0",
|
| 21 |
+
"CompVis/stable-diffusion-v1-4",
|
| 22 |
+
]
|
| 23 |
+
|
| 24 |
+
def generate_image(model_name, image, prompt, length, temperature, n_samples, use_image2image=False):
|
| 25 |
+
data = {
|
| 26 |
+
"image_prompt": image,
|
| 27 |
+
"prompt": prompt,
|
| 28 |
+
"length": length,
|
| 29 |
+
"temperature": temperature,
|
| 30 |
+
"n_samples": n_samples,
|
| 31 |
+
"model": model_name,
|
| 32 |
+
}
|
| 33 |
+
|
| 34 |
+
if use_image2image:
|
| 35 |
+
data["use_image2image"] = True
|
| 36 |
+
data["image2image_prompt"] = image # Provide the target image for image2image
|
| 37 |
+
|
| 38 |
+
response = requests.post("https://api.stable-diffusion.ml/generate", json=data)
|
| 39 |
+
response_json = response.json()
|
| 40 |
+
|
| 41 |
+
if response.status_code == 200:
|
| 42 |
+
results = response_json["generated_images"]
|
| 43 |
+
generated_image = np.frombuffer(BytesIO(results[0]["image"]).read(), dtype=np.uint8)
|
| 44 |
+
generated_image = generated_image.reshape(results[0]["metadata"]["height"], results[0]["metadata"]["width"], 3)
|
| 45 |
+
return Image.fromarray(generated_image)
|
| 46 |
+
else:
|
| 47 |
+
return None
|
| 48 |
+
|
| 49 |
+
def app(model=gr.inputs.Selector(options=models),
|
| 50 |
+
image=gr.inputs.Image(shape=(None, None)),
|
| 51 |
+
prompt=gr.inputs.Textbox(default="an image generated with"),
|
| 52 |
+
length=gr.inputs.Slider(1, 20, step=1, default=8),
|
| 53 |
+
temperature=gr.inputs.Slider(0.5, 1.5, step=0.1, default=1),
|
| 54 |
+
n_samples=gr.inputs.Slider(1, 5, step=1, default=1),
|
| 55 |
+
use_image2image=gr.inputs.Boolean(default=False)):
|
| 56 |
+
generated_image = generate_image(model,
|
| 57 |
+
image=image.data if image else None,
|
| 58 |
+
prompt=prompt,
|
| 59 |
+
length=int(length),
|
| 60 |
+
temperature=float(temperature),
|
| 61 |
+
n_samples=int(n_samples),
|
| 62 |
+
use_image2image=use_image2image)
|
| 63 |
+
return gr.outputs.Image(as_pil=True)(generated_image) if generated_image else None
|
| 64 |
+
|
| 65 |
+
if __name__ == "__main__":
|
| 66 |
+
title = "Image Generation App"
|
| 67 |
+
description = "Select a model and customize your image generation or image2image settings!"
|
| 68 |
+
gradio.launch(app, port=8000, title=title, description=description)
|