| import streamlit as st |
| import os |
| from PIL import Image |
| from collections import Counter |
| import pandas as pd |
| |
| 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] |
|
|
| |
| def get_tag_frequencies(text_files): |
| tag_counter = Counter() |
| for text_file in text_files: |
| with open(text_file, 'r') as file: |
| tags = file.read().split() |
| tag_counter.update(tags) |
| return tag_counter |
|
|
| |
| 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 |
|
|
| |
| text_files_paths = [os.path.join(folder_path, text) for text in texts] |
| tag_frequencies = get_tag_frequencies(text_files_paths) |
|
|
| |
| tag_frequencies_data = [{'Tag': tag, 'Frequency': freq} for tag, freq in tag_frequencies.items()] |
| tag_frequencies_df = pd.DataFrame(tag_frequencies_data) |
|
|
| |
| st.subheader("Tag Frequencies") |
| st.table(tag_frequencies_df) |
|
|
| |
| for image_file, text_file in file_map.items(): |
| 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: |
| with open(os.path.join(folder_path, text_file), 'r') as file: |
| st.text_area(text_file, file.read(), height=300) |
|
|