Spaces:
Sleeping
Sleeping
File size: 3,161 Bytes
7b28cbc |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
from bert_similarity import get_similarity
from text_generator import get_gpt_response
from textstat import flesch_reading_ease
def generate_user_prompt(prompt_type, base_text):
prompts = {
"too_simple": f"""
Convert this text to a higher reading level of the original text.
The higher reading level text should have more syllables per word and more words per sentence.
It should retain the core meaning of the original text.
Here is the text:
{base_text}
""",
"too_complex": f"""
Convert this text to a simpler version of the original text.
The simpler versions of text have fewer syllables per word and fewer words per sentence.
It should retain the core meaning of the original text.
Here is the text:
{base_text}
"""
}
return prompts[prompt_type].format(base_text=base_text)
def generate_similar_sentence(input_text, min_reading_level, max_reading_level, min_entailment, system_prompt, max_iter):
i = 0
completed = False
user_prompt = ""
curr_reading_level = flesch_reading_ease(input_text)
input_reading_level = flesch_reading_ease(input_text)
curr_text = input_text
response = None
similarity = 0
reading_level = 0
print(f"Current reading level is: {curr_reading_level}")
if curr_reading_level > min_reading_level and curr_reading_level < max_reading_level:
return input_text, 1, curr_reading_level, input_reading_level, "Input text was already within the target reading level!"
else:
while i < max_iter and not completed:
if curr_reading_level > max_reading_level:
print(f"Too simple, current reading level is {curr_reading_level}")
user_prompt = generate_user_prompt("too_simple", curr_text)
elif curr_reading_level < min_reading_level:
print(f"Too complex, current reading level is {curr_reading_level}")
user_prompt = generate_user_prompt("too_complex", curr_text)
elif similarity < min_entailment:
print(f"Entailment level is too low: {similarity}")
user_prompt = f"Can you convert this text '{input_text}' to a grade level more similar to this text '{curr_text}'"
response = get_gpt_response(user_prompt, system_prompt)
similarity = get_similarity(response, input_text)
reading_level = flesch_reading_ease(response)
if similarity >= min_entailment and min_reading_level <= reading_level <= max_reading_level:
completed = True
curr_text = response
curr_reading_level = reading_level
print(response)
i += 1
if completed:
return response, similarity, reading_level, input_reading_level, "Success! Please see the converted text at your target reading level."
else:
return response, similarity, reading_level, input_reading_level, "Failed. We could not reach the target reading level while maintaining the text meaning." |