Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -13,39 +13,57 @@ from datetime import datetime
|
|
13 |
|
14 |
import sqlite3
|
15 |
from datetime import datetime
|
16 |
-
|
|
|
17 |
|
18 |
# Initialize the database
|
19 |
def init_db(file='logs.db'):
|
20 |
conn = sqlite3.connect(file)
|
21 |
c = conn.cursor()
|
22 |
-
c.execute('''CREATE TABLE IF NOT EXISTS logs
|
23 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
24 |
conn.commit()
|
25 |
conn.close()
|
26 |
|
27 |
# Log a request
|
28 |
-
def log_request(prompt, is_negative, steps, cfg_scale, sampler, seed, strength, use_dev,
|
29 |
-
|
30 |
-
|
31 |
-
log_message += f"enhance_prompt_style='{enhance_prompt_style}', enhance_prompt_option={enhance_prompt_option}, "
|
32 |
-
log_message += f"nemo_enhance_prompt_style='{nemo_enhance_prompt_style}', use_mistral_nemo={use_mistral_nemo}"
|
33 |
-
if huggingface_api_key:
|
34 |
-
log_message += f"huggingface_api_key='{huggingface_api_key}'"
|
35 |
-
|
36 |
-
conn = sqlite3.connect('acces_log.log')
|
37 |
c = conn.cursor()
|
38 |
-
c.execute(
|
|
|
|
|
|
|
|
|
|
|
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://huggingface.co/spaces/K00B404/FLUX.1-Dev-Serverless-darn-enhanced-prompt/resolve/main/edge.png")
|
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 |
timeout = 100
|
48 |
-
|
|
|
|
|
49 |
|
50 |
# Set up logging
|
51 |
logging.basicConfig(filename='access.log', level=logging.INFO,
|
@@ -53,15 +71,6 @@ logging.basicConfig(filename='access.log', level=logging.INFO,
|
|
53 |
|
54 |
|
55 |
|
56 |
-
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):
|
57 |
-
log_message = f"Request: prompt='{prompt}', is_negative={is_negative}, steps={steps}, cfg_scale={cfg_scale}, "
|
58 |
-
log_message += f"sampler='{sampler}', seed={seed}, strength={strength}, use_dev={use_dev}, "
|
59 |
-
log_message += f"enhance_prompt_style='{enhance_prompt_style}', enhance_prompt_option={enhance_prompt_option}, "
|
60 |
-
log_message += f"nemo_enhance_prompt_style='{nemo_enhance_prompt_style}', use_mistral_nemo={use_mistral_nemo}"
|
61 |
-
if huggingface_api_key:
|
62 |
-
log_message += f"huggingface_api_key='{huggingface_api_key}'"
|
63 |
-
logging.info(log_message)
|
64 |
-
|
65 |
def check_ubuse(prompt,word_list=["little girl"]):
|
66 |
for word in word_list:
|
67 |
if word in prompt:
|
@@ -141,33 +150,36 @@ def mistral_nemo_call(prompt, API_TOKEN, model="mistralai/Mistral-Nemo-Instruct-
|
|
141 |
):
|
142 |
response += message.choices[0].delta.content
|
143 |
return response
|
144 |
-
|
145 |
-
|
146 |
-
|
147 |
-
|
148 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
149 |
api_url = API_URL_DEV if use_dev else API_URL
|
150 |
|
151 |
-
#
|
152 |
is_api_call = huggingface_api_key is not None
|
153 |
-
|
154 |
if is_api_call:
|
155 |
-
# Use
|
156 |
-
API_TOKEN = os.getenv("HF_READ_TOKEN")
|
157 |
else:
|
158 |
-
# Validate the API key if it's an API call
|
159 |
if huggingface_api_key == "":
|
160 |
raise gr.Error("API key is required for API calls.")
|
161 |
API_TOKEN = huggingface_api_key
|
162 |
|
163 |
headers = {"Authorization": f"Bearer {API_TOKEN}"}
|
164 |
|
165 |
-
if
|
166 |
return None, None, None
|
167 |
|
168 |
key = random.randint(0, 999)
|
169 |
prompt = check_ubuse(prompt)
|
170 |
-
#prompt = GoogleTranslator(source='ru', target='en').translate(prompt)
|
171 |
print(f'\033[1mGeneration {key} translation:\033[0m {prompt}')
|
172 |
|
173 |
original_prompt = prompt
|
@@ -181,7 +193,7 @@ def query(prompt, is_negative=False, steps=30, cfg_scale=7, sampler="DPM++ 2M Ka
|
|
181 |
final_prompt = f"{prompt} | ultra detail, ultra elaboration, ultra quality, perfect."
|
182 |
print(f'\033[1mGeneration {key}:\033[0m {final_prompt}')
|
183 |
|
184 |
-
#
|
185 |
if seed == -1:
|
186 |
seed = random.randint(1, 1000000000)
|
187 |
|
@@ -207,7 +219,7 @@ def query(prompt, is_negative=False, steps=30, cfg_scale=7, sampler="DPM++ 2M Ka
|
|
207 |
image = Image.open(io.BytesIO(image_bytes))
|
208 |
print(f'\033[1mGeneration {key} completed!\033[0m ({final_prompt})')
|
209 |
|
210 |
-
# Save the image
|
211 |
output_path = f"./output_{key}.png"
|
212 |
image.save(output_path)
|
213 |
|
@@ -215,8 +227,6 @@ def query(prompt, is_negative=False, steps=30, cfg_scale=7, sampler="DPM++ 2M Ka
|
|
215 |
except Exception as e:
|
216 |
print(f"Error when trying to open the image: {e}")
|
217 |
return None, None, None
|
218 |
-
|
219 |
-
|
220 |
|
221 |
title_html="""
|
222 |
<center>
|
@@ -431,7 +441,9 @@ with gr.Blocks( css=css) as app:
|
|
431 |
seed_output = gr.Textbox(label="Seed Used", elem_id="seed-output")
|
432 |
final_prompt_output = gr.Textbox(label="Final Prompt", elem_id="final-prompt-output")
|
433 |
|
|
|
|
|
434 |
# Adjust the click function to include the API key, use_dev, and enhance_prompt_option as inputs
|
435 |
-
text_button.click(query, inputs=
|
436 |
|
437 |
app.launch(show_api=True, share=False)
|
|
|
13 |
|
14 |
import sqlite3
|
15 |
from datetime import datetime
|
16 |
+
import sqlite3
|
17 |
+
from datetime import datetime
|
18 |
|
19 |
# Initialize the database
|
20 |
def init_db(file='logs.db'):
|
21 |
conn = sqlite3.connect(file)
|
22 |
c = conn.cursor()
|
23 |
+
c.execute('''CREATE TABLE IF NOT EXISTS logs (
|
24 |
+
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
25 |
+
timestamp TEXT,
|
26 |
+
prompt TEXT,
|
27 |
+
is_negative INTEGER,
|
28 |
+
steps INTEGER,
|
29 |
+
cfg_scale REAL,
|
30 |
+
sampler TEXT,
|
31 |
+
seed INTEGER,
|
32 |
+
strength REAL,
|
33 |
+
use_dev INTEGER,
|
34 |
+
enhance_prompt_style TEXT,
|
35 |
+
enhance_prompt_option INTEGER,
|
36 |
+
nemo_enhance_prompt_style TEXT,
|
37 |
+
use_mistral_nemo INTEGER
|
38 |
+
)''')
|
39 |
conn.commit()
|
40 |
conn.close()
|
41 |
|
42 |
# Log a request
|
43 |
+
def log_request(prompt, is_negative, steps, cfg_scale, sampler, seed, strength, use_dev,
|
44 |
+
enhance_prompt_style, enhance_prompt_option, nemo_enhance_prompt_style, use_mistral_nemo):
|
45 |
+
conn = sqlite3.connect('logs.db')
|
|
|
|
|
|
|
|
|
|
|
|
|
46 |
c = conn.cursor()
|
47 |
+
c.execute('''INSERT INTO logs (timestamp, prompt, is_negative, steps, cfg_scale, sampler, seed, strength, use_dev,
|
48 |
+
enhance_prompt_style, enhance_prompt_option, nemo_enhance_prompt_style, use_mistral_nemo)
|
49 |
+
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)''',
|
50 |
+
(datetime.now().isoformat(), prompt, is_negative, steps, cfg_scale, sampler, seed, strength,
|
51 |
+
use_dev, enhance_prompt_style, enhance_prompt_option, nemo_enhance_prompt_style, use_mistral_nemo))
|
52 |
+
|
53 |
conn.commit()
|
54 |
conn.close()
|
55 |
+
|
56 |
+
|
57 |
+
|
58 |
# os.makedirs('assets', exist_ok=True)
|
59 |
if not os.path.exists('icon.png'):
|
60 |
os.system("wget -O icon.png https://huggingface.co/spaces/K00B404/FLUX.1-Dev-Serverless-darn-enhanced-prompt/resolve/main/edge.png")
|
61 |
API_URL_DEV = "https://api-inference.huggingface.co/models/black-forest-labs/FLUX.1-dev"
|
62 |
API_URL = "https://api-inference.huggingface.co/models/black-forest-labs/FLUX.1-schnell"
|
63 |
timeout = 100
|
64 |
+
|
65 |
+
|
66 |
+
init_db()
|
67 |
|
68 |
# Set up logging
|
69 |
logging.basicConfig(filename='access.log', level=logging.INFO,
|
|
|
71 |
|
72 |
|
73 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
74 |
def check_ubuse(prompt,word_list=["little girl"]):
|
75 |
for word in word_list:
|
76 |
if word in prompt:
|
|
|
150 |
):
|
151 |
response += message.choices[0].delta.content
|
152 |
return response
|
153 |
+
|
154 |
+
|
155 |
+
def query(prompt, is_negative=False, steps=30, cfg_scale=7, sampler="DPM++ 2M Karras", seed=-1,
|
156 |
+
strength=0.7, huggingface_api_key=None, use_dev=False,
|
157 |
+
enhance_prompt_style="generic", enhance_prompt_option=False,
|
158 |
+
nemo_enhance_prompt_style="generic", use_mistral_nemo=False):
|
159 |
+
|
160 |
+
# Log the request (WITHOUT storing API keys)
|
161 |
+
log_request(prompt, is_negative, steps, cfg_scale, sampler, seed, strength, use_dev,
|
162 |
+
enhance_prompt_style, enhance_prompt_option, nemo_enhance_prompt_style, use_mistral_nemo)
|
163 |
+
|
164 |
+
# Determine API URL
|
165 |
api_url = API_URL_DEV if use_dev else API_URL
|
166 |
|
167 |
+
# API key handling
|
168 |
is_api_call = huggingface_api_key is not None
|
|
|
169 |
if is_api_call:
|
170 |
+
API_TOKEN = os.getenv("HF_READ_TOKEN") # Use env var in GUI mode
|
|
|
171 |
else:
|
|
|
172 |
if huggingface_api_key == "":
|
173 |
raise gr.Error("API key is required for API calls.")
|
174 |
API_TOKEN = huggingface_api_key
|
175 |
|
176 |
headers = {"Authorization": f"Bearer {API_TOKEN}"}
|
177 |
|
178 |
+
if not prompt: # Simplified check
|
179 |
return None, None, None
|
180 |
|
181 |
key = random.randint(0, 999)
|
182 |
prompt = check_ubuse(prompt)
|
|
|
183 |
print(f'\033[1mGeneration {key} translation:\033[0m {prompt}')
|
184 |
|
185 |
original_prompt = prompt
|
|
|
193 |
final_prompt = f"{prompt} | ultra detail, ultra elaboration, ultra quality, perfect."
|
194 |
print(f'\033[1mGeneration {key}:\033[0m {final_prompt}')
|
195 |
|
196 |
+
# Ensure seed is always positive
|
197 |
if seed == -1:
|
198 |
seed = random.randint(1, 1000000000)
|
199 |
|
|
|
219 |
image = Image.open(io.BytesIO(image_bytes))
|
220 |
print(f'\033[1mGeneration {key} completed!\033[0m ({final_prompt})')
|
221 |
|
222 |
+
# Save the image and return path
|
223 |
output_path = f"./output_{key}.png"
|
224 |
image.save(output_path)
|
225 |
|
|
|
227 |
except Exception as e:
|
228 |
print(f"Error when trying to open the image: {e}")
|
229 |
return None, None, None
|
|
|
|
|
230 |
|
231 |
title_html="""
|
232 |
<center>
|
|
|
441 |
seed_output = gr.Textbox(label="Seed Used", elem_id="seed-output")
|
442 |
final_prompt_output = gr.Textbox(label="Final Prompt", elem_id="final-prompt-output")
|
443 |
|
444 |
+
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]
|
445 |
+
|
446 |
# Adjust the click function to include the API key, use_dev, and enhance_prompt_option as inputs
|
447 |
+
text_button.click(query, inputs=inputs, outputs=[image_output, seed_output, final_prompt_output])
|
448 |
|
449 |
app.launch(show_api=True, share=False)
|