feat(all): reject state, major cleanup
Browse files- app.py +85 -81
- requirements.txt +0 -0
app.py
CHANGED
@@ -1,8 +1,8 @@
|
|
1 |
# Import libraries and modules
|
2 |
-
# Run pip install gradio and
|
3 |
import gradio as gr
|
4 |
import pandas as pd
|
5 |
-
from
|
6 |
from main import generate_dfa
|
7 |
|
8 |
# DataFrame
|
@@ -21,23 +21,30 @@ with open("words.txt") as file:
|
|
21 |
dfa = generate_dfa(words)
|
22 |
|
23 |
|
24 |
-
# Generate examples
|
25 |
def generateExamples():
|
26 |
-
|
27 |
examples = []
|
28 |
for i in range(3):
|
29 |
-
examples.append(
|
30 |
return examples
|
31 |
|
32 |
|
33 |
# Color match function
|
34 |
-
def color_match(text):
|
35 |
colored_text = []
|
36 |
pointer = 0
|
37 |
|
38 |
# Get the result of the DFA check on the input text
|
39 |
match_dict = dfa.check(text)
|
40 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
41 |
# Flatten the match_dict into a list of tuples and sort by the start index
|
42 |
matches = sorted(
|
43 |
(start, end, word)
|
@@ -47,67 +54,31 @@ def color_match(text):
|
|
47 |
|
48 |
for start, end, word in matches:
|
49 |
colored_text.append(text[pointer:start])
|
50 |
-
# print(f"Start: {start}, End: {end}, Word: {word}")
|
51 |
# End need to be incremented by 1 to include the last character
|
52 |
-
colored_text.append(f
|
53 |
-
# print(f"Colored Text: {colored_text}")
|
54 |
# Move the pointer to the end of the match
|
55 |
pointer = end + 1
|
56 |
|
57 |
# Add remaining text
|
58 |
colored_text.append(text[pointer:])
|
59 |
-
# print(f"Text before merge: {colored_text}")
|
60 |
# Combine the strings
|
61 |
colored_text = "".join(colored_text)
|
62 |
-
# print(f"Colored Text after merging: {colored_text}")
|
63 |
-
|
64 |
-
# Call getOccurrences function and get the DataFrame
|
65 |
-
occurrences_df = getOccurrences(text)
|
66 |
-
# print(f"Occurences_df: {occurrences_df}")
|
67 |
|
68 |
# Call getPositions function and get the DataFrame
|
69 |
positions_df = getPositions(text)
|
70 |
-
# print(f"Positions_df: {positions_df}")
|
71 |
-
|
72 |
-
return colored_text, occurrences_df, positions_df
|
73 |
-
|
74 |
|
75 |
-
|
76 |
-
def getOccurrences(text):
|
77 |
-
match_dict = dfa.check(text)
|
78 |
-
wordCount = {}
|
79 |
-
for word, positions in match_dict.items():
|
80 |
-
# print(f"Word: {word}, Positions: {positions}")
|
81 |
-
# print(f"Length of positions: {len(positions)}")
|
82 |
-
# Store the word and the number of occurrences in the wordCount dictionary
|
83 |
-
wordCount[word] = len(positions)
|
84 |
-
# print(f"Word Count: {wordCount}")
|
85 |
-
|
86 |
-
# Convert the wordCount dictionary to a DataFrame
|
87 |
-
occurrences_df = pd.DataFrame(
|
88 |
-
list(wordCount.items()), columns=["Words", "Occurrences"]
|
89 |
-
)
|
90 |
-
# print(occurences_df)
|
91 |
-
return occurrences_df
|
92 |
|
93 |
|
94 |
# Get positions function
|
95 |
def getPositions(text):
|
96 |
match_dict = dfa.check(text)
|
97 |
-
|
98 |
for word, positions in match_dict.items():
|
99 |
# Convert the list of tuples to a string
|
100 |
positions_str = ", ".join(map(str, positions))
|
101 |
-
print(f"Word: {word}, Positions: {positions_str}")
|
102 |
# Store the word and the positions string in the wordPositions dictionary
|
103 |
-
|
104 |
-
print(f"Word Positions: {wordPositions}")
|
105 |
-
|
106 |
-
# Convert the wordPositions dictionary to a DataFrame
|
107 |
-
positions_df = pd.DataFrame(
|
108 |
-
list(wordPositions.items()), columns=["Words", "Positions"]
|
109 |
-
)
|
110 |
-
print(f"Positions_df: {positions_df}")
|
111 |
|
112 |
return positions_df
|
113 |
|
@@ -116,10 +87,27 @@ def getPositions(text):
|
|
116 |
def search_and_display(search_query):
|
117 |
# Filter the DataFrame based on the search query
|
118 |
filtered_df = df[df["Words"].str.contains(search_query)]
|
119 |
-
# print(f"Filtered text: {filtered_df}")
|
120 |
return filtered_df
|
121 |
|
122 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
123 |
# CSS styling
|
124 |
# css = """
|
125 |
# warning {background-color: #FFCCCB}
|
@@ -133,41 +121,47 @@ def search_and_display(search_query):
|
|
133 |
|
134 |
# Gradio UI
|
135 |
with gr.Blocks() as demo:
|
136 |
-
|
137 |
# Title block
|
138 |
# Apply CSS styling to the title
|
139 |
title = gr.HTML(
|
140 |
-
"<h1 style='color:
|
141 |
)
|
142 |
-
|
143 |
-
|
144 |
-
|
145 |
-
|
146 |
-
|
147 |
-
|
148 |
-
|
149 |
-
|
150 |
-
|
151 |
-
|
152 |
-
|
153 |
-
|
154 |
-
|
155 |
-
|
156 |
-
|
157 |
-
|
158 |
-
|
159 |
-
|
160 |
-
|
161 |
-
|
|
|
|
|
|
|
162 |
|
163 |
# Text block for DFA and color match
|
164 |
-
textTitle = gr.HTML("<h2>Try it here!</h2>")
|
|
|
|
|
|
|
165 |
text = gr.Textbox(
|
|
|
166 |
label="Text",
|
167 |
placeholder="Enter text here",
|
168 |
info="Enter text to check for DFA match",
|
|
|
169 |
)
|
170 |
-
submit_btn = gr.Button(value="Submit")
|
171 |
|
172 |
# Examples block
|
173 |
examples_data = generateExamples()
|
@@ -175,27 +169,37 @@ with gr.Blocks() as demo:
|
|
175 |
examples=examples_data,
|
176 |
inputs=[text],
|
177 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
178 |
|
179 |
# Result block
|
180 |
-
resultTitle = gr.HTML("<h2 style='color:
|
181 |
result = gr.HTML("<p></p>")
|
182 |
|
183 |
-
# Occurrences block
|
184 |
-
occurrencesTitle = gr.HTML(
|
185 |
-
"<h2 style='color: gold; margin-bottom: 5px'>Occurrences</h2>"
|
186 |
-
)
|
187 |
-
occurrences = gr.Dataframe()
|
188 |
-
|
189 |
# Position block
|
190 |
-
positionTitle = gr.HTML("<h2 style='color: gold;'>Position</h2>")
|
191 |
-
position = gr.Dataframe(
|
|
|
|
|
192 |
|
193 |
submit_btn.click(
|
194 |
color_match,
|
195 |
inputs=[text],
|
196 |
-
outputs=[result,
|
197 |
api_name=False,
|
198 |
)
|
|
|
|
|
|
|
|
|
|
|
199 |
|
200 |
# Launch the app
|
201 |
demo.launch()
|
|
|
1 |
# Import libraries and modules
|
2 |
+
# Run pip install gradio and essential_generators in terminal
|
3 |
import gradio as gr
|
4 |
import pandas as pd
|
5 |
+
from essential_generators import DocumentGenerator
|
6 |
from main import generate_dfa
|
7 |
|
8 |
# DataFrame
|
|
|
21 |
dfa = generate_dfa(words)
|
22 |
|
23 |
|
24 |
+
# Generate examples
|
25 |
def generateExamples():
|
26 |
+
gen = DocumentGenerator()
|
27 |
examples = []
|
28 |
for i in range(3):
|
29 |
+
examples.append(gen.paragraph())
|
30 |
return examples
|
31 |
|
32 |
|
33 |
# Color match function
|
34 |
+
def color_match(text: gr.Textbox):
|
35 |
colored_text = []
|
36 |
pointer = 0
|
37 |
|
38 |
# Get the result of the DFA check on the input text
|
39 |
match_dict = dfa.check(text)
|
40 |
|
41 |
+
if not match_dict:
|
42 |
+
return (
|
43 |
+
'<div style="background-color: #dc2626; color: #fff; text-align: center; width: 100%; padding: 10px; font-weight:800; font-size:1.5rem">Rejected</div>',
|
44 |
+
None,
|
45 |
+
None,
|
46 |
+
)
|
47 |
+
|
48 |
# Flatten the match_dict into a list of tuples and sort by the start index
|
49 |
matches = sorted(
|
50 |
(start, end, word)
|
|
|
54 |
|
55 |
for start, end, word in matches:
|
56 |
colored_text.append(text[pointer:start])
|
|
|
57 |
# End need to be incremented by 1 to include the last character
|
58 |
+
colored_text.append(f"<span style='color:#4ade80'>{text[start:end + 1]}</span>")
|
|
|
59 |
# Move the pointer to the end of the match
|
60 |
pointer = end + 1
|
61 |
|
62 |
# Add remaining text
|
63 |
colored_text.append(text[pointer:])
|
|
|
64 |
# Combine the strings
|
65 |
colored_text = "".join(colored_text)
|
|
|
|
|
|
|
|
|
|
|
66 |
|
67 |
# Call getPositions function and get the DataFrame
|
68 |
positions_df = getPositions(text)
|
|
|
|
|
|
|
|
|
69 |
|
70 |
+
return colored_text, positions_df
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
71 |
|
72 |
|
73 |
# Get positions function
|
74 |
def getPositions(text):
|
75 |
match_dict = dfa.check(text)
|
76 |
+
positions_df = pd.DataFrame(columns=["Words", "Positions", "Occurences"])
|
77 |
for word, positions in match_dict.items():
|
78 |
# Convert the list of tuples to a string
|
79 |
positions_str = ", ".join(map(str, positions))
|
|
|
80 |
# Store the word and the positions string in the wordPositions dictionary
|
81 |
+
positions_df.loc[len(positions_df)] = [word, positions_str, len(positions)]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
82 |
|
83 |
return positions_df
|
84 |
|
|
|
87 |
def search_and_display(search_query):
|
88 |
# Filter the DataFrame based on the search query
|
89 |
filtered_df = df[df["Words"].str.contains(search_query)]
|
|
|
90 |
return filtered_df
|
91 |
|
92 |
|
93 |
+
def text_change_search(text: gr.Textbox):
|
94 |
+
if text == "":
|
95 |
+
return gr.update(interactive=False)
|
96 |
+
else:
|
97 |
+
return gr.update(interactive=True)
|
98 |
+
|
99 |
+
|
100 |
+
def text_change_test(text: gr.Textbox):
|
101 |
+
if text == "":
|
102 |
+
return gr.update(interactive=False), gr.update(interactive=False)
|
103 |
+
else:
|
104 |
+
return gr.update(interactive=True), gr.update(interactive=True)
|
105 |
+
|
106 |
+
|
107 |
+
def remove_output(result, position):
|
108 |
+
return None, None
|
109 |
+
|
110 |
+
|
111 |
# CSS styling
|
112 |
# css = """
|
113 |
# warning {background-color: #FFCCCB}
|
|
|
121 |
|
122 |
# Gradio UI
|
123 |
with gr.Blocks() as demo:
|
|
|
124 |
# Title block
|
125 |
# Apply CSS styling to the title
|
126 |
title = gr.HTML(
|
127 |
+
"<h1 style='color: #2563eb; font-weight:bold'>English Conjunctions/Adverb/Adjectives Finder</h1>"
|
128 |
)
|
129 |
+
with gr.Accordion("Accepted Words", open=False):
|
130 |
+
# Search block
|
131 |
+
search = gr.Textbox(
|
132 |
+
label="Search",
|
133 |
+
placeholder="Search accepted words here",
|
134 |
+
lines=1,
|
135 |
+
info="List of accpetable words in DFA",
|
136 |
+
show_copy_button=True,
|
137 |
+
)
|
138 |
+
with gr.Row():
|
139 |
+
cancel_btn = gr.ClearButton(search, variant="stop", interactive=False)
|
140 |
+
search_btn = gr.Button(value="Search", variant="primary")
|
141 |
+
resultSearch = gr.Dataframe(df, height=300, col_count=1, headers=["Words"])
|
142 |
+
|
143 |
+
search.change(
|
144 |
+
text_change_search,
|
145 |
+
inputs=[search],
|
146 |
+
outputs=[cancel_btn],
|
147 |
+
)
|
148 |
+
|
149 |
+
search_btn.click(
|
150 |
+
search_and_display, inputs=[search], outputs=[resultSearch], api_name=False
|
151 |
+
)
|
152 |
|
153 |
# Text block for DFA and color match
|
154 |
+
textTitle = gr.HTML("<h2 style='color: #2563eb;'>Try it here!</h2>")
|
155 |
+
description = gr.HTML(
|
156 |
+
"<p style='color: #a78bfa;'>Enter a text and see the words that are accepted by the DFA highlighted in <span style='color:#4ade80'>green</span>.</p>"
|
157 |
+
)
|
158 |
text = gr.Textbox(
|
159 |
+
autofocus=True,
|
160 |
label="Text",
|
161 |
placeholder="Enter text here",
|
162 |
info="Enter text to check for DFA match",
|
163 |
+
show_copy_button=True,
|
164 |
)
|
|
|
165 |
|
166 |
# Examples block
|
167 |
examples_data = generateExamples()
|
|
|
169 |
examples=examples_data,
|
170 |
inputs=[text],
|
171 |
)
|
172 |
+
with gr.Row():
|
173 |
+
cancel_btn = gr.ClearButton([text], variant="stop", interactive=False)
|
174 |
+
submit_btn = gr.Button(value="Submit", variant="primary", interactive=False)
|
175 |
+
|
176 |
+
text.change(
|
177 |
+
text_change_test,
|
178 |
+
inputs=[text],
|
179 |
+
outputs=[cancel_btn, submit_btn],
|
180 |
+
)
|
181 |
|
182 |
# Result block
|
183 |
+
resultTitle = gr.HTML("<h2 style='color: #2563eb;'>Result</h2>")
|
184 |
result = gr.HTML("<p></p>")
|
185 |
|
|
|
|
|
|
|
|
|
|
|
|
|
186 |
# Position block
|
187 |
+
# positionTitle = gr.HTML("<h2 style='color: gold;'>Position</h2>")
|
188 |
+
position = gr.Dataframe(
|
189 |
+
show_label=True, col_count=3, headers=["Words", "Positions", "Occurences"]
|
190 |
+
)
|
191 |
|
192 |
submit_btn.click(
|
193 |
color_match,
|
194 |
inputs=[text],
|
195 |
+
outputs=[result, position],
|
196 |
api_name=False,
|
197 |
)
|
198 |
+
cancel_btn.click(
|
199 |
+
remove_output,
|
200 |
+
inputs=[result, position],
|
201 |
+
outputs=[result, position],
|
202 |
+
)
|
203 |
|
204 |
# Launch the app
|
205 |
demo.launch()
|
requirements.txt
CHANGED
Binary files a/requirements.txt and b/requirements.txt differ
|
|