Spaces:
Build error
Build error
Upload 2 files
Browse files- app.py +13 -9
- text_converter.py +12 -0
app.py
CHANGED
|
@@ -1,5 +1,5 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
from text_converter import fre_levels, sbert_levels,
|
| 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,10 +8,14 @@ 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
|
| 12 |
-
|
| 13 |
-
min_level, max_level
|
| 14 |
-
output_text, similarity, reading_level, message
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
return output_text, similarity, reading_level, message
|
| 16 |
|
| 17 |
with gr.Blocks(css='styles.css') as app:
|
|
@@ -36,8 +40,8 @@ with gr.Blocks(css='styles.css') as app:
|
|
| 36 |
output_input_reading_level = gr.Textbox(label="Input Text Reading Level", placeholder="Input Text Reading Level...", lines=1, interactive=False)
|
| 37 |
|
| 38 |
fetch_score_and_lvl_btn.click(
|
| 39 |
-
fn=
|
| 40 |
-
inputs=[input_text
|
| 41 |
outputs=[output_input_reading_score, output_input_reading_level]
|
| 42 |
)
|
| 43 |
|
|
@@ -55,8 +59,8 @@ with gr.Blocks(css='styles.css') as app:
|
|
| 55 |
convert_button = gr.Button("Convert Text")
|
| 56 |
|
| 57 |
convert_button.click(
|
| 58 |
-
fn=
|
| 59 |
-
inputs=[input_text, grade_level_fre, output_input_reading_score
|
| 60 |
outputs=[output_converted_text, output_similarity, output_reading_level, output_message]
|
| 61 |
)
|
| 62 |
#with gr.Tab("SBERT"):
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
from text_converter import fre_levels, sbert_levels, generate_sim_fre, generate_sim_sbert, fetch_readability_score_fre, fetch_readability_score_sbert
|
| 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_fre(input_text, grade_level, input_reading_score):
|
| 12 |
+
min_level, max_level = fre_levels[grade_level]
|
| 13 |
+
output_text, similarity, reading_level, message = generate_sim_fre(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 |
+
def convert_text_sbert(input_text, grade_level, input_reading_score, is_fre):
|
| 17 |
+
min_level, max_level = sbert_levels[grade_level]
|
| 18 |
+
output_text, similarity, reading_level, message = generate_sim_sbert(input_text, min_level, max_level, MIN_ENTAILMENT, SYSTEM_PROMPT, MAX_ITER, float(input_reading_score))
|
| 19 |
return output_text, similarity, reading_level, message
|
| 20 |
|
| 21 |
with gr.Blocks(css='styles.css') as app:
|
|
|
|
| 40 |
output_input_reading_level = gr.Textbox(label="Input Text Reading Level", placeholder="Input Text Reading Level...", lines=1, interactive=False)
|
| 41 |
|
| 42 |
fetch_score_and_lvl_btn.click(
|
| 43 |
+
fn=fetch_readability_score_fre,
|
| 44 |
+
inputs=[input_text],
|
| 45 |
outputs=[output_input_reading_score, output_input_reading_level]
|
| 46 |
)
|
| 47 |
|
|
|
|
| 59 |
convert_button = gr.Button("Convert Text")
|
| 60 |
|
| 61 |
convert_button.click(
|
| 62 |
+
fn=convert_text_fre,
|
| 63 |
+
inputs=[input_text, grade_level_fre, output_input_reading_score],
|
| 64 |
outputs=[output_converted_text, output_similarity, output_reading_level, output_message]
|
| 65 |
)
|
| 66 |
#with gr.Tab("SBERT"):
|
text_converter.py
CHANGED
|
@@ -61,6 +61,12 @@ def set_reading_levels(level_type):
|
|
| 61 |
inverse_reading_levels = {v: k for k, v in reading_levels.items()}
|
| 62 |
return level_type
|
| 63 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 64 |
def user_input_readability_level(input_text, is_fre):
|
| 65 |
current_score = ping_api(input_text, model_types[0] if is_fre else model_types[1])
|
| 66 |
print(f'Reading score for user input is: {current_score} for model type: {model_types[0] if is_fre else model_types[1]}')
|
|
@@ -72,6 +78,12 @@ def user_input_readability_level(input_text, is_fre):
|
|
| 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, is_fre):
|
| 76 |
i = 0
|
| 77 |
completed = False
|
|
|
|
| 61 |
inverse_reading_levels = {v: k for k, v in reading_levels.items()}
|
| 62 |
return level_type
|
| 63 |
|
| 64 |
+
def fetch_readability_score_fre(input_text):
|
| 65 |
+
return user_input_readability_level(input_text, True)
|
| 66 |
+
|
| 67 |
+
def fetch_readability_score_sbert(input_text):
|
| 68 |
+
return user_input_readability_level(input_text, False)
|
| 69 |
+
|
| 70 |
def user_input_readability_level(input_text, is_fre):
|
| 71 |
current_score = ping_api(input_text, model_types[0] if is_fre else model_types[1])
|
| 72 |
print(f'Reading score for user input is: {current_score} for model type: {model_types[0] if is_fre else model_types[1]}')
|
|
|
|
| 78 |
break
|
| 79 |
return current_score, current_level
|
| 80 |
|
| 81 |
+
def generate_sim_fre(input_text, min_reading_level, max_reading_level, min_entailment, system_prompt, max_iter, curr_reading_level):
|
| 82 |
+
return generate_similar_sentence(input_text, min_reading_level, max_reading_level, min_entailment, system_prompt, max_iter, curr_reading_level, True)
|
| 83 |
+
|
| 84 |
+
def generate_sim_sbert(input_text, min_reading_level, max_reading_level, min_entailment, system_prompt, max_iter, curr_reading_level):
|
| 85 |
+
return generate_similar_sentence(input_text, min_reading_level, max_reading_level, min_entailment, system_prompt, max_iter, curr_reading_level, False)
|
| 86 |
+
|
| 87 |
def generate_similar_sentence(input_text, min_reading_level, max_reading_level, min_entailment, system_prompt, max_iter, curr_reading_level, is_fre):
|
| 88 |
i = 0
|
| 89 |
completed = False
|