File size: 3,162 Bytes
c452620
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
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