Spaces:
Sleeping
Sleeping
Update README and mcp_server to reflect Gradio version 4.44.1 and enhance UI for text counting features
6622464
| import gradio as gr | |
| """Module for counting various text elements.""" | |
| def count_letters(word: str) -> int: | |
| """Count the number of letters in a word. | |
| Args: | |
| word: The word or text to count letters in. | |
| Returns: | |
| The number of alphabetic characters in the input. | |
| """ | |
| word = str(word) # Ensure string input | |
| return sum(1 for char in word if char.isalpha()) | |
| def count_words(sentence: str) -> int: | |
| """Count the number of words in a sentence. | |
| Args: | |
| sentence: The sentence or text to count words in. | |
| Returns: | |
| The number of words in the input text. | |
| """ | |
| sentence = str(sentence) # Ensure string input | |
| words = sentence.split() | |
| return len(words) | |
| def count_sentences(paragraph: str) -> int: | |
| """Count the number of sentences in a paragraph. | |
| Args: | |
| paragraph: The paragraph or text to count sentences in. | |
| Returns: | |
| The number of sentences in the input text. | |
| """ | |
| paragraph = str(paragraph) # Ensure string input | |
| # Split on multiple sentence terminators | |
| import re | |
| sentences = re.split(r'[.!?]+', paragraph) | |
| return len([s for s in sentences if s.strip()]) | |
| def count_paragraphs(text: str) -> int: | |
| """Count the number of paragraphs in a text. | |
| Args: | |
| text: The text to count paragraphs in. | |
| Returns: | |
| The number of paragraphs in the input text. | |
| """ | |
| text = str(text) # Ensure string input | |
| paragraphs = text.split('\n\n') | |
| return len([p for p in paragraphs if p.strip()]) | |
| def count_vowels(word: str) -> int: | |
| """Count the number of vowels in a word. | |
| Args: | |
| word: The word or text to count vowels in. | |
| Returns: | |
| The number of vowel characters in the input. | |
| """ | |
| word = str(word) # Ensure string input | |
| vowels = 'aeiouAEIOU' | |
| return sum(1 for char in word if char in vowels) | |
| def count_consonants(word: str) -> int: | |
| """Count the number of consonants in a word. | |
| Args: | |
| word: The word or text to count consonants in. | |
| Returns: | |
| The number of consonant characters in the input. | |
| """ | |
| word = str(word) # Ensure string input | |
| vowels = 'aeiouAEIOU' | |
| return sum(1 for char in word if char.isalpha() and char not in vowels) | |
| def count_special_characters(text: str) -> int: | |
| """Count the number of special characters in a text. | |
| Args: | |
| text: The text to count special characters in. | |
| Returns: | |
| The number of special characters in the input. | |
| """ | |
| text = str(text) # Ensure string input | |
| special_characters = '!@#$%^&*()_+-=[]{}|;:\'",.<>?/`~' | |
| return sum(1 for char in text if char in special_characters) | |
| def count_digits(text: str) -> int: | |
| """Count the number of digits in a text. | |
| Args: | |
| text: The text to count digits in. | |
| Returns: | |
| The number of digit characters in the input. | |
| """ | |
| text = str(text) # Ensure string input | |
| return sum(1 for char in text if char.isdigit()) | |
| def count_whitespaces(text: str) -> int: | |
| """Count the number of whitespace characters in a text. | |
| Args: | |
| text: The text to count whitespace characters in. | |
| Returns: | |
| The number of whitespace characters in the input. | |
| """ | |
| text = str(text) # Ensure string input | |
| return sum(1 for char in text if char.isspace()) | |
| def count_uppercase_letters(text: str) -> int: | |
| """Count the number of uppercase letters in a text. | |
| Args: | |
| text: The text to count uppercase letters in. | |
| Returns: | |
| The number of uppercase letters in the input. | |
| """ | |
| text = str(text) # Ensure string input | |
| return sum(1 for char in text if char.isupper()) | |
| def count_lowercase_letters(text: str) -> int: | |
| """Count the number of lowercase letters in a text. | |
| Args: | |
| text: The text to count lowercase letters in. | |
| Returns: | |
| The number of lowercase letters in the input. | |
| """ | |
| text = str(text) # Ensure string input | |
| return sum(1 for char in text if char.islower()) | |
| def count_unique_words(sentence: str) -> int: | |
| """Count the number of unique words in a sentence. | |
| Args: | |
| sentence: The sentence or text to count unique words in. | |
| Returns: | |
| The number of unique words in the input. | |
| """ | |
| sentence = str(sentence) # Ensure string input | |
| words = sentence.split() | |
| unique_words = set(words) | |
| return len(unique_words) | |
| def count_syllables(word: str) -> int: | |
| """Count the number of syllables in a word. | |
| Args: | |
| word: The word to count syllables in. | |
| Returns: | |
| The estimated number of syllables in the word. | |
| """ | |
| word = str(word).lower().strip() # Ensure string input and normalize | |
| if not word: | |
| return 0 | |
| vowels = 'aeiou' | |
| count = 0 | |
| in_vowel_group = False | |
| for char in word: | |
| if char in vowels: | |
| if not in_vowel_group: | |
| count += 1 | |
| in_vowel_group = True | |
| else: | |
| in_vowel_group = False | |
| # Handle silent 'e' at the end | |
| if word.endswith('e') and count > 1: | |
| count -= 1 | |
| # Ensure at least one syllable for non-empty words | |
| return max(1, count) if any(c.isalpha() for c in word) else 0 | |
| # Create a standard Gradio interface to test the functions | |
| with gr.Blocks() as demo: | |
| gr.Markdown("# Module for counting various text elements") | |
| with gr.Tab("Letter Counter"): | |
| with gr.Row(): | |
| text_input_1 = gr.Textbox(label="Enter text", placeholder="Type your text here...") | |
| result_output_1 = gr.Number(label="Letter Count") | |
| count_btn_1 = gr.Button("Count Letters") | |
| count_btn_1.click(fn=count_letters, inputs=text_input_1, outputs=result_output_1) | |
| with gr.Tab("Word Counter"): | |
| with gr.Row(): | |
| text_input_2 = gr.Textbox(label="Enter text", placeholder="Type your text here...") | |
| result_output_2 = gr.Number(label="Word Count") | |
| count_btn_2 = gr.Button("Count Words") | |
| count_btn_2.click(fn=count_words, inputs=text_input_2, outputs=result_output_2) | |
| with gr.Tab("Sentence Counter"): | |
| with gr.Row(): | |
| text_input_3 = gr.Textbox(label="Enter text", placeholder="Type your text here...") | |
| result_output_3 = gr.Number(label="Sentence Count") | |
| count_btn_3 = gr.Button("Count Sentences") | |
| count_btn_3.click(fn=count_sentences, inputs=text_input_3, outputs=result_output_3) | |
| with gr.Tab("Paragraph Counter"): | |
| with gr.Row(): | |
| text_input_4 = gr.Textbox(label="Enter text", placeholder="Type your text here...") | |
| result_output_4 = gr.Number(label="Paragraph Count") | |
| count_btn_4 = gr.Button("Count Paragraphs") | |
| count_btn_4.click(fn=count_paragraphs, inputs=text_input_4, outputs=result_output_4) | |
| with gr.Tab("Vowel Counter"): | |
| with gr.Row(): | |
| text_input_5 = gr.Textbox(label="Enter text", placeholder="Type your text here...") | |
| result_output_5 = gr.Number(label="Vowel Count") | |
| count_btn_5 = gr.Button("Count Vowels") | |
| count_btn_5.click(fn=count_vowels, inputs=text_input_5, outputs=result_output_5) | |
| with gr.Tab("Consonant Counter"): | |
| with gr.Row(): | |
| text_input_6 = gr.Textbox(label="Enter text", placeholder="Type your text here...") | |
| result_output_6 = gr.Number(label="Consonant Count") | |
| count_btn_6 = gr.Button("Count Consonants") | |
| count_btn_6.click(fn=count_consonants, inputs=text_input_6, outputs=result_output_6) | |
| with gr.Tab("Special Character Counter"): | |
| with gr.Row(): | |
| text_input_7 = gr.Textbox(label="Enter text", placeholder="Type your text here...") | |
| result_output_7 = gr.Number(label="Special Character Count") | |
| count_btn_7 = gr.Button("Count Special Characters") | |
| count_btn_7.click(fn=count_special_characters, inputs=text_input_7, outputs=result_output_7) | |
| with gr.Tab("Digit Counter"): | |
| with gr.Row(): | |
| text_input_8 = gr.Textbox(label="Enter text", placeholder="Type your text here...") | |
| result_output_8 = gr.Number(label="Digit Count") | |
| count_btn_8 = gr.Button("Count Digits") | |
| count_btn_8.click(fn=count_digits, inputs=text_input_8, outputs=result_output_8) | |
| with gr.Tab("Whitespace Counter"): | |
| with gr.Row(): | |
| text_input_9 = gr.Textbox(label="Enter text", placeholder="Type your text here...") | |
| result_output_9 = gr.Number(label="Whitespace Count") | |
| count_btn_9 = gr.Button("Count Whitespaces") | |
| count_btn_9.click(fn=count_whitespaces, inputs=text_input_9, outputs=result_output_9) | |
| with gr.Tab("Uppercase Letter Counter"): | |
| with gr.Row(): | |
| text_input_10 = gr.Textbox(label="Enter text", placeholder="Type your text here...") | |
| result_output_10 = gr.Number(label="Uppercase Letter Count") | |
| count_btn_10 = gr.Button("Count Uppercase Letters") | |
| count_btn_10.click(fn=count_uppercase_letters, inputs=text_input_10, outputs=result_output_10) | |
| with gr.Tab("Lowercase Letter Counter"): | |
| with gr.Row(): | |
| text_input_11 = gr.Textbox(label="Enter text", placeholder="Type your text here...") | |
| result_output_11 = gr.Number(label="Lowercase Letter Count") | |
| count_btn_11 = gr.Button("Count Lowercase Letters") | |
| count_btn_11.click(fn=count_lowercase_letters, inputs=text_input_11, outputs=result_output_11) | |
| with gr.Tab("Unique Word Counter"): | |
| with gr.Row(): | |
| text_input_12 = gr.Textbox(label="Enter text", placeholder="Type your text here...") | |
| result_output_12 = gr.Number(label="Unique Word Count") | |
| count_btn_12 = gr.Button("Count Unique Words") | |
| count_btn_12.click(fn=count_unique_words, inputs=text_input_12, outputs=result_output_12) | |
| with gr.Tab("Syllable Counter"): | |
| with gr.Row(): | |
| text_input_13 = gr.Textbox(label="Enter text", placeholder="Type your text here...") | |
| result_output_13 = gr.Number(label="Syllable Count") | |
| count_btn_13 = gr.Button("Count Syllables") | |
| count_btn_13.click(fn=count_syllables, inputs=text_input_13, outputs=result_output_13) | |
| if __name__ == "__main__": | |
| demo.launch() | |