Spaces:
Runtime error
Runtime error
Delete utils.py
#3
by
Baldpapi
- opened
utils.py
DELETED
|
@@ -1,87 +0,0 @@
|
|
| 1 |
-
import re
|
| 2 |
-
import base64
|
| 3 |
-
import html
|
| 4 |
-
from io import BytesIO
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
def image_to_base64(image):
|
| 8 |
-
# Convert the image to bytes
|
| 9 |
-
buffered = BytesIO()
|
| 10 |
-
image.save(buffered, format="PNG") # You can change format to PNG if needed
|
| 11 |
-
|
| 12 |
-
# Encode the bytes to base64
|
| 13 |
-
img_str = base64.b64encode(buffered.getvalue())
|
| 14 |
-
|
| 15 |
-
return img_str.decode('utf-8') # Convert bytes to string
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
def remove_id_and_ext(text):
|
| 19 |
-
text = re.sub(r'\[.*\]$', '', text)
|
| 20 |
-
extension = text[-12:].strip()
|
| 21 |
-
if extension == "safetensors":
|
| 22 |
-
text = text[:-13]
|
| 23 |
-
elif extension == "ckpt":
|
| 24 |
-
text = text[:-4]
|
| 25 |
-
return text
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
def extract_data(text):
|
| 29 |
-
results = {}
|
| 30 |
-
patterns = {
|
| 31 |
-
'prompt': r'(.*)',
|
| 32 |
-
'negative_prompt': r'Negative prompt: (.*)',
|
| 33 |
-
'steps': r'Steps: (\d+),',
|
| 34 |
-
'seed': r'Seed: (\d+),',
|
| 35 |
-
'sampler': r'Sampler:\s*([^\s,]+(?:\s+[^\s,]+)*)',
|
| 36 |
-
'model': r'Model:\s*([^\s,]+)',
|
| 37 |
-
'cfg_scale': r'CFG scale:\s*([\d\.]+)',
|
| 38 |
-
'size': r'Size:\s*([0-9]+x[0-9]+)'
|
| 39 |
-
}
|
| 40 |
-
for key in ['prompt', 'negative_prompt', 'steps', 'seed', 'sampler', 'model', 'cfg_scale', 'size']:
|
| 41 |
-
match = re.search(patterns[key], text)
|
| 42 |
-
if match:
|
| 43 |
-
results[key] = match.group(1)
|
| 44 |
-
else:
|
| 45 |
-
results[key] = None
|
| 46 |
-
if results['size'] is not None:
|
| 47 |
-
w, h = results['size'].split("x")
|
| 48 |
-
results['w'] = w
|
| 49 |
-
results['h'] = h
|
| 50 |
-
else:
|
| 51 |
-
results['w'] = None
|
| 52 |
-
results['h'] = None
|
| 53 |
-
return results
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
def place_lora(current_prompt, lora_name):
|
| 57 |
-
pattern = r"<lora:" + lora_name + r":.*?>"
|
| 58 |
-
|
| 59 |
-
if re.search(pattern, current_prompt):
|
| 60 |
-
return re.sub(pattern, "", current_prompt)
|
| 61 |
-
else:
|
| 62 |
-
return current_prompt + " <lora:" + lora_name + ":1> "
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
def plaintext_to_html(text, classname=None):
|
| 66 |
-
content = "<br>\n".join(html.escape(x) for x in text.split('\n'))
|
| 67 |
-
|
| 68 |
-
return f"<p class='{classname}'>{content}</p>" if classname else f"<p>{content}</p>"
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
def get_exif_data(image):
|
| 72 |
-
items = image.info
|
| 73 |
-
|
| 74 |
-
info = ''
|
| 75 |
-
for key, text in items.items():
|
| 76 |
-
info += f"""
|
| 77 |
-
<div>
|
| 78 |
-
<p><b>{plaintext_to_html(str(key))}</b></p>
|
| 79 |
-
<p>{plaintext_to_html(str(text))}</p>
|
| 80 |
-
</div>
|
| 81 |
-
""".strip() + "\n"
|
| 82 |
-
|
| 83 |
-
if len(info) == 0:
|
| 84 |
-
message = "Nothing found in the image."
|
| 85 |
-
info = f"<div><p>{message}<p></div>"
|
| 86 |
-
|
| 87 |
-
return info
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|