Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,138 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import requests
|
| 3 |
+
import io
|
| 4 |
+
import random
|
| 5 |
+
import os
|
| 6 |
+
from PIL import Image
|
| 7 |
+
|
| 8 |
+
# List of available models
|
| 9 |
+
list_models = [
|
| 10 |
+
"SDXL 1.0", "SD 1.5", "OpenJourney", "Anything V4.0",
|
| 11 |
+
"Disney Pixar Cartoon", "Pixel Art XL", "Dalle 3 XL",
|
| 12 |
+
"Midjourney V4 XL", "Open Diffusion V1", "SSD 1B",
|
| 13 |
+
"Segmind Vega", "Animagine XL-2.0", "Animagine XL-3.0",
|
| 14 |
+
"OpenDalle", "OpenDalle V1.1", "PlaygroundV2 1024px aesthetic",
|
| 15 |
+
]
|
| 16 |
+
|
| 17 |
+
# Function to generate images from text
|
| 18 |
+
def generate_txt2img(current_model, prompt, is_negative=False, image_style="None style", steps=50, cfg_scale=7, seed=None):
|
| 19 |
+
|
| 20 |
+
if current_model == "SD 1.5":
|
| 21 |
+
API_URL = "https://api-inference.huggingface.co/models/runwayml/stable-diffusion-v1-5"
|
| 22 |
+
elif current_model == "SDXL 1.0":
|
| 23 |
+
API_URL = "https://api-inference.huggingface.co/models/stabilityai/stable-diffusion-xl-base-1.0"
|
| 24 |
+
elif current_model == "OpenJourney":
|
| 25 |
+
API_URL = "https://api-inference.huggingface.co/models/prompthero/openjourney"
|
| 26 |
+
elif current_model == "Anything V4.0":
|
| 27 |
+
API_URL = "https://api-inference.huggingface.co/models/xyn-ai/anything-v4.0"
|
| 28 |
+
elif current_model == "Disney Pixar Cartoon":
|
| 29 |
+
API_URL = "https://api-inference.huggingface.co/models/stablediffusionapi/disney-pixar-cartoon"
|
| 30 |
+
elif current_model == "Pixel Art XL":
|
| 31 |
+
API_URL = "https://api-inference.huggingface.co/models/nerijs/pixel-art-xl"
|
| 32 |
+
elif current_model == "Dalle 3 XL":
|
| 33 |
+
API_URL = "https://api-inference.huggingface.co/models/openskyml/dalle-3-xl"
|
| 34 |
+
elif current_model == "Midjourney V4 XL":
|
| 35 |
+
API_URL = "https://api-inference.huggingface.co/models/openskyml/midjourney-v4-xl"
|
| 36 |
+
elif current_model == "Open Diffusion V1":
|
| 37 |
+
API_URL = "https://api-inference.huggingface.co/models/openskyml/open-diffusion-v1"
|
| 38 |
+
elif current_model == "SSD 1B":
|
| 39 |
+
API_URL = "https://api-inference.huggingface.co/models/segmind/SSD-1B"
|
| 40 |
+
elif current_model == "Segmind Vega":
|
| 41 |
+
API_URL = "https://api-inference.huggingface.co/models/segmind/Segmind-Vega"
|
| 42 |
+
elif current_model == "Animagine XL-2.0":
|
| 43 |
+
API_URL = "https://api-inference.huggingface.co/models/Linaqruf/animagine-xl-2.0"
|
| 44 |
+
elif current_model == "Animagine XL-3.0":
|
| 45 |
+
API_URL = "https://api-inference.huggingface.co/models/cagliostrolab/animagine-xl-3.0"
|
| 46 |
+
elif current_model == "OpenDalle":
|
| 47 |
+
API_URL = "https://api-inference.huggingface.co/models/dataautogpt3/OpenDalle"
|
| 48 |
+
elif current_model == "OpenDalle V1.1":
|
| 49 |
+
API_URL = "https://api-inference.huggingface.co/models/dataautogpt3/OpenDalleV1.1"
|
| 50 |
+
elif current_model == "PlaygroundV2 1024px aesthetic":
|
| 51 |
+
API_URL = "https://api-inference.huggingface.co/models/playgroundai/playground-v2-1024px-aesthetic"
|
| 52 |
+
|
| 53 |
+
|
| 54 |
+
api_key = os.getenv("ImageGenerating")
|
| 55 |
+
headers = {"Authorization": "f'Bearer {api_key}"}
|
| 56 |
+
|
| 57 |
+
|
| 58 |
+
if image_style == "None style":
|
| 59 |
+
payload = {
|
| 60 |
+
"inputs": prompt + ", 8k",
|
| 61 |
+
"is_negative": is_negative,
|
| 62 |
+
"steps": steps,
|
| 63 |
+
"cfg_scale": cfg_scale,
|
| 64 |
+
"seed": seed if seed is not None else random.randint(-1, 2147483647)
|
| 65 |
+
}
|
| 66 |
+
elif image_style == "Cinematic":
|
| 67 |
+
payload = {
|
| 68 |
+
"inputs": prompt + ", realistic, detailed, textured, skin, hair, eyes, by Alex Huguet, Mike Hill, Ian Spriggs, JaeCheol Park, Marek Denko",
|
| 69 |
+
"is_negative": is_negative + ", abstract, cartoon, stylized",
|
| 70 |
+
"steps": steps,
|
| 71 |
+
"cfg_scale": cfg_scale,
|
| 72 |
+
"seed": seed if seed is not None else random.randint(-1, 2147483647)
|
| 73 |
+
}
|
| 74 |
+
elif image_style == "Digital Art":
|
| 75 |
+
payload = {
|
| 76 |
+
"inputs": prompt + ", faded , vintage , nostalgic , by Jose Villa , Elizabeth Messina , Ryan Brenizer , Jonas Peterson , Jasmine Star",
|
| 77 |
+
"is_negative": is_negative + ", sharp , modern , bright",
|
| 78 |
+
"steps": steps,
|
| 79 |
+
"cfg_scale": cfg_scale,
|
| 80 |
+
"seed": seed if seed is not None else random.randint(-1, 2147483647)
|
| 81 |
+
}
|
| 82 |
+
elif image_style == "Portrait":
|
| 83 |
+
payload = {
|
| 84 |
+
"inputs": prompt + ", soft light, sharp, exposure blend, medium shot, bokeh, (hdr:1.4), high contrast, (cinematic, teal and orange:0.85), (muted colors, dim colors, soothing tones:1.3), low saturation, (hyperdetailed:1.2), (noir:0.4), (natural skin texture, hyperrealism, soft light, sharp:1.2)",
|
| 85 |
+
"is_negative": is_negative,
|
| 86 |
+
"steps": steps,
|
| 87 |
+
"cfg_scale": cfg_scale,
|
| 88 |
+
"seed": seed if seed is not None else random.randint(-1, 2147483647)
|
| 89 |
+
}
|
| 90 |
+
|
| 91 |
+
image_bytes = requests.post(API_URL, headers=headers, json=payload).content
|
| 92 |
+
image = Image.open(io.BytesIO(image_bytes))
|
| 93 |
+
return image
|
| 94 |
+
|
| 95 |
+
# Function to read CSS from file
|
| 96 |
+
def read_css_from_file(filename):
|
| 97 |
+
with open(filename, "r") as file:
|
| 98 |
+
return file.read()
|
| 99 |
+
|
| 100 |
+
# Read CSS from file
|
| 101 |
+
css = read_css_from_file("style.css")
|
| 102 |
+
|
| 103 |
+
PTI_SD_DESCRIPTION = '''
|
| 104 |
+
<div id="content_align">
|
| 105 |
+
<span style="color:darkred;font-size:32px;font-weight:bold">
|
| 106 |
+
Image Generation using Gradio UI
|
| 107 |
+
</span>
|
| 108 |
+
</div>
|
| 109 |
+
<div id="content_align">
|
| 110 |
+
<span style="color:blue;font-size:16px;font-weight:bold">
|
| 111 |
+
Generate images
|
| 112 |
+
</span>
|
| 113 |
+
</div>
|
| 114 |
+
<div id="content_align" style="margin-top: 10px;">
|
| 115 |
+
</div>
|
| 116 |
+
'''
|
| 117 |
+
|
| 118 |
+
|
| 119 |
+
# Creating Gradio interface
|
| 120 |
+
with gr.Blocks(css=css) as demo:
|
| 121 |
+
gr.Markdown(PTI_SD_DESCRIPTION)
|
| 122 |
+
with gr.Row():
|
| 123 |
+
with gr.Column():
|
| 124 |
+
current_model = gr.Dropdown(label="Select Model to generate", choices=list_models, value=list_models[1])
|
| 125 |
+
text_prompt = gr.Textbox(label="Enter promt", lines=2)
|
| 126 |
+
with gr.Column():
|
| 127 |
+
negative_prompt = gr.Textbox(label="Negative Prompt (optional)", placeholder="Example: blurry, unfocused", lines=2)
|
| 128 |
+
image_style = gr.Dropdown(label="Select Style", choices=["None style", "Cinematic", "Digital Art", "Portrait"], value="None style")
|
| 129 |
+
|
| 130 |
+
generate_button = gr.Button("Submit", variant='primary')
|
| 131 |
+
|
| 132 |
+
with gr.Row():
|
| 133 |
+
image_output = gr.Image(type="pil", label="Generated Image")
|
| 134 |
+
|
| 135 |
+
generate_button.click(generate_txt2img, inputs=[current_model, text_prompt, negative_prompt, image_style], outputs=image_output)
|
| 136 |
+
|
| 137 |
+
# Launch the app
|
| 138 |
+
demo.launch()
|