Spaces:
Sleeping
Sleeping
import gradio as gr | |
from text_converter import fre_levels, sbert_levels, generate_sim_fre, generate_sim_sbert, fetch_readability_score_fre, fetch_readability_score_sbert | |
APP_DESCRIPTION = '''# Reading Level Converter | |
<div id="content_align">Convert any text to a specified reading level while retaining the core text meaning</div>''' | |
MIN_ENTAILMENT = 0.5 | |
MAX_ITER = 5 | |
SYSTEM_PROMPT = "You are a writing assistant. You help convert complex texts to simpler texts while maintaining the core meaning of the text." | |
def convert_text_fre(input_text, grade_level, input_reading_score): | |
min_level, max_level = fre_levels[grade_level] | |
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)) | |
return output_text, similarity, reading_level, message | |
def convert_text_sbert(input_text, grade_level, input_reading_score, is_fre): | |
min_level, max_level = sbert_levels[grade_level] | |
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)) | |
return output_text, similarity, reading_level, message | |
with gr.Blocks(css='styles.css') as app: | |
gr.Markdown(APP_DESCRIPTION) | |
with gr.Tab("FRE"): | |
with gr.Row(): | |
input_text = gr.Textbox(label="Input Text", placeholder="Type here...", lines=4, scale = 2) | |
fetch_score_and_lvl_btn = gr.Button("Fetch Score and Level", scale = 1) | |
output_input_reading_score = gr.Textbox(label="Input Text Reading Score", placeholder="Input Text Reading Score...", lines=1, interactive=False) | |
output_input_reading_level = gr.Textbox(label="Input Text Reading Level", placeholder="Input Text Reading Level...", lines=1, interactive=False) | |
fetch_score_and_lvl_btn.click( | |
fn=fetch_readability_score_fre, | |
inputs=[input_text], | |
outputs=[output_input_reading_score, output_input_reading_level] | |
) | |
grade_level_fre = gr.Radio(choices=list(fre_levels.keys()), label="Target Reading Level", interactive=True) | |
output_reading_level = gr.Textbox(label="Output Reading Level", placeholder="Output Reading Level...", lines=1) | |
output_similarity = gr.Textbox(label="Similarity", placeholder="Similarity Score...", lines=1) | |
output_converted_text = gr.Textbox(label="Converted Text", placeholder="Results will appear here...", lines=4) | |
output_message = gr.Textbox(label="Message", placeholder="System Message...", lines=2) | |
convert_button = gr.Button("Convert Text") | |
convert_button.click( | |
fn=convert_text_fre, | |
inputs=[input_text, grade_level_fre, output_input_reading_score], | |
outputs=[output_converted_text, output_similarity, output_reading_level, output_message] | |
) | |
with gr.Tab("SBERT"): | |
with gr.Row(): | |
input_text = gr.Textbox(label="Input Text", placeholder="Type here...", lines=4, scale = 2) | |
fetch_score_and_lvl_btn = gr.Button("Fetch Score and Level", scale = 1) | |
output_input_reading_score = gr.Textbox(label="Input Text Reading Score", placeholder="Input Text Reading Score...", lines=1, interactive=False) | |
output_input_reading_level = gr.Textbox(label="Input Text Reading Level", placeholder="Input Text Reading Level...", lines=1, interactive=False) | |
fetch_score_and_lvl_btn.click( | |
fn=fetch_readability_score_sbert, | |
inputs=[input_text], | |
outputs=[output_input_reading_score, output_input_reading_level] | |
) | |
grade_level_sbert = gr.Radio(choices=list(sbert_levels.keys()), label="Target Reading Level", interactive=True) | |
output_reading_level = gr.Textbox(label="Output Reading Level", placeholder="Output Reading Level...", lines=1) | |
output_similarity = gr.Textbox(label="Similarity", placeholder="Similarity Score...", lines=1) | |
output_converted_text = gr.Textbox(label="Converted Text", placeholder="Results will appear here...", lines=4) | |
output_message = gr.Textbox(label="Message", placeholder="System Message...", lines=2) | |
convert_button = gr.Button("Convert Text") | |
convert_button.click( | |
fn=convert_text_sbert, | |
inputs=[input_text, grade_level_fre, output_input_reading_score], | |
outputs=[output_converted_text, output_similarity, output_reading_level, output_message] | |
) | |
if __name__ == '__main__': | |
app.launch() |