chokiproai commited on
Commit
1a79408
1 Parent(s): f0a844c

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +189 -0
app.py ADDED
@@ -0,0 +1,189 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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", "OpenDalle",
14
+ "OpenDalle V1.1", "Playground v2 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 == "OpenDalle":
45
+ API_URL = "https://api-inference.huggingface.co/models/dataautogpt3/OpenDalle"
46
+ elif current_model == "OpenDalle V1.1":
47
+ API_URL = "https://api-inference.huggingface.co/models/dataautogpt3/OpenDalleV1.1"
48
+ elif current_model == "Playground v2 1024px aesthetic":
49
+ API_URL = "https://api-inference.huggingface.co/models/playgroundai/playground-v2-1024px-aesthetic"
50
+
51
+
52
+ API_TOKEN = os.environ.get("HF_READ_TOKEN")
53
+ headers = {"Authorization": f"Bearer {API_TOKEN}"}
54
+
55
+
56
+ if image_style == "None style":
57
+ payload = {
58
+ "inputs": prompt + ", 8k",
59
+ "is_negative": is_negative,
60
+ "steps": steps,
61
+ "cfg_scale": cfg_scale,
62
+ "seed": seed if seed is not None else random.randint(-1, 2147483647)
63
+ }
64
+ elif image_style == "Cinematic":
65
+ payload = {
66
+ "inputs": prompt + ", realistic, detailed, textured, skin, hair, eyes, by Alex Huguet, Mike Hill, Ian Spriggs, JaeCheol Park, Marek Denko",
67
+ "is_negative": is_negative + ", abstract, cartoon, stylized",
68
+ "steps": steps,
69
+ "cfg_scale": cfg_scale,
70
+ "seed": seed if seed is not None else random.randint(-1, 2147483647)
71
+ }
72
+ elif image_style == "Digital Art":
73
+ payload = {
74
+ "inputs": prompt + ", faded , vintage , nostalgic , by Jose Villa , Elizabeth Messina , Ryan Brenizer , Jonas Peterson , Jasmine Star",
75
+ "is_negative": is_negative + ", sharp , modern , bright",
76
+ "steps": steps,
77
+ "cfg_scale": cfg_scale,
78
+ "seed": seed if seed is not None else random.randint(-1, 2147483647)
79
+ }
80
+ elif image_style == "Portrait":
81
+ payload = {
82
+ "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)",
83
+ "is_negative": is_negative,
84
+ "steps": steps,
85
+ "cfg_scale": cfg_scale,
86
+ "seed": seed if seed is not None else random.randint(-1, 2147483647)
87
+ }
88
+
89
+ image_bytes = requests.post(API_URL, headers=headers, json=payload).content
90
+ image = Image.open(io.BytesIO(image_bytes))
91
+ return image
92
+
93
+ css = """
94
+ /* General Container Styles */
95
+ .gradio-container {
96
+ font-family: 'IBM Plex Sans', sans-serif;
97
+ max-width: 730px !important;
98
+ margin: auto;
99
+ padding-top: 1.5rem;
100
+ text-align: center; /* Center the content horizontally */
101
+ }
102
+ /* Button Styles */
103
+ .gr-button {
104
+ color: white;
105
+ background: #007bff; /* Use a primary color for the background */
106
+ white-space: nowrap;
107
+ border: none;
108
+ padding: 10px 20px;
109
+ border-radius: 8px;
110
+ cursor: pointer;
111
+ transition: background-color 0.3s, color 0.3s;
112
+ }
113
+ .gr-button:hover {
114
+ background-color: #0056b3; /* Darken the background color on hover */
115
+ }
116
+ /* Share Button Styles */
117
+ #share-btn-container {
118
+ padding: 0.5rem !important;
119
+ background-color: #007bff; /* Use a primary color for the background */
120
+ justify-content: center;
121
+ align-items: center;
122
+ border-radius: 9999px !important;
123
+ max-width: 13rem;
124
+ margin: 0 auto; /* Center the container horizontally */
125
+ transition: background-color 0.3s;
126
+ }
127
+ #share-btn-container:hover {
128
+ background-color: #0056b3; /* Darken the background color on hover */
129
+ }
130
+ #share-btn {
131
+ all: initial;
132
+ color: #ffffff;
133
+ font-weight: 600;
134
+ cursor: pointer;
135
+ font-family: 'IBM Plex Sans', sans-serif;
136
+ margin: 0.5rem !important;
137
+ padding: 0.5rem !important;
138
+ }
139
+ /* Other Styles */
140
+ #gallery {
141
+ min-height: 22rem;
142
+ margin: auto; /* Center the gallery horizontally */
143
+ border-bottom-right-radius: 0.5rem !important;
144
+ border-bottom-left-radius: 0.5rem !important;
145
+ }
146
+ /* Centered Container for the Image */
147
+ .image-container {
148
+ max-width: 100%; /* Set the maximum width for the container */
149
+ margin: auto; /* Center the container horizontally */
150
+ padding: 20px; /* Add padding for spacing */
151
+ border: 1px solid #ccc; /* Add a subtle border to the container */
152
+ border-radius: 10px;
153
+ overflow: hidden; /* Hide overflow if the image is larger */
154
+ max-height: 22rem; /* Set a maximum height for the container */
155
+ }
156
+ /* Set a fixed size for the image */
157
+ .image-container img {
158
+ max-width: 100%; /* Ensure the image fills the container */
159
+ height: auto; /* Maintain aspect ratio */
160
+ max-height: 100%; /* Set a maximum height for the image */
161
+ border-radius: 10px;
162
+ box-shadow: 0px 2px 4px rgba(0, 0, 0, 0.2);
163
+ }
164
+ """
165
+
166
+ # Creating Gradio interface
167
+ with gr.Blocks(css=css) as demo:
168
+
169
+ with gr.Row():
170
+ with gr.Column():
171
+ gr.Markdown("<h1>AI Diffusion</h1>")
172
+ current_model = gr.Dropdown(label="Select Model", choices=list_models, value=list_models[1])
173
+ text_prompt = gr.Textbox(label="Enter Prompt", placeholder="Example: a cute dog", lines=2)
174
+ generate_button = gr.Button("Generate Image", variant='primary')
175
+
176
+ with gr.Column():
177
+ gr.Markdown("<h4>Advanced Settings</h4>")
178
+ with gr.Accordion("Advanced Customizations", open=False):
179
+ negative_prompt = gr.Textbox(label="Negative Prompt (Optional)", placeholder="Example: blurry, unfocused", lines=2)
180
+ image_style = gr.Dropdown(label="Select Style", choices=["None style", "Cinematic", "Digital Art", "Portrait"], value="None style")
181
+ # Add more options if needed
182
+
183
+ with gr.Row():
184
+ image_output = gr.Image(type="pil", label="Output Image")
185
+
186
+ generate_button.click(generate_txt2img, inputs=[current_model, text_prompt, negative_prompt, image_style], outputs=image_output)
187
+
188
+ # Launch the app
189
+ demo.launch()