Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -41,8 +41,11 @@ def log_request(prompt, is_negative, steps, cfg_scale, sampler, seed, strength,
|
|
| 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,6 +54,14 @@ 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}, "
|
|
@@ -141,11 +152,17 @@ def mistral_nemo_call(prompt, API_TOKEN, model="mistralai/Mistral-Nemo-Instruct-
|
|
| 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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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,7 +183,6 @@ def query(prompt, is_negative=False, steps=30, cfg_scale=7, sampler="DPM++ 2M Ka
|
|
| 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,6 +200,17 @@ def query(prompt, is_negative=False, steps=30, cfg_scale=7, sampler="DPM++ 2M Ka
|
|
| 184 |
if seed == -1:
|
| 185 |
seed = random.randint(1, 1000000000)
|
| 186 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 187 |
payload = {
|
| 188 |
"inputs": final_prompt,
|
| 189 |
"is_negative": is_negative,
|
|
@@ -214,6 +241,7 @@ def query(prompt, is_negative=False, steps=30, cfg_scale=7, sampler="DPM++ 2M Ka
|
|
| 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,7 +304,6 @@ css1 = """
|
|
| 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,7 +332,8 @@ with gr.Blocks(theme='Nymbo/Nymbo_Theme', css=css) as app:
|
|
| 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,7 +342,8 @@ with gr.Blocks(theme='Nymbo/Nymbo_Theme', css=css) as app:
|
|
| 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 |
-
#
|
| 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,
|
|
|
|
| 319 |
|
| 320 |
app.launch(show_api=True, share=False)
|
|
|
|
| 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 |
+
|
| 45 |
API_URL_DEV = "https://api-inference.huggingface.co/models/black-forest-labs/FLUX.1-dev"
|
| 46 |
API_URL = "https://api-inference.huggingface.co/models/black-forest-labs/FLUX.1-schnell"
|
| 47 |
+
API_URL_UNHINGED = "https://api-inference.huggingface.co/models/enhanceaiteam/Flux-uncensored"
|
| 48 |
+
|
| 49 |
timeout = 100
|
| 50 |
init_db('acces_log.log')
|
| 51 |
|
|
|
|
| 54 |
format='%(asctime)s - %(message)s', datefmt='%Y-%m-%d %H:%M:%S')
|
| 55 |
|
| 56 |
|
| 57 |
+
# Function to query the uncensored API
|
| 58 |
+
def query_unhinged_api(prompt):
|
| 59 |
+
key = os.getenv('HUGGINGFACEHUB_API_TOKEN')
|
| 60 |
+
headers = {"Authorization": f"Bearer {key}"}
|
| 61 |
+
payload = {"inputs": prompt}
|
| 62 |
+
response = requests.post(API_URL_UNHINGED, headers=headers, json=payload)
|
| 63 |
+
return response.content
|
| 64 |
+
|
| 65 |
|
| 66 |
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):
|
| 67 |
log_message = f"Request: prompt='{prompt}', is_negative={is_negative}, steps={steps}, cfg_scale={cfg_scale}, "
|
|
|
|
| 152 |
response += message.choices[0].delta.content
|
| 153 |
return response
|
| 154 |
|
| 155 |
+
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, use_unhinged=False):
|
| 156 |
+
|
| 157 |
+
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)
|
| 158 |
|
|
|
|
| 159 |
# Determine which API URL to use
|
| 160 |
+
if use_unhinged:
|
| 161 |
+
api_url = API_URL_UNHINGED
|
| 162 |
+
elif use_dev:
|
| 163 |
+
api_url = API_URL_DEV
|
| 164 |
+
else:
|
| 165 |
+
api_url = 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
|
|
|
|
| 183 |
|
| 184 |
key = random.randint(0, 999)
|
| 185 |
prompt = check_ubuse(prompt)
|
|
|
|
| 186 |
print(f'\033[1mGeneration {key} translation:\033[0m {prompt}')
|
| 187 |
|
| 188 |
original_prompt = prompt
|
|
|
|
| 200 |
if seed == -1:
|
| 201 |
seed = random.randint(1, 1000000000)
|
| 202 |
|
| 203 |
+
# Use uncensored API if selected
|
| 204 |
+
if use_unhinged:
|
| 205 |
+
unhinged_gallery = 'unhinged_images'
|
| 206 |
+
os.makedirs(unhinged_gallery, exist_ok=True)
|
| 207 |
+
image_bytes = query_unhinged_api(prompt)
|
| 208 |
+
image = Image.open(io.BytesIO(image_bytes))
|
| 209 |
+
output_path = f"./{unhinged_gallery}/unhinged_output_{random.randint(0, 999)}.png"
|
| 210 |
+
print(output_path)
|
| 211 |
+
image.save(output_path)
|
| 212 |
+
return output_path, seed, final_prompt
|
| 213 |
+
|
| 214 |
payload = {
|
| 215 |
"inputs": final_prompt,
|
| 216 |
"is_negative": is_negative,
|
|
|
|
| 241 |
except Exception as e:
|
| 242 |
print(f"Error when trying to open the image: {e}")
|
| 243 |
return None, None, None
|
| 244 |
+
|
| 245 |
|
| 246 |
css = """
|
| 247 |
body {
|
|
|
|
| 304 |
font-weight: bold;
|
| 305 |
}
|
| 306 |
"""
|
|
|
|
| 307 |
with gr.Blocks(theme='Nymbo/Nymbo_Theme', css=css) as app:
|
| 308 |
gr.HTML("""
|
| 309 |
<center>
|
|
|
|
| 332 |
enhance_prompt_option = gr.Checkbox(label="Enhance Prompt", value=False, elem_id="enhance-prompt-checkbox")
|
| 333 |
use_mistral_nemo = gr.Checkbox(label="Use Mistral Nemo", value=False, elem_id="use-mistral-checkbox")
|
| 334 |
nemo_prompt_style = gr.Textbox(label="Nemo Enhance Prompt Style", placeholder="Enter style for the prompt enhancer here", elem_id="nemo-enhance-prompt-style")
|
| 335 |
+
use_unhinged = gr.Checkbox(label="Use Flux Unhinged API", value=False, elem_id="use-unhinged-checkbox")
|
| 336 |
+
|
| 337 |
with gr.Row():
|
| 338 |
text_button = gr.Button("Run", variant='primary', elem_id="gen-button")
|
| 339 |
with gr.Row():
|
|
|
|
| 342 |
seed_output = gr.Textbox(label="Seed Used", elem_id="seed-output")
|
| 343 |
final_prompt_output = gr.Textbox(label="Final Prompt", elem_id="final-prompt-output")
|
| 344 |
|
| 345 |
+
# Update the click function to include the use_unhinged option
|
| 346 |
+
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, nemo_prompt_style, use_mistral_nemo, use_unhinged], outputs=[image_output, seed_output, final_prompt_output])
|
| 347 |
+
|
| 348 |
|
| 349 |
app.launch(show_api=True, share=False)
|