|
import streamlit as st |
|
import os |
|
from PIL import Image |
|
import math |
|
|
|
|
|
def list_files(folder_path, extensions): |
|
files = [f for f in os.listdir(folder_path) if os.path.isfile(os.path.join(folder_path, f))] |
|
return [f for f in files if f.split('.')[-1] in extensions] |
|
|
|
|
|
st.title("Display Images and Corresponding Text Files") |
|
|
|
|
|
folder_path = "/home/caimera-prod/kohya_new_dataset" |
|
|
|
|
|
image_extensions = ['jpg', 'jpeg', 'png'] |
|
text_extensions = ['txt'] |
|
|
|
|
|
files = list_files(folder_path, image_extensions + text_extensions) |
|
|
|
|
|
images = [f for f in files if f.split('.')[-1] in image_extensions] |
|
texts = [f for f in files if f.split('.')[-1] in text_extensions] |
|
|
|
|
|
file_map = {} |
|
for image in images: |
|
base_name = os.path.splitext(image)[0] |
|
corresponding_text = base_name + '.txt' |
|
if corresponding_text in texts: |
|
file_map[image] = corresponding_text |
|
|
|
|
|
items_per_page = 5 |
|
total_items = len(file_map) |
|
total_pages = math.ceil(total_items / items_per_page) |
|
page = st.sidebar.slider('Page', 1, total_pages, 1) |
|
|
|
|
|
start_idx = (page - 1) * items_per_page |
|
end_idx = start_idx + items_per_page |
|
|
|
|
|
for image_file, text_file in list(file_map.items())[start_idx:end_idx]: |
|
col1, col2 = st.columns(2) |
|
|
|
with col1: |
|
st.image(os.path.join(folder_path, image_file), caption=image_file, use_column_width=True) |
|
|
|
with col2: |
|
text_path = os.path.join(folder_path, text_file) |
|
with open(text_path, 'r') as file: |
|
text_content = file.read() |
|
|
|
|
|
updated_text = st.text_area(text_file, text_content, height=300) |
|
|
|
|
|
if st.button(f'Save {text_file}'): |
|
with open(text_path, 'w') as file: |
|
file.write(updated_text) |
|
st.success(f"Changes to {text_file} saved successfully.") |
|
|