Spaces:
Sleeping
Sleeping
File size: 7,293 Bytes
123ce18 |
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 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 |
#------------------------------------------------------------------------
# Import Modules
#------------------------------------------------------------------------
import streamlit as st
import string
from annotated_text import annotated_text
from PIL import Image
from pathlib import Path
from dotenv import load_dotenv
load_dotenv()
#------------------------------------------------------------------------
# Configurations
#------------------------------------------------------------------------
# Streamlit page setup
# icon = Image.open("MTSS.ai_Icon.png")
icon = Image.open("/Users/cheynelevesseur/Desktop/Python_Code/LLM_Projects/LLM_Prxmpting/MTSS.ai_Icon.png")
st.set_page_config(
page_title="Kaleidoscope | Text Annotation",
page_icon=icon,
layout="centered",
initial_sidebar_state="auto",
menu_items={
'About': "### *This application was created by* \n### LeVesseur Ph.D | MTSS.ai"
}
)
#------------------------------------------------------------------------
# Header
#------------------------------------------------------------------------
# st.image('MTSS.ai_Logo.png', width=300)
st.title('MTSS:grey[.ai]')
st.header('Kaleidoscope:grey[ | Text Annotation]')
contact = st.sidebar.toggle('Handmade by \n**LeVesseur** :grey[ PhD] \n| :grey[MTSS.ai]')
if contact:
st.sidebar.write('Inquiries: [[email protected]](mailto:[email protected]) \nProfile: [levesseur.com](http://levesseur.com) \nCheck out: [InkQA | Dynamic PDFs](http://www.inkqa.com)')
#------------------------------------------------------------------------
# Sidebar
#------------------------------------------------------------------------
# Color options
colors = {
"Green (DAF1E7)": "#DAF1E7",
"Blue (BDE5FF)": "#BDE5FF",
"Navy (D1DBE9)": "#D1DBE9",
"Teal (D6EAED)": "#D6EAED",
"Iceburg (E4EEF6)": "#E4EEF6",
"Vermillion (F6DCDD)": "#F6DCDD",
}
with st.sidebar:
st.divider()
# Sidebar display (Option 1: Color blocks with hex)
st.sidebar.header("Recommended Colors")
for color_name, hex_code in colors.items():
st.sidebar.color_picker(color_name, hex_code)
st.subheader("Example")
annotated_text(
"I",
" ",
"really",
" ",
("appreciate", "Emotion", "#daf1e7"),
" ",
"all",
" ",
"that",
" ",
"the",
" ",
"social",
" ",
"committee",
" ",
"has",
" ",
"done",
" ",
"to",
" ",
"keep",
" ",
"us",
" ",
"feeling",
" ",
"connected",
" ",
".",
" ",
"I",
" ",
"also",
" ",
"really",
" ",
("value", "Priority", "#bde5ff"),
" ",
"our",
" ",
"in",
" ",
"-person",
" ",
"meetings",
" ",
"and",
" ",
"the",
" ",
"social",
" ",
"opportunities",
" ",
"built",
" ",
"into",
" ",
"these",
" ",
"meetings",
" ",
".",
)
st.divider()
st.subheader("Directions for Using the Text Annotation Tool")
directions = """
1. **Enter Your Text**:
- Type or paste the text you want to annotate in the text area provided.
2. **Submit Your Text**:
- Click the "Submit Text" button to process your input. This will split your text into individual words.
3. **Annotate the Words**:
- For each word, you can provide a label (e.g., "Emotion", "Priority", "Verb").
- Choose a color for each label using the color picker.
4. **Generate Annotated Text**:
- Once you have labeled and colored the words you want to annotate, click the "Generate Annotated Text" button.
- The annotated text will be displayed, showing your labels and chosen colors.
5. **Take a Screenshot**:
- To use the annotated text, take a screenshot of the displayed text.
6. **Adjust Text Width** (Optional):
- If you want to adjust the width of the sentences for a better screenshot, minimize or resize your browser window accordingly before taking the screenshot.
"""
st.markdown(directions)
#------------------------------------------------------------------------
# Functions: Personalized Labels and Colors
#------------------------------------------------------------------------
# Function to split text into words
def split_text(text):
# Add a space before punctuation marks
for char in string.punctuation:
text = text.replace(char, f" {char}")
return text.split()
# Initialize session state to store text and annotations
if 'user_text' not in st.session_state:
st.session_state.user_text = ""
if 'words' not in st.session_state:
st.session_state.words = []
if 'labels' not in st.session_state:
st.session_state.labels = []
if 'colors' not in st.session_state:
st.session_state.colors = []
# User input for the text
user_text = st.text_area("Enter the text you want to annotate:", value=st.session_state.user_text, height=100)
# Button to process the text
if st.button("Submit Text"):
st.session_state.user_text = user_text
st.session_state.words = split_text(user_text)
st.session_state.labels = [""] * len(st.session_state.words)
st.session_state.colors = ["#FFFFFF"] * len(st.session_state.words)
# Collect annotation inputs for each word
if st.session_state.words:
for i, word in enumerate(st.session_state.words):
st.write(f"Annotate the word: {word}")
st.session_state.labels[i] = st.text_input(
f"Label for '{word}'",
value=st.session_state.labels[i],
key=f"label_{i}"
)
st.session_state.colors[i] = st.color_picker(
f"Color for '{word}'",
value=st.session_state.colors[i],
key=f"color_{i}"
)
# Generate button to process the annotations
if st.button("Generate Annotated Text", type="primary"):
annotated_elements = []
for i, word in enumerate(st.session_state.words):
if st.session_state.labels[i] and st.session_state.colors[i] != "#FFFFFF":
annotated_elements.append((word, st.session_state.labels[i], st.session_state.colors[i]))
else:
annotated_elements.append(word)
annotated_elements.append(" ") # Add space between words
# Remove the last extra space added
if annotated_elements and annotated_elements[-1] == " ":
annotated_elements.pop()
# Display the annotated text using the `annotated_text` function
st.subheader("Annotated Text:")
annotated_text(*annotated_elements)
# Print the code for the annotated text
st.subheader("Generated Code:")
code_str = 'annotated_text(\n'
for elem in annotated_elements:
if isinstance(elem, tuple):
code_str += f' ("{elem[0]}", "{elem[1]}", "{elem[2]}"),\n'
else:
code_str += f' "{elem}",\n'
code_str += ')'
st.code(code_str, language='python')
|