Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -24,29 +24,25 @@ def init_db(file='logs.db'):
|
|
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 |
-
log_message += f"use_unhinged={use_unhinged}"
|
33 |
if huggingface_api_key:
|
34 |
-
log_message += f"
|
35 |
|
36 |
conn = sqlite3.connect('acces_log.log')
|
37 |
c = conn.cursor()
|
38 |
c.execute("INSERT INTO logs VALUES (?, ?)", (datetime.now().isoformat(), log_message))
|
39 |
conn.commit()
|
40 |
conn.close()
|
41 |
-
|
42 |
# os.makedirs('assets', exist_ok=True)
|
43 |
if not os.path.exists('icon.png'):
|
44 |
os.system("wget -O icon.png https://i.pinimg.com/564x/64/49/88/644988c59447eb00286834c2e70fdd6b.jpg")
|
45 |
-
|
46 |
API_URL_DEV = "https://api-inference.huggingface.co/models/black-forest-labs/FLUX.1-dev"
|
47 |
API_URL = "https://api-inference.huggingface.co/models/black-forest-labs/FLUX.1-schnell"
|
48 |
-
API_URL_UNHINGED = "https://api-inference.huggingface.co/models/enhanceaiteam/Flux-uncensored"
|
49 |
-
|
50 |
timeout = 100
|
51 |
init_db('acces_log.log')
|
52 |
|
@@ -55,14 +51,6 @@ logging.basicConfig(filename='access.log', level=logging.INFO,
|
|
55 |
format='%(asctime)s - %(message)s', datefmt='%Y-%m-%d %H:%M:%S')
|
56 |
|
57 |
|
58 |
-
# Function to query the uncensored API
|
59 |
-
def query_unhinged_api(prompt):
|
60 |
-
key = os.getenv('HUGGINGFACEHUB_API_TOKEN')
|
61 |
-
headers = {"Authorization": f"Bearer {key}"}
|
62 |
-
payload = {"inputs": prompt}
|
63 |
-
response = requests.post(API_URL_UNHINGED, headers=headers, json=payload)
|
64 |
-
return response.content
|
65 |
-
|
66 |
|
67 |
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):
|
68 |
log_message = f"Request: prompt='{prompt}', is_negative={is_negative}, steps={steps}, cfg_scale={cfg_scale}, "
|
@@ -153,17 +141,11 @@ def mistral_nemo_call(prompt, API_TOKEN, model="mistralai/Mistral-Nemo-Instruct-
|
|
153 |
response += message.choices[0].delta.content
|
154 |
return response
|
155 |
|
156 |
-
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,
|
157 |
-
|
158 |
-
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, use_unhinged)
|
159 |
|
|
|
160 |
# Determine which API URL to use
|
161 |
-
if
|
162 |
-
api_url = API_URL_UNHINGED
|
163 |
-
elif use_dev:
|
164 |
-
api_url = API_URL_DEV
|
165 |
-
else:
|
166 |
-
api_url = API_URL
|
167 |
|
168 |
# Check if the request is an API call by checking for the presence of the huggingface_api_key
|
169 |
is_api_call = huggingface_api_key is not None
|
@@ -184,6 +166,7 @@ def query(prompt, is_negative=False, steps=30, cfg_scale=7, sampler="DPM++ 2M Ka
|
|
184 |
|
185 |
key = random.randint(0, 999)
|
186 |
prompt = check_ubuse(prompt)
|
|
|
187 |
print(f'\033[1mGeneration {key} translation:\033[0m {prompt}')
|
188 |
|
189 |
original_prompt = prompt
|
@@ -201,17 +184,6 @@ def query(prompt, is_negative=False, steps=30, cfg_scale=7, sampler="DPM++ 2M Ka
|
|
201 |
if seed == -1:
|
202 |
seed = random.randint(1, 1000000000)
|
203 |
|
204 |
-
# Use uncensored API if selected
|
205 |
-
if use_unhinged:
|
206 |
-
unhinged_gallery = 'unhinged_images'
|
207 |
-
os.makedirs(unhinged_gallery, exist_ok=True)
|
208 |
-
image_bytes = query_unhinged_api(prompt)
|
209 |
-
image = Image.open(io.BytesIO(image_bytes))
|
210 |
-
output_path = f"./{unhinged_gallery}/unhinged_output_{random.randint(0, 999)}.png"
|
211 |
-
print(output_path)
|
212 |
-
image.save(output_path)
|
213 |
-
return output_path, seed, final_prompt
|
214 |
-
|
215 |
payload = {
|
216 |
"inputs": final_prompt,
|
217 |
"is_negative": is_negative,
|
@@ -242,7 +214,6 @@ def query(prompt, is_negative=False, steps=30, cfg_scale=7, sampler="DPM++ 2M Ka
|
|
242 |
except Exception as e:
|
243 |
print(f"Error when trying to open the image: {e}")
|
244 |
return None, None, None
|
245 |
-
|
246 |
|
247 |
css = """
|
248 |
body {
|
@@ -305,6 +276,7 @@ css1 = """
|
|
305 |
font-weight: bold;
|
306 |
}
|
307 |
"""
|
|
|
308 |
with gr.Blocks(theme='Nymbo/Nymbo_Theme', css=css) as app:
|
309 |
gr.HTML("""
|
310 |
<center>
|
@@ -333,8 +305,7 @@ with gr.Blocks(theme='Nymbo/Nymbo_Theme', css=css) as app:
|
|
333 |
enhance_prompt_option = gr.Checkbox(label="Enhance Prompt", value=False, elem_id="enhance-prompt-checkbox")
|
334 |
use_mistral_nemo = gr.Checkbox(label="Use Mistral Nemo", value=False, elem_id="use-mistral-checkbox")
|
335 |
nemo_prompt_style = gr.Textbox(label="Nemo Enhance Prompt Style", placeholder="Enter style for the prompt enhancer here", elem_id="nemo-enhance-prompt-style")
|
336 |
-
|
337 |
-
|
338 |
with gr.Row():
|
339 |
text_button = gr.Button("Run", variant='primary', elem_id="gen-button")
|
340 |
with gr.Row():
|
@@ -343,8 +314,7 @@ with gr.Blocks(theme='Nymbo/Nymbo_Theme', css=css) as app:
|
|
343 |
seed_output = gr.Textbox(label="Seed Used", elem_id="seed-output")
|
344 |
final_prompt_output = gr.Textbox(label="Final Prompt", elem_id="final-prompt-output")
|
345 |
|
346 |
-
#
|
347 |
-
text_button.click(query, inputs=[text_prompt, negative_prompt, steps, cfg, method, seed, strength, huggingface_api_key, use_dev, enhance_prompt_style,
|
348 |
-
|
349 |
|
350 |
app.launch(show_api=True, share=False)
|
|
|
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://i.pinimg.com/564x/64/49/88/644988c59447eb00286834c2e70fdd6b.jpg")
|
|
|
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 |
|
|
|
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}, "
|
|
|
141 |
response += message.choices[0].delta.content
|
142 |
return response
|
143 |
|
144 |
+
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):
|
|
|
|
|
145 |
|
146 |
+
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)
|
147 |
# Determine which API URL to use
|
148 |
+
api_url = API_URL_DEV if use_dev else API_URL
|
|
|
|
|
|
|
|
|
|
|
149 |
|
150 |
# Check if the request is an API call by checking for the presence of the huggingface_api_key
|
151 |
is_api_call = huggingface_api_key is not None
|
|
|
166 |
|
167 |
key = random.randint(0, 999)
|
168 |
prompt = check_ubuse(prompt)
|
169 |
+
#prompt = GoogleTranslator(source='ru', target='en').translate(prompt)
|
170 |
print(f'\033[1mGeneration {key} translation:\033[0m {prompt}')
|
171 |
|
172 |
original_prompt = prompt
|
|
|
184 |
if seed == -1:
|
185 |
seed = random.randint(1, 1000000000)
|
186 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
187 |
payload = {
|
188 |
"inputs": final_prompt,
|
189 |
"is_negative": is_negative,
|
|
|
214 |
except Exception as e:
|
215 |
print(f"Error when trying to open the image: {e}")
|
216 |
return None, None, None
|
|
|
217 |
|
218 |
css = """
|
219 |
body {
|
|
|
276 |
font-weight: bold;
|
277 |
}
|
278 |
"""
|
279 |
+
|
280 |
with gr.Blocks(theme='Nymbo/Nymbo_Theme', css=css) as app:
|
281 |
gr.HTML("""
|
282 |
<center>
|
|
|
305 |
enhance_prompt_option = gr.Checkbox(label="Enhance Prompt", value=False, elem_id="enhance-prompt-checkbox")
|
306 |
use_mistral_nemo = gr.Checkbox(label="Use Mistral Nemo", value=False, elem_id="use-mistral-checkbox")
|
307 |
nemo_prompt_style = gr.Textbox(label="Nemo Enhance Prompt Style", placeholder="Enter style for the prompt enhancer here", elem_id="nemo-enhance-prompt-style")
|
308 |
+
|
|
|
309 |
with gr.Row():
|
310 |
text_button = gr.Button("Run", variant='primary', elem_id="gen-button")
|
311 |
with gr.Row():
|
|
|
314 |
seed_output = gr.Textbox(label="Seed Used", elem_id="seed-output")
|
315 |
final_prompt_output = gr.Textbox(label="Final Prompt", elem_id="final-prompt-output")
|
316 |
|
317 |
+
# Adjust the click function to include the API key, use_dev, and enhance_prompt_option as inputs
|
318 |
+
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])
|
|
|
319 |
|
320 |
app.launch(show_api=True, share=False)
|