mobrown commited on
Commit
cff83e2
1 Parent(s): 25a5b44

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +65 -0
app.py ADDED
@@ -0,0 +1,65 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+
3
+ # Example texts
4
+ example_texts = {
5
+ "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.",
6
+ "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."
7
+ }
8
+
9
+
10
+ def get_readability_level(fog_index):
11
+ if fog_index <= 12:
12
+ return "Middle School Level"
13
+ elif fog_index <= 16:
14
+ return "High School Senior Level"
15
+ elif fog_index <= 18:
16
+ return "College Junior Level"
17
+ else:
18
+ return "Graduate School Level"
19
+
20
+
21
+ def calculate_fog_index(text):
22
+ if not text.strip(): # If the text is empty or only whitespace, return a placeholder
23
+ return "Enter text to see the Fog Index and readability level."
24
+ # Split text into sentences and words
25
+ sentences = text.split('.')
26
+ words = text.split()
27
+ complex_words = [word for word in words if len(word) > 7] # Simplified definition for complex words
28
+
29
+ # Calculate Gunning Fog Index
30
+ average_sentence_length = len(words) / max(len(sentences), 1) # Avoid division by zero
31
+ percentage_complex_words = (len(complex_words) / max(len(words), 1)) * 100 # Avoid division by zero
32
+ fog_index = 0.4 * (average_sentence_length + percentage_complex_words)
33
+
34
+ readability_level = get_readability_level(fog_index)
35
+
36
+ return f"Fog Index: {fog_index:.2f}. Readability is at the {readability_level}."
37
+
38
+
39
+ def update_text_area(choice):
40
+ # Update the text area with the selected example text or clear it for custom text
41
+ return example_texts.get(choice, "")
42
+
43
+
44
+ css = ".label-chicago { text-align: right; width: 100%; font-size: 0.75em; margin-top: 20px; } \
45
+ .instructions { font-size: 1.25em; font-weight: bold; }"
46
+
47
+ with gr.Blocks(css=css) as app:
48
+ gr.Markdown(
49
+ "<div class='instructions'>"
50
+ "Start by using the buttons to select a text sample or enter your custom text.<br>"
51
+ "The Fog Index and readability level will update automatically."
52
+ "</div>"
53
+ )
54
+ choice = gr.Radio(choices=["Basic Text", "Nvidia Earnings", "Custom Text"], label="Select Text", value="")
55
+ analyze_text_area = gr.TextArea(label="Analyze Text", lines=10,
56
+ placeholder="Select an example text or enter your custom text here.",
57
+ interactive=True)
58
+ fog_index_display = gr.Label()
59
+
60
+ choice.change(update_text_area, inputs=[choice], outputs=[analyze_text_area])
61
+ analyze_text_area.change(calculate_fog_index, inputs=[analyze_text_area], outputs=[fog_index_display])
62
+
63
+ gr.Markdown("<div class='label-chicago'>App by Monika Brown</div>")
64
+
65
+ app.launch()