Spaces:
Sleeping
A newer version of the Gradio SDK is available:
6.5.1
title: Text Counting MCP Server
emoji: π’
colorFrom: blue
colorTo: green
sdk: gradio
sdk_version: 4.44.1
app_file: mcp_server.py
pinned: false
Text Counting MCP Server
A comprehensive text analysis and counting operations server built with Gradio, featuring both a web interface and MCP (Model Context Protocol) server capabilities.
Features
This application provides 13 different text counting and analysis functions:
- Letter Counter: Count alphabetic characters in text
- Word Counter: Count words in sentences or paragraphs
- Sentence Counter: Count sentences using multiple punctuation marks (., !, ?)
- Paragraph Counter: Count paragraphs separated by double line breaks
- Vowel Counter: Count vowel characters (a, e, i, o, u)
- Consonant Counter: Count consonant characters
- Special Character Counter: Count special symbols and punctuation
- Digit Counter: Count numeric digits (0-9)
- Whitespace Counter: Count spaces, tabs, and line breaks
- Uppercase Letter Counter: Count capital letters
- Lowercase Letter Counter: Count lowercase letters
- Unique Word Counter: Count distinct words in text
- Syllable Counter: Estimate syllables with enhanced algorithm
Quick Start
Prerequisites
- Python 3.8+
- pip package manager
Installation
Clone or download the project
git clone <repository-url> cd hugging-faceCreate and activate virtual environment (recommended)
python -m venv .venv # On Windows: .venv\Scripts\activate # On macOS/Linux: source .venv/bin/activateInstall dependencies
pip install gradio
Running the Application
Web Interface Mode:
python mcp_server.py
The application will launch at http://localhost:7860 with a tabbed interface for each counting function.
MCP Server Mode:
# Enable MCP server functionality by setting mcp_server=True in the code
python mcp_server.py
Usage Examples
Web Interface
- Navigate to the launched URL (typically
http://localhost:7860) - Select the desired counting function from the tabs
- Enter your text in the input field
- View the results instantly
Python API
from mcp_server import count_letters, count_words, count_syllables
# Count letters
result = count_letters("Hello World!") # Returns: 10
# Count words
result = count_words("This is a test sentence.") # Returns: 5
# Count syllables
result = count_syllables("beautiful") # Returns: 3
Project Structure
hugging-face/
βββ mcp_server.py # Main application with Gradio interface
βββ mcp.json # MCP server configuration
βββ agent.json # Agent configuration
βββ README.md # This file
βββ .gitignore # Git ignore rules
βββ .venv/ # Virtual environment (created after setup)
Configuration
MCP Server Configuration
The mcp.json file defines the MCP server capabilities and tool definitions. It includes:
- Server metadata (name, version, description)
- Tool definitions with parameter schemas
- Capability declarations
Environment Setup
- Python virtual environment in
.venv/ - Gradio for web interface
- MCP server capabilities built into Python application
Function Details
Enhanced Features
- Robust Input Handling: All functions accept any input type and convert to string
- Improved Algorithms:
- Sentence counting uses regex for multiple punctuation marks
- Syllable counting handles silent 'e' and edge cases
- Error Handling: Functions gracefully handle empty inputs and special cases
- Type Safety: Full type hints and comprehensive docstrings
Algorithm Highlights
- Syllable Counter: Uses vowel grouping with silent 'e' detection
- Sentence Counter: Regex-based splitting on
.!?patterns - Letter Counter: Only counts alphabetic characters (excludes numbers/symbols)
Development
Code Quality Features
- Complete type hints for all functions
- Comprehensive docstrings with Args: blocks
- Input validation and type conversion
- Error handling for edge cases
- Consistent code formatting
Adding New Functions
- Define the function with proper type hints
- Add comprehensive docstring with Args: and Returns: sections
- Include input validation (
str(input)conversion) - Add the function to the Gradio interface
- Update MCP configuration if needed
Contributing
- Fork the repository
- Create a feature branch (
git checkout -b feature/new-counter) - Make your changes following the code quality standards
- Add tests if applicable
- Submit a pull request
License
This project is licensed under the ISC License - see the LICENSE file for details.
Troubleshooting
Common Issues
"ModuleNotFoundError: No module named 'gradio'"
pip install gradio
"JSON-RPC error: Not Acceptable: Client must accept text/event-stream"
- This occurs when
mcp_server=Trueis enabled but the client doesn't support event streams - For web interface only, ensure
mcp_server=Falseor remove the parameter
Virtual Environment Issues
# Recreate virtual environment
rm -rf .venv
python -m venv .venv
.venv\Scripts\activate # Windows
pip install gradio
Performance
- Lightweight algorithms optimized for text processing
- Efficient character iteration using generator expressions
- Memory-efficient for large text inputs
- Real-time processing suitable for interactive use
Built using Python, Gradio, and MCP