shengqiangShi commited on
Commit
93e607f
·
1 Parent(s): 81185f1

Add application file

Browse files
Files changed (1) hide show
  1. app.py +148 -4
app.py CHANGED
@@ -1,7 +1,151 @@
1
  import gradio as gr
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2
 
3
- def greet(name):
4
- return "Hello " + name + "!!"
5
 
6
- iface = gr.Interface(fn=greet, inputs="text", outputs="text")
7
- iface.launch()
 
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_TOKEN = os.environ.get("HF_READ_TOKEN")
55
+ headers = {"Authorization": f"Bearer {API_TOKEN}"}
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
+ Stable Diffusion Models Image Generation
107
+ </span>
108
+ </div>
109
+ <div id="content_align">
110
+ <span style="color:blue;font-size:16px;font-weight:bold">
111
+ Generate images directly from text prompts (no parameter tuning required)
112
+ </span>
113
+ </div>
114
+ <div id="content_align" style="margin-top: 10px;">
115
+ </div>
116
+ '''
117
+ # Prompt examples
118
+ #prompt_examples = [
119
+ # "A blue jay standing on a large basket of rainbow macarons.",
120
+ # "A dog looking curiously in the mirror, seeing a cat.",
121
+ # "A robot couple fine dining with Eiffel Tower in the background.",
122
+ # "A chrome-plated duck with a golden beak arguing with an angry turtle in a forest.",
123
+ # "A transparent sculpture of a duck made out of glass. The sculpture is in front of a painting of a landscape.",
124
+ # "A cute corgi lives in a house made out of sushi.",
125
+ # "A single beam of light enter the room from the ceiling. The beam of light is illuminating an easel. On the easel there is a Rembrandt painting of a raccoon.",
126
+ # "A photo of a Corgi dog riding a bike in Times Square. It is wearing sunglasses and a beach hat."]
127
+
128
+
129
+
130
+ # Creating Gradio interface
131
+ with gr.Blocks(css=css) as demo:
132
+ gr.Markdown(PTI_SD_DESCRIPTION)
133
+ with gr.Row():
134
+ with gr.Column():
135
+ current_model = gr.Dropdown(label="Select Model", choices=list_models, value=list_models[1])
136
+ text_prompt = gr.Textbox(label="Input Prompt", placeholder="Example: A blue jay ", lines=2)
137
+ with gr.Column():
138
+ negative_prompt = gr.Textbox(label="Negative Prompt (optional)", placeholder="Example: blurry, unfocused", lines=2)
139
+ image_style = gr.Dropdown(label="Select Style", choices=["None style", "Cinematic", "Digital Art", "Portrait"], value="None style")
140
+
141
+ generate_button = gr.Button("Generate Image", variant='primary')
142
+
143
+ with gr.Row():
144
+ image_output = gr.Image(type="pil", label="Image Output")
145
+
146
+ generate_button.click(generate_txt2img, inputs=[current_model, text_prompt, negative_prompt, image_style], outputs=image_output)
147
+
148
+ # Launch the app
149
+ demo.launch()
150
 
 
 
151