import gradio as gr # Example texts 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 <= 12: return "Middle School Level" elif fog_index <= 16: return "High School Senior Level" elif fog_index <= 18: return "College Junior Level" else: return "Graduate School Level" def calculate_fog_index(text): if not text.strip(): # If the text is empty or only whitespace, return a placeholder return "Enter text to see the Fog Index and readability level." # Split text into sentences and words sentences = text.split('.') words = text.split() complex_words = [word for word in words if len(word) > 7] # Simplified definition for complex words # Calculate Gunning Fog Index average_sentence_length = len(words) / max(len(sentences), 1) # Avoid division by zero percentage_complex_words = (len(complex_words) / max(len(words), 1)) * 100 # Avoid division by zero 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): # Update the text area with the selected example text or clear it for custom text 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( "
" "Start by using the buttons to select a text sample or enter your custom text.
" "The Fog Index and readability level will update automatically." "
" ) 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("
App by Monika Brown
") app.launch()