K00B404 commited on
Commit
0777cbf
β€’
1 Parent(s): d48de17

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +341 -40
app.py CHANGED
@@ -1,45 +1,346 @@
 
1
  import gradio as gr
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2
 
3
- # Custom CSS for the glowing effect
4
  css = """
5
- <style>
6
- .glowing-panel {
7
- position: absolute;
8
- top: calc(50% - 150px);
9
- left: calc(50% - 150px);
10
- width: 300px;
11
- height: 300px;
12
- border-radius: 50%;
13
- box-shadow:
14
- inset 0 0 50px #fff,
15
- inset 20px 0 80px #f0f,
16
- inset -20px 0 80px #0ff,
17
- inset 20px 0 300px #f0f,
18
- inset -20px 0 300px #0ff,
19
- 0 0 50px #fff,
20
- -10px 0 80px #f0f,
21
- 10px 0 80px #0ff;
22
- }
23
- </style>
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
24
  """
25
 
26
- def demo_function(input_text):
27
- return f"You entered: {input_text}"
28
-
29
- # Create a Gradio interface
30
- with gr.Blocks() as demo:
31
- gr.HTML(css) # Add custom CSS
32
- with gr.Row():
33
- with gr.Column():
34
- gr.Markdown("## Welcome to the Glowing Panel App")
35
- input_text = gr.Textbox(label="Enter something:")
36
- output_text = gr.Textbox(label="Output:", interactive=False)
37
- submit_button = gr.Button("Submit")
38
-
39
- submit_button.click(demo_function, inputs=input_text, outputs=output_text)
40
-
41
- # Create the glowing panel
42
- gr.HTML('<div class="glowing-panel"></div>')
43
-
44
- # Launch the app
45
- demo.launch()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+
2
  import gradio as gr
3
+ import requests
4
+ import io
5
+ import random
6
+ import os
7
+ from PIL import Image
8
+ from huggingface_hub import InferenceClient
9
+ from gradio_client import Client
10
+ import logging
11
+ from datetime import datetime
12
+
13
+ import sqlite3
14
+ from datetime import datetime
15
+
16
+
17
+ # Initialize the database
18
+ def init_db(file='logs.db'):
19
+ conn = sqlite3.connect(file)
20
+ c = conn.cursor()
21
+ c.execute('''CREATE TABLE IF NOT EXISTS logs
22
+ (timestamp TEXT, message TEXT)''')
23
+ conn.commit()
24
+ conn.close()
25
+
26
+ # Log a request
27
+ def log_request(prompt, is_negative, steps, cfg_scale, sampler, seed, strength, use_dev, enhance_prompt_style, enhance_prompt_option, nemo_enhance_prompt_style, use_mistral_nemo, huggingface_api_key):
28
+ log_message = f"Request: prompt='{prompt}', is_negative={is_negative}, steps={steps}, cfg_scale={cfg_scale}, "
29
+ log_message += f"sampler='{sampler}', seed={seed}, strength={strength}, use_dev={use_dev}, "
30
+ log_message += f"enhance_prompt_style='{enhance_prompt_style}', enhance_prompt_option={enhance_prompt_option}, "
31
+ log_message += f"nemo_enhance_prompt_style='{nemo_enhance_prompt_style}', use_mistral_nemo={use_mistral_nemo}"
32
+ if huggingface_api_key:
33
+ log_message += f"huggingface_api_key='{huggingface_api_key}'"
34
+
35
+ conn = sqlite3.connect('acces_log.log')
36
+ c = conn.cursor()
37
+ c.execute("INSERT INTO logs VALUES (?, ?)", (datetime.now().isoformat(), log_message))
38
+ conn.commit()
39
+ conn.close()
40
+
41
+ # os.makedirs('assets', exist_ok=True)
42
+ if not os.path.exists('icon.png'):
43
+ os.system("wget -O icon.png https://huggingface.co/spaces/K00B404/FLUX.1-Dev-Serverless-darn-enhanced-prompt/resolve/main/edge.png")
44
+ API_URL_DEV = "https://api-inference.huggingface.co/models/black-forest-labs/FLUX.1-dev"
45
+ API_URL = "https://api-inference.huggingface.co/models/black-forest-labs/FLUX.1-schnell"
46
+ timeout = 100
47
+ init_db('acces_log.log')
48
+ client = Client("https://https://huggingface.co/spaces/K00B404/SnelleJelle")
49
+ # Set up logging
50
+ logging.basicConfig(filename='access.log', level=logging.INFO,
51
+ format='%(asctime)s - %(message)s', datefmt='%Y-%m-%d %H:%M:%S')
52
+
53
+
54
+
55
+ def log_requestold(prompt, is_negative, steps, cfg_scale, sampler, seed, strength, use_dev, enhance_prompt_style, enhance_prompt_option, nemo_enhance_prompt_style, use_mistral_nemo, huggingface_api_key):
56
+ log_message = f"Request: prompt='{prompt}', is_negative={is_negative}, steps={steps}, cfg_scale={cfg_scale}, "
57
+ log_message += f"sampler='{sampler}', seed={seed}, strength={strength}, use_dev={use_dev}, "
58
+ log_message += f"enhance_prompt_style='{enhance_prompt_style}', enhance_prompt_option={enhance_prompt_option}, "
59
+ log_message += f"nemo_enhance_prompt_style='{nemo_enhance_prompt_style}', use_mistral_nemo={use_mistral_nemo}"
60
+ if huggingface_api_key:
61
+ log_message += f"huggingface_api_key='{huggingface_api_key}'"
62
+ logging.info(log_message)
63
+
64
+
65
+ def check_ubuse(prompt,word_list=["little girl"]):
66
+ for word in word_list:
67
+ if word in prompt:
68
+ print(f"Abuse! prompt {prompt} wiped!")
69
+ return "None"
70
+ return prompt
71
+
72
+
73
+ def enhance_prompt(prompt, style="photo-realistic"):
74
+ system_message = f"""
75
+ You are an image generation prompt enhancer specialized in the {style} style.
76
+ You must respond only with the enhanced version of the user's input prompt.
77
+ Remember, image generation models can be stimulated by referring to camera 'effects' in the prompt like: 4k, award-winning, super details, 35mm lens, hd
78
+ """
79
+
80
+ result = client.predict(
81
+ message=prompt,
82
+ system_message=system_message,
83
+ max_tokens=512,
84
+ temperature=0.7,
85
+ top_p=0.95,
86
+ api_name="/chat"
87
+ )
88
+ return result
89
+
90
+
91
+
92
+ def enhance_prompt(prompt, model="mistralai/Mistral-Nemo-Instruct-2407", style="photo-realistic"):
93
+ system_prompt=f"""
94
+ You are a image generation prompt enhancer specialized in the {style} style.
95
+ You must respond only with the enhanced version of the users input prompt
96
+ Remember, image generation models can be stimulated by refering to camera 'effect' in the prompt like :4k ,award winning, super details, 35mm lens, hd
97
+ """
98
+
99
+ result = client.predict(
100
+ system_prompt=system_prompt,
101
+ user_message=user_message,
102
+ max_tokens=256,
103
+ model_id=model,# "mistralai/Mistral-Nemo-Instruct-2407",
104
+ api_name="/chat"
105
+ )
106
+ return result
107
+
108
+ # The output value that appears in the "Response" Textbox component.
109
+ """result = client.predict(
110
+ system_prompt=system_prompt,#"You are a image generation prompt enhancer and must respond only with the enhanced version of the users input prompt",
111
+ user_message=user_message,
112
+ max_tokens=500,
113
+ api_name="/predict"
114
+ )
115
+ return result
116
+ """
117
+
118
+
119
+ def enhance_prompt_v2(prompt, model="mistralai/Mistral-Nemo-Instruct-2407", style="photo-realistic"):
120
+
121
+ client = Client("K00B404/Mistral-Nemo-custom")
122
+
123
+ system_prompt=f"""
124
+ You are a image generation prompt enhancer specialized in the {style} style.
125
+ You must respond only with the enhanced version of the users input prompt
126
+ Remember, image generation models can be stimulated by refering to camera 'effect' in the prompt like :4k ,award winning, super details, 35mm lens, hd
127
+ """
128
+ user_message=f"###input image generation prompt### {prompt}"
129
+
130
+ result = client.predict(
131
+ system_prompt=system_prompt,
132
+ user_message=user_message,
133
+ max_tokens=256,
134
+ model_id=model,
135
+ api_name="/predict"
136
+ )
137
+ return result
138
+
139
+
140
+ def mistral_nemo_call(prompt, API_TOKEN, model="mistralai/Mistral-Nemo-Instruct-2407", style="photo-realistic"):
141
+
142
+ client = InferenceClient(api_key=API_TOKEN)
143
+ system_prompt=f"""
144
+ You are a image generation prompt enhancer specialized in the {style} style.
145
+ You must respond only with the enhanced version of the users input prompt
146
+ Remember, image generation models can be stimulated by refering to camera 'effect' in the prompt like :4k ,award winning, super details, 35mm lens, hd
147
+ """
148
+
149
+ response = ""
150
+ for message in client.chat_completion(
151
+ model=model,
152
+ messages=[{"role": "system", "content": system_prompt},
153
+ {"role": "user", "content": prompt}
154
+ ],
155
+ max_tokens=500,
156
+ stream=True,
157
+ ):
158
+ response += message.choices[0].delta.content
159
+ return response
160
+
161
+ def query(prompt, is_negative=False, steps=30, cfg_scale=7, sampler="DPM++ 2M Karras", seed=-1, strength=0.7, huggingface_api_key=None, use_dev=False,enhance_prompt_style="generic", enhance_prompt_option=False, nemo_enhance_prompt_style="generic", use_mistral_nemo=False):
162
+
163
+ log_request(prompt, is_negative, steps, cfg_scale, sampler, seed, strength, use_dev, enhance_prompt_style, enhance_prompt_option, nemo_enhance_prompt_style, use_mistral_nemo, huggingface_api_key)
164
+ # Determine which API URL to use
165
+ api_url = API_URL_DEV if use_dev else API_URL
166
+
167
+ # Check if the request is an API call by checking for the presence of the huggingface_api_key
168
+ is_api_call = huggingface_api_key is not None
169
+
170
+ if is_api_call:
171
+ # Use the environment variable for the API key in GUI mode
172
+ API_TOKEN = os.getenv("HF_READ_TOKEN")
173
+ else:
174
+ # Validate the API key if it's an API call
175
+ if huggingface_api_key == "":
176
+ raise gr.Error("API key is required for API calls.")
177
+ API_TOKEN = huggingface_api_key
178
+
179
+ headers = {"Authorization": f"Bearer {API_TOKEN}"}
180
+
181
+ if prompt == "" or prompt is None:
182
+ return None, None, None
183
+
184
+ key = random.randint(0, 999)
185
+ prompt = check_ubuse(prompt)
186
+ #prompt = GoogleTranslator(source='ru', target='en').translate(prompt)
187
+ print(f'\033[1mGeneration {key} translation:\033[0m {prompt}')
188
+
189
+ original_prompt = prompt
190
+ if enhance_prompt_option:
191
+ prompt = enhance_prompt_v2(prompt, style=enhance_prompt_style)
192
+ print(f'\033[1mGeneration {key} enhanced prompt:\033[0m {prompt}')
193
+ if use_mistral_nemo:
194
+ prompt = mistral_nemo_call(prompt, API_TOKEN=API_TOKEN, style=nemo_enhance_prompt_style)
195
+ print(f'\033[1mGeneration {key} Mistral-Nemo prompt:\033[0m {prompt}')
196
+
197
+ final_prompt = f"{prompt} | ultra detail, ultra elaboration, ultra quality, perfect."
198
+ print(f'\033[1mGeneration {key}:\033[0m {final_prompt}')
199
+
200
+ # If seed is -1, generate a random seed and use it
201
+ if seed == -1:
202
+ seed = random.randint(1, 1000000000)
203
+
204
+ payload = {
205
+ "inputs": final_prompt,
206
+ "is_negative": is_negative,
207
+ "steps": steps,
208
+ "cfg_scale": cfg_scale,
209
+ "seed": seed,
210
+ "strength": strength
211
+ }
212
+
213
+ response = requests.post(api_url, headers=headers, json=payload, timeout=timeout)
214
+ if response.status_code != 200:
215
+ print(f"Error: Failed to get image. Response status: {response.status_code}")
216
+ print(f"Response content: {response.text}")
217
+ if response.status_code == 503:
218
+ raise gr.Error(f"{response.status_code} : The model is being loaded")
219
+ raise gr.Error(f"{response.status_code}")
220
+
221
+ try:
222
+ image_bytes = response.content
223
+ image = Image.open(io.BytesIO(image_bytes))
224
+ print(f'\033[1mGeneration {key} completed!\033[0m ({final_prompt})')
225
+
226
+ # Save the image to a file and return the file path and seed
227
+ output_path = f"./output_{key}.png"
228
+ image.save(output_path)
229
+
230
+ return output_path, seed, prompt if enhance_prompt_option else original_prompt
231
+ except Exception as e:
232
+ print(f"Error when trying to open the image: {e}")
233
+ return None, None, None
234
+
235
+
236
+
237
+ title_html="""
238
+ <center>
239
+ <div id="title-container">
240
+ <h1 id="title-text">FLUX Capacitor</h1>
241
+ </div>
242
+ </center>
243
+ """
244
 
 
245
  css = """
246
+ .gradio-container {
247
+ background: url(https://huggingface.co/spaces/K00B404/FLUX.1-Dev-Serverless-darn-enhanced-prompt/resolve/main/edge.png);
248
+ background-size: 900px 880px;
249
+ background-repeat: no-repeat;
250
+ background-position: center;
251
+ background-attachment: fixed;
252
+ color:#000;
253
+ }
254
+ .dark\:bg-gray-950:is(.dark *) {
255
+ --tw-bg-opacity: 1;
256
+ background-color: rgb(157, 17, 142);
257
+ }
258
+ .gradio-container-4-41-0 .prose :last-child {
259
+ margin-top: 8px !important;
260
+ }
261
+ .gradio-container-4-41-0 .prose :last-child {
262
+ margin-bottom: -7px !important;
263
+ }
264
+ .dark {
265
+ --button-primary-background-fill: #09e60d70;
266
+ --button-primary-background-fill-hover: #00000070;
267
+ --background-fill-primary: #000;
268
+ --background-fill-secondary: #000;
269
+ }
270
+ .hide-container {
271
+ margin-top;-2px;
272
+ }
273
+ #app-container3 {
274
+ background-color: rgba(255, 255, 255, 0.001); /* Corrected to make semi-transparent */
275
+ max-width: 600px;
276
+ margin-left: auto;
277
+ margin-right: auto;
278
+ margin-bottom: 10px;
279
+ border-radius: 125px;
280
+ box-shadow: 0 0 10px rgba(0,0,0,0.1); /* Adjusted shadow opacity */
281
+ }
282
+ #app-container {
283
+ background-color: rgba(255, 255, 255, 0.001); /* Semi-transparent background */
284
+ max-width: 600px;
285
+ margin: 0 auto; /* Center horizontally */
286
+ padding-bottom: 10px;
287
+ border-radius: 25px;
288
+ box-shadow: 0 0 10px rgba(0, 0, 0, 0.1); /* Adjusted shadow opacity */
289
+ }
290
+ #title-container {
291
+ display: flex;
292
+ align-items: center
293
+ margin-bottom:10px;
294
+ justify-content: center;
295
+ }
296
+ #title-icon {
297
+ width: 32px;
298
+ height: auto;
299
+ margin-right: 10px;
300
+ }
301
+ #title-text {
302
+ font-size: 30px;
303
+ font-weight: bold;
304
+ color: #000;
305
+ }
306
  """
307
 
308
+
309
+ with gr.Blocks(theme='Nymbo/Nymbo_Theme', css=css) as app:
310
+
311
+
312
+
313
+ gr.HTML(title_html) # title html
314
+
315
+ with gr.Column(elem_id="app-container"):
316
+ with gr.Row():
317
+ with gr.Column(elem_id="prompt-container"):
318
+ with gr.Row():
319
+ text_prompt = gr.Textbox(label="Prompt", placeholder="Enter a prompt here", lines=2, elem_id="prompt-text-input")
320
+ with gr.Row():
321
+ with gr.Accordion("Advanced Settings", open=False):
322
+ negative_prompt = gr.Textbox(label="Negative Prompt", placeholder="What should not be in the image", value="(deformed, distorted, disfigured), poorly drawn, bad anatomy, wrong anatomy, extra limb, missing limb, floating limbs, (mutated hands and fingers), disconnected limbs, mutation, mutated, ugly, disgusting, blurry, amputation, misspellings, typos", lines=3, elem_id="negative-prompt-text-input")
323
+ steps = gr.Slider(label="Sampling steps", value=35, minimum=1, maximum=100, step=1)
324
+ cfg = gr.Slider(label="CFG Scale", value=7, minimum=1, maximum=20, step=1)
325
+ method = gr.Radio(label="Sampling method", value="DPM++ 2M Karras", choices=["DPM++ 2M Karras", "DPM++ SDE Karras", "Euler", "Euler a", "Heun", "DDIM"])
326
+ strength = gr.Slider(label="Strength", value=0.7, minimum=0, maximum=1, step=0.001)
327
+ seed = gr.Slider(label="Seed", value=-1, minimum=-1, maximum=1000000000, step=1)
328
+ huggingface_api_key = gr.Textbox(label="Hugging Face API Key (required for API calls)", placeholder="Enter your Hugging Face API Key here", type="password", elem_id="api-key")
329
+ use_dev = gr.Checkbox(label="Use Dev API", value=False, elem_id="use-dev-checkbox")
330
+ enhance_prompt_style = gr.Textbox(label="Enhance Prompt Style", placeholder="Enter style for the prompt enhancer here", elem_id="enhance-prompt-style")
331
+ enhance_prompt_option = gr.Checkbox(label="Enhance Prompt", value=False, elem_id="enhance-prompt-checkbox")
332
+ use_mistral_nemo = gr.Checkbox(label="Use Mistral Nemo", value=False, elem_id="use-mistral-checkbox")
333
+ nemo_prompt_style = gr.Textbox(label="Nemo Enhance Prompt Style", placeholder="Enter style for the prompt enhancer here", elem_id="nemo-enhance-prompt-style")
334
+
335
+ with gr.Row():
336
+ text_button = gr.Button("Run", variant='primary', elem_id="gen-button")
337
+ with gr.Row():
338
+ image_output = gr.Image(type="pil", label="Image Output", elem_id="gallery")
339
+ with gr.Row():
340
+ seed_output = gr.Textbox(label="Seed Used", elem_id="seed-output")
341
+ final_prompt_output = gr.Textbox(label="Final Prompt", elem_id="final-prompt-output")
342
+
343
+ # Adjust the click function to include the API key, use_dev, and enhance_prompt_option as inputs
344
+ text_button.click(query, inputs=[text_prompt, negative_prompt, steps, cfg, method, seed, strength, huggingface_api_key, use_dev, enhance_prompt_style,enhance_prompt_option, enhance_prompt_style, use_mistral_nemo], outputs=[image_output, seed_output, final_prompt_output])
345
+
346
+ app.launch(show_api=True, share=False)