|
import gradio as gr |
|
|
|
|
|
example_texts = { |
|
"Basic Text": "The quick brown fox jumps over the lazy dog. This sentence contains every letter of the alphabet, making it a pangram. Pangrams are used to display font samples and test typography. They also serve as exercises for handwriting and calligraphy. This particular pangram is well-known and widely used in English.", |
|
"Nvidia Earnings": "Artificial Intelligence, or AI, is a field of computer science that aims to create machines capable of intelligent behavior. In the simplest terms, AI seeks to enable machines to perform tasks that would typically require human intelligence. These tasks include problem-solving, recognizing patterns, understanding language, and making decisions. The development of AI has the potential to transform industries and societies in profound ways." |
|
} |
|
|
|
|
|
def get_readability_level(fog_index): |
|
if fog_index < 4: |
|
return "First to Third Grade Level" |
|
if fog_index < 7: |
|
return "Elementary School Level" |
|
if fog_index < 9: |
|
return "Junior High School Level" |
|
elif fog_index < 12: |
|
return "High School Level" |
|
elif fog_index < 13: |
|
return "High School Senior Level" |
|
elif fog_index < 17: |
|
return "College Level" |
|
else: |
|
return "Graduate School Level" |
|
|
|
|
|
def calculate_fog_index(text): |
|
if not text.strip(): |
|
return "Enter text to see the Fog Index and readability level." |
|
|
|
sentences = text.split('.') |
|
words = text.split() |
|
complex_words = [word for word in words if len(word) > 7] |
|
|
|
|
|
average_sentence_length = len(words) / max(len(sentences), 1) |
|
percentage_complex_words = (len(complex_words) / max(len(words), 1)) * 100 |
|
fog_index = 0.4 * (average_sentence_length + percentage_complex_words) |
|
|
|
readability_level = get_readability_level(fog_index) |
|
|
|
return f"Fog Index: {fog_index:.2f}. Readability is at the {readability_level}." |
|
|
|
|
|
def update_text_area(choice): |
|
|
|
return example_texts.get(choice, "") |
|
|
|
|
|
css = ".label-chicago { text-align: right; width: 100%; font-size: 0.75em; margin-top: 20px; } \ |
|
.instructions { font-size: 1.25em; font-weight: bold; }" |
|
|
|
with gr.Blocks(css=css) as app: |
|
gr.Markdown( |
|
"<div class='instructions'>" |
|
"Start by using the buttons to select a text sample or enter your custom text.<br>" |
|
"The Fog Index and readability level will update automatically." |
|
"</div>" |
|
) |
|
choice = gr.Radio(choices=["Basic Text", "Nvidia Earnings", "Custom Text"], label="Select Text", value="") |
|
analyze_text_area = gr.TextArea(label="Analyze Text", lines=10, |
|
placeholder="Select an example text or enter your custom text here.", |
|
interactive=True) |
|
fog_index_display = gr.Label() |
|
|
|
choice.change(update_text_area, inputs=[choice], outputs=[analyze_text_area]) |
|
analyze_text_area.change(calculate_fog_index, inputs=[analyze_text_area], outputs=[fog_index_display]) |
|
|
|
gr.Markdown("<div class='label-chicago'>App by Monika Brown</div>") |
|
|
|
app.launch(share=True) |
|
|