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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
#------------------------------------------------------------------------
|
2 |
+
# Import Modules
|
3 |
+
#------------------------------------------------------------------------
|
4 |
+
|
5 |
+
import streamlit as st
|
6 |
+
import spacy
|
7 |
+
import string
|
8 |
+
from annotated_text import annotated_text
|
9 |
+
from PIL import Image
|
10 |
+
from pathlib import Path
|
11 |
+
from dotenv import load_dotenv
|
12 |
+
load_dotenv()
|
13 |
+
|
14 |
+
# Load the English NLP model
|
15 |
+
nlp = spacy.load("en_core_web_sm")
|
16 |
+
|
17 |
+
#------------------------------------------------------------------------
|
18 |
+
# Configurations
|
19 |
+
#------------------------------------------------------------------------
|
20 |
+
|
21 |
+
# Streamlit page setup
|
22 |
+
# icon = Image.open("MTSS.ai_Icon.png")
|
23 |
+
icon = Image.open("/Users/cheynelevesseur/Desktop/Python_Code/LLM_Projects/LLM_Prxmpting/MTSS.ai_Icon.png")
|
24 |
+
st.set_page_config(
|
25 |
+
page_title="Kaleidoscope | Text Annotation",
|
26 |
+
page_icon=icon,
|
27 |
+
layout="centered",
|
28 |
+
initial_sidebar_state="auto",
|
29 |
+
menu_items={
|
30 |
+
'About': "### *This application was created by* \n### LeVesseur Ph.D | MTSS.ai"
|
31 |
+
}
|
32 |
+
)
|
33 |
+
|
34 |
+
#------------------------------------------------------------------------
|
35 |
+
# Header
|
36 |
+
#------------------------------------------------------------------------
|
37 |
+
|
38 |
+
# st.image('MTSS.ai_Logo.png', width=300)
|
39 |
+
|
40 |
+
st.title('MTSS:grey[.ai]')
|
41 |
+
st.header('Kaleidoscope:grey[ | Text Annotation]')
|
42 |
+
|
43 |
+
contact = st.sidebar.toggle('Handmade by \n**LeVesseur** :grey[ PhD] \n| :grey[MTSS.ai]')
|
44 |
+
if contact:
|
45 |
+
st.sidebar.write('Inquiries: [[email protected]](mailto:[email protected]) \nProfile: [levesseur.com](http://levesseur.com) \nCheck out: [InkQA | Dynamic PDFs](http://www.inkqa.com)')
|
46 |
+
|
47 |
+
#------------------------------------------------------------------------
|
48 |
+
# Sidebar
|
49 |
+
#------------------------------------------------------------------------
|
50 |
+
|
51 |
+
# Color options
|
52 |
+
colors = {
|
53 |
+
"Green (DAF1E7)": "#DAF1E7",
|
54 |
+
"Blue (BDE5FF)": "#BDE5FF",
|
55 |
+
"Navy (D1DBE9)": "#D1DBE9",
|
56 |
+
"Teal (D6EAED)": "#D6EAED",
|
57 |
+
"Iceburg (E4EEF6)": "#E4EEF6",
|
58 |
+
"Vermillion (F6DCDD)": "#F6DCDD",
|
59 |
+
}
|
60 |
+
|
61 |
+
with st.sidebar:
|
62 |
+
st.divider()
|
63 |
+
# Sidebar display (Option 1: Color blocks with hex)
|
64 |
+
st.sidebar.header("Recommended Colors")
|
65 |
+
|
66 |
+
for color_name, hex_code in colors.items():
|
67 |
+
st.sidebar.color_picker(color_name, hex_code)
|
68 |
+
|
69 |
+
st.subheader("Example")
|
70 |
+
|
71 |
+
annotated_text(
|
72 |
+
"I",
|
73 |
+
" ",
|
74 |
+
"really",
|
75 |
+
" ",
|
76 |
+
("appreciate", "Emotion", "#daf1e7"),
|
77 |
+
" ",
|
78 |
+
"all",
|
79 |
+
" ",
|
80 |
+
"that",
|
81 |
+
" ",
|
82 |
+
"the",
|
83 |
+
" ",
|
84 |
+
"social",
|
85 |
+
" ",
|
86 |
+
"committee",
|
87 |
+
" ",
|
88 |
+
"has",
|
89 |
+
" ",
|
90 |
+
"done",
|
91 |
+
" ",
|
92 |
+
"to",
|
93 |
+
" ",
|
94 |
+
"keep",
|
95 |
+
" ",
|
96 |
+
"us",
|
97 |
+
" ",
|
98 |
+
"feeling",
|
99 |
+
" ",
|
100 |
+
"connected",
|
101 |
+
" ",
|
102 |
+
".",
|
103 |
+
" ",
|
104 |
+
"I",
|
105 |
+
" ",
|
106 |
+
"also",
|
107 |
+
" ",
|
108 |
+
"really",
|
109 |
+
" ",
|
110 |
+
("value", "Priority", "#bde5ff"),
|
111 |
+
" ",
|
112 |
+
"our",
|
113 |
+
" ",
|
114 |
+
"in",
|
115 |
+
" ",
|
116 |
+
"-person",
|
117 |
+
" ",
|
118 |
+
"meetings",
|
119 |
+
" ",
|
120 |
+
"and",
|
121 |
+
" ",
|
122 |
+
"the",
|
123 |
+
" ",
|
124 |
+
"social",
|
125 |
+
" ",
|
126 |
+
"opportunities",
|
127 |
+
" ",
|
128 |
+
"built",
|
129 |
+
" ",
|
130 |
+
"into",
|
131 |
+
" ",
|
132 |
+
"these",
|
133 |
+
" ",
|
134 |
+
"meetings",
|
135 |
+
" ",
|
136 |
+
".",
|
137 |
+
)
|
138 |
+
|
139 |
+
st.divider()
|
140 |
+
|
141 |
+
st.subheader("Directions for Using the Text Annotation Tool")
|
142 |
+
|
143 |
+
directions = """
|
144 |
+
1. **Enter Your Text**:
|
145 |
+
- Type or paste the text you want to annotate in the text area provided.
|
146 |
+
|
147 |
+
2. **Submit Your Text**:
|
148 |
+
- Click the "Submit Text" button to process your input. This will split your text into individual words.
|
149 |
+
|
150 |
+
3. **Annotate the Words**:
|
151 |
+
- For each word, you can provide a label (e.g., "Emotion", "Priority", "Verb").
|
152 |
+
- Choose a color for each label using the color picker.
|
153 |
+
|
154 |
+
4. **Generate Annotated Text**:
|
155 |
+
- Once you have labeled and colored the words you want to annotate, click the "Generate Annotated Text" button.
|
156 |
+
- The annotated text will be displayed, showing your labels and chosen colors.
|
157 |
+
|
158 |
+
5. **Take a Screenshot**:
|
159 |
+
- To use the annotated text, take a screenshot of the displayed text.
|
160 |
+
|
161 |
+
6. **Adjust Text Width** (Optional):
|
162 |
+
- 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.
|
163 |
+
"""
|
164 |
+
|
165 |
+
st.markdown(directions)
|
166 |
+
|
167 |
+
#------------------------------------------------------------------------
|
168 |
+
# Functions: Personalized Labels and Colors
|
169 |
+
#------------------------------------------------------------------------
|
170 |
+
|
171 |
+
# Function to split text into words
|
172 |
+
def split_text(text):
|
173 |
+
# Add a space before punctuation marks
|
174 |
+
for char in string.punctuation:
|
175 |
+
text = text.replace(char, f" {char}")
|
176 |
+
return text.split()
|
177 |
+
|
178 |
+
# Initialize session state to store text and annotations
|
179 |
+
if 'user_text' not in st.session_state:
|
180 |
+
st.session_state.user_text = ""
|
181 |
+
if 'words' not in st.session_state:
|
182 |
+
st.session_state.words = []
|
183 |
+
if 'labels' not in st.session_state:
|
184 |
+
st.session_state.labels = []
|
185 |
+
if 'colors' not in st.session_state:
|
186 |
+
st.session_state.colors = []
|
187 |
+
|
188 |
+
# User input for the text
|
189 |
+
user_text = st.text_area("Enter the text you want to annotate:", value=st.session_state.user_text, height=100)
|
190 |
+
|
191 |
+
# Button to process the text
|
192 |
+
if st.button("Submit Text"):
|
193 |
+
st.session_state.user_text = user_text
|
194 |
+
st.session_state.words = split_text(user_text)
|
195 |
+
st.session_state.labels = [""] * len(st.session_state.words)
|
196 |
+
st.session_state.colors = ["#FFFFFF"] * len(st.session_state.words)
|
197 |
+
|
198 |
+
# Collect annotation inputs for each word
|
199 |
+
if st.session_state.words:
|
200 |
+
for i, word in enumerate(st.session_state.words):
|
201 |
+
st.write(f"Annotate the word: {word}")
|
202 |
+
st.session_state.labels[i] = st.text_input(
|
203 |
+
f"Label for '{word}'",
|
204 |
+
value=st.session_state.labels[i],
|
205 |
+
key=f"label_{i}"
|
206 |
+
)
|
207 |
+
st.session_state.colors[i] = st.color_picker(
|
208 |
+
f"Color for '{word}'",
|
209 |
+
value=st.session_state.colors[i],
|
210 |
+
key=f"color_{i}"
|
211 |
+
)
|
212 |
+
|
213 |
+
# Generate button to process the annotations
|
214 |
+
if st.button("Generate Annotated Text", type="primary"):
|
215 |
+
annotated_elements = []
|
216 |
+
for i, word in enumerate(st.session_state.words):
|
217 |
+
if st.session_state.labels[i] and st.session_state.colors[i] != "#FFFFFF":
|
218 |
+
annotated_elements.append((word, st.session_state.labels[i], st.session_state.colors[i]))
|
219 |
+
else:
|
220 |
+
annotated_elements.append(word)
|
221 |
+
annotated_elements.append(" ") # Add space between words
|
222 |
+
|
223 |
+
# Remove the last extra space added
|
224 |
+
if annotated_elements and annotated_elements[-1] == " ":
|
225 |
+
annotated_elements.pop()
|
226 |
+
|
227 |
+
# Display the annotated text using the `annotated_text` function
|
228 |
+
st.subheader("Annotated Text:")
|
229 |
+
annotated_text(*annotated_elements)
|
230 |
+
|
231 |
+
# Print the code for the annotated text
|
232 |
+
st.subheader("Generated Code:")
|
233 |
+
code_str = 'annotated_text(\n'
|
234 |
+
for elem in annotated_elements:
|
235 |
+
if isinstance(elem, tuple):
|
236 |
+
code_str += f' ("{elem[0]}", "{elem[1]}", "{elem[2]}"),\n'
|
237 |
+
else:
|
238 |
+
code_str += f' "{elem}",\n'
|
239 |
+
code_str += ')'
|
240 |
+
st.code(code_str, language='python')
|