Spaces:
Sleeping
Sleeping
Commit
•
4433993
1
Parent(s):
19aa03c
Upload 2 files
Browse files- app.py +41 -18
- text_converter.py +4 -7
app.py
CHANGED
@@ -1,5 +1,5 @@
|
|
1 |
import gradio as gr
|
2 |
-
from text_converter import
|
3 |
|
4 |
APP_DESCRIPTION = '''# Reading Level Converter
|
5 |
<div id="content_align">Convert any text to a specified reading level while retaining the core text meaning</div>'''
|
@@ -8,39 +8,63 @@ MIN_ENTAILMENT = 0.5
|
|
8 |
MAX_ITER = 5
|
9 |
SYSTEM_PROMPT = "You are a writing assistant. You help convert complex texts to simpler texts while maintaining the core meaning of the text."
|
10 |
|
11 |
-
def convert_text(input_text, grade_level, input_reading_score):
|
|
|
|
|
|
|
|
|
12 |
min_level, max_level = reading_levels[grade_level]
|
13 |
-
output_text, similarity, reading_level, message = generate_similar_sentence(input_text, min_level, max_level, MIN_ENTAILMENT, SYSTEM_PROMPT, MAX_ITER, float(input_reading_score))
|
14 |
return output_text, similarity, reading_level, message
|
15 |
|
16 |
with gr.Blocks(css='styles.css') as app:
|
17 |
gr.Markdown(APP_DESCRIPTION)
|
18 |
|
19 |
-
with gr.Tab("
|
20 |
with gr.Row():
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
input_text = gr.Textbox(label="Input Text", placeholder="Type here...", lines=4)
|
25 |
-
|
26 |
-
fetch_score_and_lvl_btn = gr.Button("Fetch Score and Level")
|
27 |
|
28 |
output_input_reading_score = gr.Textbox(label="Input Text Reading Score", placeholder="Input Text Reading Score...", lines=1)
|
29 |
output_input_reading_level = gr.Textbox(label="Input Text Reading Level", placeholder="Input Text Reading Level...", lines=1)
|
30 |
|
31 |
fetch_score_and_lvl_btn.click(
|
32 |
fn=user_input_readability_level,
|
33 |
-
inputs=[input_text],
|
34 |
outputs=[output_input_reading_score, output_input_reading_level]
|
35 |
)
|
36 |
|
37 |
-
grade_level = gr.Radio(label="Target Reading Level", interactive=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
38 |
|
39 |
-
|
40 |
-
fn=
|
41 |
-
inputs=[
|
42 |
-
outputs=[
|
43 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
44 |
|
45 |
output_reading_level = gr.Textbox(label="Output Reading Level", placeholder="Output Reading Level...", lines=1)
|
46 |
output_similarity = gr.Textbox(label="Similarity", placeholder="Similarity Score...", lines=1)
|
@@ -52,10 +76,9 @@ with gr.Blocks(css='styles.css') as app:
|
|
52 |
|
53 |
convert_button.click(
|
54 |
fn=convert_text,
|
55 |
-
inputs=[input_text, grade_level, output_input_reading_score],
|
56 |
outputs=[output_converted_text, output_similarity, output_reading_level, output_message]
|
57 |
)
|
58 |
|
59 |
-
|
60 |
if __name__ == '__main__':
|
61 |
app.launch()
|
|
|
1 |
import gradio as gr
|
2 |
+
from text_converter import fre_levels, sbert_levels, model_types, generate_similar_sentence, user_input_readability_level
|
3 |
|
4 |
APP_DESCRIPTION = '''# Reading Level Converter
|
5 |
<div id="content_align">Convert any text to a specified reading level while retaining the core text meaning</div>'''
|
|
|
8 |
MAX_ITER = 5
|
9 |
SYSTEM_PROMPT = "You are a writing assistant. You help convert complex texts to simpler texts while maintaining the core meaning of the text."
|
10 |
|
11 |
+
def convert_text(input_text, grade_level, input_reading_score, model_type):
|
12 |
+
if model_type == "FRE":
|
13 |
+
reading_levels = fre_levels
|
14 |
+
else:
|
15 |
+
reading_levels = sbert_levels
|
16 |
min_level, max_level = reading_levels[grade_level]
|
17 |
+
output_text, similarity, reading_level, message = generate_similar_sentence(input_text, min_level, max_level, MIN_ENTAILMENT, SYSTEM_PROMPT, MAX_ITER, float(input_reading_score), model_type)
|
18 |
return output_text, similarity, reading_level, message
|
19 |
|
20 |
with gr.Blocks(css='styles.css') as app:
|
21 |
gr.Markdown(APP_DESCRIPTION)
|
22 |
|
23 |
+
with gr.Tab("FRE"):
|
24 |
with gr.Row():
|
25 |
+
input_text = gr.Textbox(label="Input Text", placeholder="Type here...", lines=4, sclae = 2.5)
|
26 |
+
fetch_score_and_lvl_btn = gr.Button("Fetch Score and Level", scale = 0.5)
|
|
|
|
|
|
|
|
|
27 |
|
28 |
output_input_reading_score = gr.Textbox(label="Input Text Reading Score", placeholder="Input Text Reading Score...", lines=1)
|
29 |
output_input_reading_level = gr.Textbox(label="Input Text Reading Level", placeholder="Input Text Reading Level...", lines=1)
|
30 |
|
31 |
fetch_score_and_lvl_btn.click(
|
32 |
fn=user_input_readability_level,
|
33 |
+
inputs=[input_text, model_types[0]],
|
34 |
outputs=[output_input_reading_score, output_input_reading_level]
|
35 |
)
|
36 |
|
37 |
+
grade_level = gr.Radio(choices=list(fre_levels.keys()), label="Target Reading Level", value = list(fre_levels.keys())[0], interactive=True)
|
38 |
+
|
39 |
+
output_reading_level = gr.Textbox(label="Output Reading Level", placeholder="Output Reading Level...", lines=1)
|
40 |
+
output_similarity = gr.Textbox(label="Similarity", placeholder="Similarity Score...", lines=1)
|
41 |
+
output_converted_text = gr.Textbox(label="Converted Text", placeholder="Results will appear here...", lines=4)
|
42 |
+
|
43 |
+
output_message = gr.Textbox(label="Message", placeholder="System Message...", lines=2)
|
44 |
+
|
45 |
+
convert_button = gr.Button("Convert Text")
|
46 |
|
47 |
+
convert_button.click(
|
48 |
+
fn=convert_text,
|
49 |
+
inputs=[input_text, grade_level, output_input_reading_score, model_types[0]],
|
50 |
+
outputs=[output_converted_text, output_similarity, output_reading_level, output_message]
|
51 |
)
|
52 |
+
|
53 |
+
with gr.Tab("SBERT"):
|
54 |
+
with gr.Row():
|
55 |
+
input_text = gr.Textbox(label="Input Text", placeholder="Type here...", lines=4, sclae = 2.5)
|
56 |
+
fetch_score_and_lvl_btn = gr.Button("Fetch Score and Level", scale = 0.5)
|
57 |
+
|
58 |
+
output_input_reading_score = gr.Textbox(label="Input Text Reading Score", placeholder="Input Text Reading Score...", lines=1)
|
59 |
+
output_input_reading_level = gr.Textbox(label="Input Text Reading Level", placeholder="Input Text Reading Level...", lines=1)
|
60 |
+
|
61 |
+
fetch_score_and_lvl_btn.click(
|
62 |
+
fn=user_input_readability_level,
|
63 |
+
inputs=[input_text, model_types[1]],
|
64 |
+
outputs=[output_input_reading_score, output_input_reading_level]
|
65 |
+
)
|
66 |
+
|
67 |
+
grade_level = gr.Radio(choices=list(sbert_levels.keys()), label="Target Reading Level", value = list(sbert_levels.keys())[0], interactive=True)
|
68 |
|
69 |
output_reading_level = gr.Textbox(label="Output Reading Level", placeholder="Output Reading Level...", lines=1)
|
70 |
output_similarity = gr.Textbox(label="Similarity", placeholder="Similarity Score...", lines=1)
|
|
|
76 |
|
77 |
convert_button.click(
|
78 |
fn=convert_text,
|
79 |
+
inputs=[input_text, grade_level, output_input_reading_score, model_types[1]],
|
80 |
outputs=[output_converted_text, output_similarity, output_reading_level, output_message]
|
81 |
)
|
82 |
|
|
|
83 |
if __name__ == '__main__':
|
84 |
app.launch()
|
text_converter.py
CHANGED
@@ -26,7 +26,6 @@ def generate_user_prompt(prompt_type, base_text):
|
|
26 |
return prompts[prompt_type].format(base_text=base_text)
|
27 |
|
28 |
model_types = ["FRE", "SBERT"]
|
29 |
-
model_type = model_types[1]
|
30 |
|
31 |
fre_levels = {
|
32 |
"5th Grade (90-100)": (90, 100),
|
@@ -55,17 +54,15 @@ inverse_reading_levels = {v: k for k, v in reading_levels.items()}
|
|
55 |
def set_reading_levels(level_type):
|
56 |
global reading_levels
|
57 |
global inverse_reading_levels
|
58 |
-
global model_type
|
59 |
if level_type == "FRE":
|
60 |
reading_levels = fre_levels
|
61 |
elif level_type == "SBERT":
|
62 |
reading_levels = sbert_levels
|
63 |
inverse_reading_levels = {v: k for k, v in reading_levels.items()}
|
64 |
-
model_type = level_type
|
65 |
-
return reading_levels.keys()
|
66 |
|
67 |
-
def user_input_readability_level(input_text):
|
68 |
-
|
|
|
69 |
print(f'Reading score for user input is: {current_score} for model type: {model_type}')
|
70 |
current_level = ''
|
71 |
for (min, max), level in inverse_reading_levels.items():
|
@@ -75,7 +72,7 @@ def user_input_readability_level(input_text):
|
|
75 |
break
|
76 |
return current_score, current_level
|
77 |
|
78 |
-
def generate_similar_sentence(input_text, min_reading_level, max_reading_level, min_entailment, system_prompt, max_iter, curr_reading_level):
|
79 |
i = 0
|
80 |
completed = False
|
81 |
user_prompt = ""
|
|
|
26 |
return prompts[prompt_type].format(base_text=base_text)
|
27 |
|
28 |
model_types = ["FRE", "SBERT"]
|
|
|
29 |
|
30 |
fre_levels = {
|
31 |
"5th Grade (90-100)": (90, 100),
|
|
|
54 |
def set_reading_levels(level_type):
|
55 |
global reading_levels
|
56 |
global inverse_reading_levels
|
|
|
57 |
if level_type == "FRE":
|
58 |
reading_levels = fre_levels
|
59 |
elif level_type == "SBERT":
|
60 |
reading_levels = sbert_levels
|
61 |
inverse_reading_levels = {v: k for k, v in reading_levels.items()}
|
|
|
|
|
62 |
|
63 |
+
def user_input_readability_level(input_text, model_type):
|
64 |
+
set_reading_levels(model_type)
|
65 |
+
current_score = ping_api(input_text, model_type)
|
66 |
print(f'Reading score for user input is: {current_score} for model type: {model_type}')
|
67 |
current_level = ''
|
68 |
for (min, max), level in inverse_reading_levels.items():
|
|
|
72 |
break
|
73 |
return current_score, current_level
|
74 |
|
75 |
+
def generate_similar_sentence(input_text, min_reading_level, max_reading_level, min_entailment, system_prompt, max_iter, curr_reading_level, model_type):
|
76 |
i = 0
|
77 |
completed = False
|
78 |
user_prompt = ""
|