manhdo's picture
first commit
c452620
Raw History Blame Contribute Delete
3.16 kB
import base64
import os
import numpy as np
import random
import json
import io
import emoji
from PIL import Image
from pathlib import Path
from typing import List
from io import BytesIO
import streamlit as st
def get_files_in_dir(path: Path) -> List[str]:
files = []
for file in os.listdir(path):
if os.path.isfile(os.path.join(path, file)):
files.append(file)
return files
def get_random_img(img_names: List[str]) -> str:
return random.choice(img_names)
def process_uploaded_file(uploaded_file):
# Convert the uploaded file to a PIL Image
pil_image = Image.open(uploaded_file)
return pil_image
def center_image(image_np):
# Convert the uploaded file to a PIL Image
image = Image.fromarray(image_np.astype('uint8'), 'RGB')
# Display the image in the middle
st.markdown(
f'<div style="display: flex; justify-content: center; align-items: center;">'
f'<img src="data:image/png;base64,{image_to_base64(image)}" alt="Uploaded Image" style="width: 50%; height: 50%;">'
f'</div>',
unsafe_allow_html=True
)
def image_to_base64(image, format="JPEG"):
# Convert image to base64 encoding
buffered = BytesIO()
image.save(buffered, format=format)
return base64.b64encode(buffered.getvalue()).decode()
def base64_to_image(base64_string):
# Convert image to base64 encoding
decoded_data = base64.base64decode(base64_string)
pil_image = Image.open(io.BytesIO(decoded_data))
return pil_image
def get_last_infos():
if os.path.exists("cache_infos.json"):
with open("cache_infos.json", "r") as f:
last_infos = json.load(f)
return last_infos
return {}
def get_last_options():
if os.path.exists("cache_options.json"):
with open("cache_options.json", "r") as f:
last_options = json.load(f)
return last_options
return {}
def remove_emojis(text):
text = "".join(c for c in text if c not in emoji.EMOJI_DATA)
tiles = text.split("\n")
tiles = [tile.strip() for tile in tiles]
return "\n".join(tiles)
def extract_emojis(text):
return "".join(c for c in text if c in emoji.EMOJI_DATA)
def check_options_changed(last_options, options):
if last_options.get("env", "") != options["env"]:
return True
if last_options.get("lang", "") != options["lang"]:
return True
if last_options.get("task", "") != options["task"]:
return True
if last_options.get("article_content", "") != options["article_content"]:
return True
if last_options.get("cached_infos", "") != options["cached_infos"]:
return True
## Check more options
ignores = []
if "more_options" in last_options:
for k in last_options["more_options"].keys():
if k in ignores:
continue
if last_options["more_options"][k] != options["more_options"][k]:
return True
# If nothing changed, do smt after return
content = last_options.get("content", "")
options["content"] = content
return False