Christopherygk commited on
Commit
82ef429
·
1 Parent(s): ff8d782

Add initial implementation of text counting features and configuration files

Browse files
Files changed (5) hide show
  1. .gitignore +7 -0
  2. README.md +190 -11
  3. agent.json +16 -0
  4. mcp.json +149 -0
  5. mcp_server.py +225 -0
.gitignore ADDED
@@ -0,0 +1,7 @@
 
 
 
 
 
 
 
 
1
+ .env
2
+ node_modules/
3
+ dist/
4
+ .vscode/
5
+ .DS_Store
6
+ .venv
7
+ __pycache__/
README.md CHANGED
@@ -1,13 +1,192 @@
1
- ---
2
- title: Text Counting
3
- emoji: 💻
4
- colorFrom: indigo
5
- colorTo: pink
6
- sdk: gradio
7
- sdk_version: 5.49.1
8
- app_file: app.py
9
- pinned: false
10
- short_description: MCP server for comprehensive text analysis and counting oper
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
11
  ---
12
 
13
- Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
 
1
+ # Text Counting MCP Server
2
+
3
+ A comprehensive text analysis and counting operations server built with Gradio, featuring both a web interface and MCP (Model Context Protocol) server capabilities.
4
+
5
+ ## Features
6
+
7
+ This application provides 13 different text counting and analysis functions:
8
+
9
+ - **Letter Counter**: Count alphabetic characters in text
10
+ - **Word Counter**: Count words in sentences or paragraphs
11
+ - **Sentence Counter**: Count sentences using multiple punctuation marks (., !, ?)
12
+ - **Paragraph Counter**: Count paragraphs separated by double line breaks
13
+ - **Vowel Counter**: Count vowel characters (a, e, i, o, u)
14
+ - **Consonant Counter**: Count consonant characters
15
+ - **Special Character Counter**: Count special symbols and punctuation
16
+ - **Digit Counter**: Count numeric digits (0-9)
17
+ - **Whitespace Counter**: Count spaces, tabs, and line breaks
18
+ - **Uppercase Letter Counter**: Count capital letters
19
+ - **Lowercase Letter Counter**: Count lowercase letters
20
+ - **Unique Word Counter**: Count distinct words in text
21
+ - **Syllable Counter**: Estimate syllables with enhanced algorithm
22
+
23
+ ## Quick Start
24
+
25
+ ### Prerequisites
26
+
27
+ - Python 3.8+
28
+ - pip package manager
29
+
30
+ ### Installation
31
+
32
+ 1. **Clone or download the project**
33
+ ```bash
34
+ git clone <repository-url>
35
+ cd hugging-face
36
+ ```
37
+
38
+ 2. **Create and activate virtual environment** (recommended)
39
+ ```bash
40
+ python -m venv .venv
41
+
42
+ # On Windows:
43
+ .venv\Scripts\activate
44
+
45
+ # On macOS/Linux:
46
+ source .venv/bin/activate
47
+ ```
48
+
49
+ 3. **Install dependencies**
50
+ ```bash
51
+ pip install gradio
52
+ ```
53
+
54
+ ### Running the Application
55
+
56
+ **Web Interface Mode:**
57
+ ```bash
58
+ python mcp_server.py
59
+ ```
60
+
61
+ The application will launch at `http://localhost:7860` with a tabbed interface for each counting function.
62
+
63
+ **MCP Server Mode:**
64
+ ```bash
65
+ # Enable MCP server functionality by setting mcp_server=True in the code
66
+ python mcp_server.py
67
+ ```
68
+
69
+ ## Usage Examples
70
+
71
+ ### Web Interface
72
+ 1. Navigate to the launched URL (typically `http://localhost:7860`)
73
+ 2. Select the desired counting function from the tabs
74
+ 3. Enter your text in the input field
75
+ 4. View the results instantly
76
+
77
+ ### Python API
78
+ ```python
79
+ from mcp_server import count_letters, count_words, count_syllables
80
+
81
+ # Count letters
82
+ result = count_letters("Hello World!") # Returns: 10
83
+
84
+ # Count words
85
+ result = count_words("This is a test sentence.") # Returns: 5
86
+
87
+ # Count syllables
88
+ result = count_syllables("beautiful") # Returns: 3
89
+ ```
90
+
91
+ ## Project Structure
92
+
93
+ ```
94
+ hugging-face/
95
+ ├── mcp_server.py # Main application with Gradio interface
96
+ ├── mcp.json # MCP server configuration
97
+ ├── agent.json # Agent configuration
98
+ ├── README.md # This file
99
+ ├── .gitignore # Git ignore rules
100
+ └── .venv/ # Virtual environment (created after setup)
101
+ ```
102
+
103
+ ## Configuration
104
+
105
+ ### MCP Server Configuration
106
+ The `mcp.json` file defines the MCP server capabilities and tool definitions. It includes:
107
+ - Server metadata (name, version, description)
108
+ - Tool definitions with parameter schemas
109
+ - Capability declarations
110
+
111
+ ### Environment Setup
112
+ - Python virtual environment in `.venv/`
113
+ - Gradio for web interface
114
+ - MCP server capabilities built into Python application
115
+
116
+ ## Function Details
117
+
118
+ ### Enhanced Features
119
+
120
+ 1. **Robust Input Handling**: All functions accept any input type and convert to string
121
+ 2. **Improved Algorithms**:
122
+ - Sentence counting uses regex for multiple punctuation marks
123
+ - Syllable counting handles silent 'e' and edge cases
124
+ 3. **Error Handling**: Functions gracefully handle empty inputs and special cases
125
+ 4. **Type Safety**: Full type hints and comprehensive docstrings
126
+
127
+ ### Algorithm Highlights
128
+
129
+ - **Syllable Counter**: Uses vowel grouping with silent 'e' detection
130
+ - **Sentence Counter**: Regex-based splitting on `.!?` patterns
131
+ - **Letter Counter**: Only counts alphabetic characters (excludes numbers/symbols)
132
+
133
+ ## Development
134
+
135
+ ### Code Quality Features
136
+ - Complete type hints for all functions
137
+ - Comprehensive docstrings with Args: blocks
138
+ - Input validation and type conversion
139
+ - Error handling for edge cases
140
+ - Consistent code formatting
141
+
142
+ ### Adding New Functions
143
+ 1. Define the function with proper type hints
144
+ 2. Add comprehensive docstring with Args: and Returns: sections
145
+ 3. Include input validation (`str(input)` conversion)
146
+ 4. Add the function to the Gradio interface
147
+ 5. Update MCP configuration if needed
148
+
149
+ ## Contributing
150
+
151
+ 1. Fork the repository
152
+ 2. Create a feature branch (`git checkout -b feature/new-counter`)
153
+ 3. Make your changes following the code quality standards
154
+ 4. Add tests if applicable
155
+ 5. Submit a pull request
156
+
157
+ ## License
158
+
159
+ This project is licensed under the ISC License - see the LICENSE file for details.
160
+
161
+ ## Troubleshooting
162
+
163
+ ### Common Issues
164
+
165
+ **"ModuleNotFoundError: No module named 'gradio'"**
166
+ ```bash
167
+ pip install gradio
168
+ ```
169
+
170
+ **"JSON-RPC error: Not Acceptable: Client must accept text/event-stream"**
171
+ - This occurs when `mcp_server=True` is enabled but the client doesn't support event streams
172
+ - For web interface only, ensure `mcp_server=False` or remove the parameter
173
+
174
+ **Virtual Environment Issues**
175
+ ```bash
176
+ # Recreate virtual environment
177
+ rm -rf .venv
178
+ python -m venv .venv
179
+ .venv\Scripts\activate # Windows
180
+ pip install gradio
181
+ ```
182
+
183
+ ## Performance
184
+
185
+ - Lightweight algorithms optimized for text processing
186
+ - Efficient character iteration using generator expressions
187
+ - Memory-efficient for large text inputs
188
+ - Real-time processing suitable for interactive use
189
+
190
  ---
191
 
192
+ **Built using Python, Gradio, and MCP**
agent.json ADDED
@@ -0,0 +1,16 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "name": "text-counting-agent",
3
+ "description": "AI agent with text counting and analysis capabilities",
4
+ "model": "Qwen/Qwen2.5-72B-Instruct",
5
+ "provider": "nebius",
6
+ "system_prompt": "You are a helpful AI assistant specialized in text analysis and counting operations. Use the available text counting tools to provide accurate analysis of text content including letters, words, sentences, paragraphs, vowels, consonants, and other text elements.",
7
+ "servers": [
8
+ {
9
+ "name": "text-counting-server",
10
+ "type": "stdio",
11
+ "command": "D:/source/hugging-face/.venv/Scripts/python.exe",
12
+ "args": ["mcp_server.py"],
13
+ "cwd": "D:/source/hugging-face"
14
+ }
15
+ ]
16
+ }
mcp.json ADDED
@@ -0,0 +1,149 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "name": "text-counting-mcp-server",
3
+ "version": "1.0.0",
4
+ "description": "MCP server for comprehensive text analysis and counting operations",
5
+ "main": "mcp_server.py",
6
+ "type": "mcp-server",
7
+ "tools": [
8
+ {
9
+ "name": "count_letters",
10
+ "description": "Count the number of letters in a word or text",
11
+ "parameters": {
12
+ "text": {
13
+ "type": "string",
14
+ "description": "The text to count letters in"
15
+ }
16
+ }
17
+ },
18
+ {
19
+ "name": "count_words",
20
+ "description": "Count the number of words in a sentence or text",
21
+ "parameters": {
22
+ "text": {
23
+ "type": "string",
24
+ "description": "The text to count words in"
25
+ }
26
+ }
27
+ },
28
+ {
29
+ "name": "count_sentences",
30
+ "description": "Count the number of sentences in a paragraph or text",
31
+ "parameters": {
32
+ "text": {
33
+ "type": "string",
34
+ "description": "The text to count sentences in"
35
+ }
36
+ }
37
+ },
38
+ {
39
+ "name": "count_paragraphs",
40
+ "description": "Count the number of paragraphs in a text",
41
+ "parameters": {
42
+ "text": {
43
+ "type": "string",
44
+ "description": "The text to count paragraphs in"
45
+ }
46
+ }
47
+ },
48
+ {
49
+ "name": "count_vowels",
50
+ "description": "Count the number of vowels in a word or text",
51
+ "parameters": {
52
+ "text": {
53
+ "type": "string",
54
+ "description": "The text to count vowels in"
55
+ }
56
+ }
57
+ },
58
+ {
59
+ "name": "count_consonants",
60
+ "description": "Count the number of consonants in a word or text",
61
+ "parameters": {
62
+ "text": {
63
+ "type": "string",
64
+ "description": "The text to count consonants in"
65
+ }
66
+ }
67
+ },
68
+ {
69
+ "name": "count_special_characters",
70
+ "description": "Count the number of special characters in a text",
71
+ "parameters": {
72
+ "text": {
73
+ "type": "string",
74
+ "description": "The text to count special characters in"
75
+ }
76
+ }
77
+ },
78
+ {
79
+ "name": "count_digits",
80
+ "description": "Count the number of digits in a text",
81
+ "parameters": {
82
+ "text": {
83
+ "type": "string",
84
+ "description": "The text to count digits in"
85
+ }
86
+ }
87
+ },
88
+ {
89
+ "name": "count_whitespaces",
90
+ "description": "Count the number of whitespace characters in a text",
91
+ "parameters": {
92
+ "text": {
93
+ "type": "string",
94
+ "description": "The text to count whitespaces in"
95
+ }
96
+ }
97
+ },
98
+ {
99
+ "name": "count_uppercase_letters",
100
+ "description": "Count the number of uppercase letters in a text",
101
+ "parameters": {
102
+ "text": {
103
+ "type": "string",
104
+ "description": "The text to count uppercase letters in"
105
+ }
106
+ }
107
+ },
108
+ {
109
+ "name": "count_lowercase_letters",
110
+ "description": "Count the number of lowercase letters in a text",
111
+ "parameters": {
112
+ "text": {
113
+ "type": "string",
114
+ "description": "The text to count lowercase letters in"
115
+ }
116
+ }
117
+ },
118
+ {
119
+ "name": "count_unique_words",
120
+ "description": "Count the number of unique words in a sentence or text",
121
+ "parameters": {
122
+ "text": {
123
+ "type": "string",
124
+ "description": "The text to count unique words in"
125
+ }
126
+ }
127
+ },
128
+ {
129
+ "name": "count_syllables",
130
+ "description": "Count the number of syllables in a word",
131
+ "parameters": {
132
+ "text": {
133
+ "type": "string",
134
+ "description": "The word to count syllables in"
135
+ }
136
+ }
137
+ }
138
+ ],
139
+ "capabilities": {
140
+ "text_analysis": true,
141
+ "counting_operations": true,
142
+ "linguistic_analysis": true
143
+ },
144
+ "config": {
145
+ "language": "python",
146
+ "runtime": "python3.12",
147
+ "entry_point": "mcp_server.py"
148
+ }
149
+ }
mcp_server.py ADDED
@@ -0,0 +1,225 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+
3
+ """Module for counting various text elements."""
4
+
5
+ def count_letters(word: str) -> int:
6
+ """Count the number of letters in a word.
7
+
8
+ Args:
9
+ word: The word or text to count letters in.
10
+
11
+ Returns:
12
+ The number of alphabetic characters in the input.
13
+ """
14
+ word = str(word) # Ensure string input
15
+ return sum(1 for char in word if char.isalpha())
16
+
17
+ def count_words(sentence: str) -> int:
18
+ """Count the number of words in a sentence.
19
+
20
+ Args:
21
+ sentence: The sentence or text to count words in.
22
+
23
+ Returns:
24
+ The number of words in the input text.
25
+ """
26
+ sentence = str(sentence) # Ensure string input
27
+ words = sentence.split()
28
+ return len(words)
29
+
30
+ def count_sentences(paragraph: str) -> int:
31
+ """Count the number of sentences in a paragraph.
32
+
33
+ Args:
34
+ paragraph: The paragraph or text to count sentences in.
35
+
36
+ Returns:
37
+ The number of sentences in the input text.
38
+ """
39
+ paragraph = str(paragraph) # Ensure string input
40
+ # Split on multiple sentence terminators
41
+ import re
42
+ sentences = re.split(r'[.!?]+', paragraph)
43
+ return len([s for s in sentences if s.strip()])
44
+
45
+ def count_paragraphs(text: str) -> int:
46
+ """Count the number of paragraphs in a text.
47
+
48
+ Args:
49
+ text: The text to count paragraphs in.
50
+
51
+ Returns:
52
+ The number of paragraphs in the input text.
53
+ """
54
+ text = str(text) # Ensure string input
55
+ paragraphs = text.split('\n\n')
56
+ return len([p for p in paragraphs if p.strip()])
57
+
58
+ def count_vowels(word: str) -> int:
59
+ """Count the number of vowels in a word.
60
+
61
+ Args:
62
+ word: The word or text to count vowels in.
63
+
64
+ Returns:
65
+ The number of vowel characters in the input.
66
+ """
67
+ word = str(word) # Ensure string input
68
+ vowels = 'aeiouAEIOU'
69
+ return sum(1 for char in word if char in vowels)
70
+
71
+ def count_consonants(word: str) -> int:
72
+ """Count the number of consonants in a word.
73
+
74
+ Args:
75
+ word: The word or text to count consonants in.
76
+
77
+ Returns:
78
+ The number of consonant characters in the input.
79
+ """
80
+ word = str(word) # Ensure string input
81
+ vowels = 'aeiouAEIOU'
82
+ return sum(1 for char in word if char.isalpha() and char not in vowels)
83
+
84
+ def count_special_characters(text: str) -> int:
85
+ """Count the number of special characters in a text.
86
+
87
+ Args:
88
+ text: The text to count special characters in.
89
+
90
+ Returns:
91
+ The number of special characters in the input.
92
+ """
93
+ text = str(text) # Ensure string input
94
+ special_characters = '!@#$%^&*()_+-=[]{}|;:\'",.<>?/`~'
95
+ return sum(1 for char in text if char in special_characters)
96
+
97
+ def count_digits(text: str) -> int:
98
+ """Count the number of digits in a text.
99
+
100
+ Args:
101
+ text: The text to count digits in.
102
+
103
+ Returns:
104
+ The number of digit characters in the input.
105
+ """
106
+ text = str(text) # Ensure string input
107
+ return sum(1 for char in text if char.isdigit())
108
+
109
+ def count_whitespaces(text: str) -> int:
110
+ """Count the number of whitespace characters in a text.
111
+
112
+ Args:
113
+ text: The text to count whitespace characters in.
114
+
115
+ Returns:
116
+ The number of whitespace characters in the input.
117
+ """
118
+ text = str(text) # Ensure string input
119
+ return sum(1 for char in text if char.isspace())
120
+
121
+ def count_uppercase_letters(text: str) -> int:
122
+ """Count the number of uppercase letters in a text.
123
+
124
+ Args:
125
+ text: The text to count uppercase letters in.
126
+
127
+ Returns:
128
+ The number of uppercase letters in the input.
129
+ """
130
+ text = str(text) # Ensure string input
131
+ return sum(1 for char in text if char.isupper())
132
+
133
+ def count_lowercase_letters(text: str) -> int:
134
+ """Count the number of lowercase letters in a text.
135
+
136
+ Args:
137
+ text: The text to count lowercase letters in.
138
+
139
+ Returns:
140
+ The number of lowercase letters in the input.
141
+ """
142
+ text = str(text) # Ensure string input
143
+ return sum(1 for char in text if char.islower())
144
+
145
+ def count_unique_words(sentence: str) -> int:
146
+ """Count the number of unique words in a sentence.
147
+
148
+ Args:
149
+ sentence: The sentence or text to count unique words in.
150
+
151
+ Returns:
152
+ The number of unique words in the input.
153
+ """
154
+ sentence = str(sentence) # Ensure string input
155
+ words = sentence.split()
156
+ unique_words = set(words)
157
+ return len(unique_words)
158
+
159
+ def count_syllables(word: str) -> int:
160
+ """Count the number of syllables in a word.
161
+
162
+ Args:
163
+ word: The word to count syllables in.
164
+
165
+ Returns:
166
+ The estimated number of syllables in the word.
167
+ """
168
+ word = str(word).lower().strip() # Ensure string input and normalize
169
+ if not word:
170
+ return 0
171
+
172
+ vowels = 'aeiou'
173
+ count = 0
174
+ in_vowel_group = False
175
+
176
+ for char in word:
177
+ if char in vowels:
178
+ if not in_vowel_group:
179
+ count += 1
180
+ in_vowel_group = True
181
+ else:
182
+ in_vowel_group = False
183
+
184
+ # Handle silent 'e' at the end
185
+ if word.endswith('e') and count > 1:
186
+ count -= 1
187
+
188
+ # Ensure at least one syllable for non-empty words
189
+ return max(1, count) if any(c.isalpha() for c in word) else 0
190
+
191
+
192
+ # Create a standard Gradio interface to test the functions
193
+
194
+ with gr.Blocks() as demo:
195
+ gr.Markdown("# Module for counting various text elements")
196
+ with gr.Tab("Letter Counter"):
197
+ gr.Interface(fn=count_letters, inputs="text", outputs="number")
198
+ with gr.Tab("Word Counter"):
199
+ gr.Interface(fn=count_words, inputs="text", outputs="number")
200
+ with gr.Tab("Sentence Counter"):
201
+ gr.Interface(fn=count_sentences, inputs="text", outputs="number")
202
+ with gr.Tab("Paragraph Counter"):
203
+ gr.Interface(fn=count_paragraphs, inputs="text", outputs="number")
204
+ with gr.Tab("Vowel Counter"):
205
+ gr.Interface(fn=count_vowels, inputs="text", outputs="number")
206
+ with gr.Tab("Consonant Counter"):
207
+ gr.Interface(fn=count_consonants, inputs="text", outputs="number")
208
+ with gr.Tab("Special Character Counter"):
209
+ gr.Interface(fn=count_special_characters, inputs="text", outputs="number")
210
+ with gr.Tab("Digit Counter"):
211
+ gr.Interface(fn=count_digits, inputs="text", outputs="number")
212
+ with gr.Tab("Whitespace Counter"):
213
+ gr.Interface(fn=count_whitespaces, inputs="text", outputs="number")
214
+ with gr.Tab("Uppercase Letter Counter"):
215
+ gr.Interface(fn=count_uppercase_letters, inputs="text", outputs="number")
216
+ with gr.Tab("Lowercase Letter Counter"):
217
+ gr.Interface(fn=count_lowercase_letters, inputs="text", outputs="number")
218
+ with gr.Tab("Unique Word Counter"):
219
+ gr.Interface(fn=count_unique_words, inputs="text", outputs="number")
220
+ with gr.Tab("Syllable Counter"):
221
+ gr.Interface(fn=count_syllables, inputs="text", outputs="number")
222
+
223
+ if __name__ == "__main__":
224
+ demo.launch(mcp_server=True)
225
+