Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -171,15 +171,43 @@ def on_select(instruction1, instruction2, instruction3, evt: gr.SelectData):
|
|
| 171 |
|
| 172 |
#----------------#
|
| 173 |
# Grammar metrics
|
| 174 |
-
|
| 175 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 176 |
|
| 177 |
-
def
|
| 178 |
-
|
| 179 |
-
|
| 180 |
-
|
|
|
|
| 181 |
|
| 182 |
-
return
|
|
|
|
| 183 |
|
| 184 |
with gr.Blocks(title="RecurrentGPT", css="footer {visibility: hidden}", theme='sudeepshouche/minimalist') as demo:
|
| 185 |
gr.Markdown(
|
|
@@ -296,7 +324,7 @@ with gr.Blocks(title="RecurrentGPT", css="footer {visibility: hidden}", theme='s
|
|
| 296 |
calculate_button = gr.Button("Calculate Metrics")
|
| 297 |
|
| 298 |
def update_metrics(text):
|
| 299 |
-
grade, ease, fog =
|
| 300 |
return grade, ease, fog
|
| 301 |
|
| 302 |
calculate_button.click(fn=update_metrics, inputs=written_paras, outputs=[flesch_kincaid_grade, flesch_reading_ease, gunning_fog])
|
|
|
|
| 171 |
|
| 172 |
#----------------#
|
| 173 |
# Grammar metrics
|
| 174 |
+
import re
|
| 175 |
+
|
| 176 |
+
def preprocess_text(text):
|
| 177 |
+
# Remove special characters and extra spaces
|
| 178 |
+
text = re.sub(r'[^\w\s]', '', text)
|
| 179 |
+
text = re.sub(r'\s+', ' ', text).strip()
|
| 180 |
+
return text
|
| 181 |
+
|
| 182 |
+
def flesch_kincaid_grade_level(text):
|
| 183 |
+
sentences = len(re.split(r'[.!?]', text))
|
| 184 |
+
words = len(text.split())
|
| 185 |
+
syllables = sum([len(re.findall(r'[aeiouyAEIOUY]', word)) for word in text.split()])
|
| 186 |
+
|
| 187 |
+
return 0.39 * (words / sentences) + 11.8 * (syllables / words) - 15.59
|
| 188 |
+
|
| 189 |
+
def flesch_reading_ease(text):
|
| 190 |
+
sentences = len(re.split(r'[.!?]', text))
|
| 191 |
+
words = len(text.split())
|
| 192 |
+
syllables = sum([len(re.findall(r'[aeiouyAEIOUY]', word)) for word in text.split()])
|
| 193 |
+
|
| 194 |
+
return 206.835 - 1.015 * (words / sentences) - 84.6 * (syllables / words)
|
| 195 |
+
|
| 196 |
+
def gunning_fog_index(text):
|
| 197 |
+
sentences = len(re.split(r'[.!?]', text))
|
| 198 |
+
words = len(text.split())
|
| 199 |
+
complex_words = len([word for word in text.split() if len(re.findall(r'[aeiouyAEIOUY]', word)) >= 3])
|
| 200 |
+
|
| 201 |
+
return 0.4 * ((words / sentences) + 100 * (complex_words / words))
|
| 202 |
|
| 203 |
+
def calculate_readability_metrics(text):
|
| 204 |
+
preprocessed_text = preprocess_text(text)
|
| 205 |
+
fk_grade_level = flesch_kincaid_grade_level(preprocessed_text)
|
| 206 |
+
fk_reading_ease = flesch_reading_ease(preprocessed_text)
|
| 207 |
+
gunning_fog = gunning_fog_index(preprocessed_text)
|
| 208 |
|
| 209 |
+
return fk_grade_level, fk_reading_ease, gunning_fog
|
| 210 |
+
#-------------#
|
| 211 |
|
| 212 |
with gr.Blocks(title="RecurrentGPT", css="footer {visibility: hidden}", theme='sudeepshouche/minimalist') as demo:
|
| 213 |
gr.Markdown(
|
|
|
|
| 324 |
calculate_button = gr.Button("Calculate Metrics")
|
| 325 |
|
| 326 |
def update_metrics(text):
|
| 327 |
+
grade, ease, fog = calculate_readability_metrics(text)
|
| 328 |
return grade, ease, fog
|
| 329 |
|
| 330 |
calculate_button.click(fn=update_metrics, inputs=written_paras, outputs=[flesch_kincaid_grade, flesch_reading_ease, gunning_fog])
|