File size: 4,644 Bytes
8f7165e f568e89 8f7165e ac57eb2 f568e89 ac57eb2 f568e89 ac57eb2 f568e89 ac57eb2 f568e89 ac57eb2 f568e89 ac57eb2 f568e89 ac57eb2 f568e89 ac57eb2 f568e89 ac57eb2 f568e89 ac57eb2 f568e89 ac57eb2 f568e89 ac57eb2 f568e89 ac57eb2 f568e89 ac57eb2 f568e89 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 |
import gradio as gr
import os
def letter_counter(word: str, letter: str) -> int:
"""
Count the number of occurrences of a letter in a word or text.
Args:
word (str): The input text to search through
letter (str): The letter to search for
Returns:
int: The number of times the letter appears in the text
"""
if not word or not letter:
return 0
word = word.lower()
letter = letter.lower()
count = word.count(letter)
return count
def word_stats(text: str) -> dict:
"""
Get comprehensive statistics about a text.
Args:
text (str): The input text to analyze
Returns:
dict: Statistics including word count, character count, etc.
"""
if not text:
return {"words": 0, "characters": 0, "letters": 0, "sentences": 0}
words = len(text.split())
characters = len(text)
letters = sum(1 for c in text if c.isalpha())
sentences = text.count('.') + text.count('!') + text.count('?')
return {
"words": words,
"characters": characters,
"letters": letters,
"sentences": sentences
}
# Create a standard Gradio interface with multiple tabs
with gr.Blocks(title="Text Analysis MCP Server") as demo:
gr.Markdown("# Text Analysis Tools")
gr.Markdown("This app provides text analysis functions and can also serve as an MCP server.")
with gr.Tab("Letter Counter"):
with gr.Row():
text_input = gr.Textbox(
label="Enter text",
placeholder="Type your text here...",
lines=3
)
letter_input = gr.Textbox(
label="Enter letter to count",
placeholder="e.g., 'a'",
max_lines=1
)
count_output = gr.Number(label="Letter count")
count_btn = gr.Button("Count Letters", variant="primary")
count_btn.click(
fn=letter_counter,
inputs=[text_input, letter_input],
outputs=count_output
)
# Example
gr.Examples(
examples=[
["Hello World!", "l"],
["The quick brown fox", "o"],
["Python programming", "p"]
],
inputs=[text_input, letter_input]
)
with gr.Tab("Text Statistics"):
stats_text_input = gr.Textbox(
label="Enter text to analyze",
placeholder="Type your text here...",
lines=5
)
stats_output = gr.JSON(label="Text Statistics")
stats_btn = gr.Button("Analyze Text", variant="primary")
stats_btn.click(
fn=word_stats,
inputs=stats_text_input,
outputs=stats_output
)
# Example
gr.Examples(
examples=[
["This is a sample text for analysis. It contains multiple sentences!"],
["Python is a powerful programming language. It's easy to learn and versatile."]
],
inputs=[stats_text_input]
)
with gr.Tab("MCP Server Info"):
# Get the Space URL dynamically
space_host = os.getenv("SPACE_HOST", "your-username-your-space-name.hf.space")
mcp_endpoint = f"https://{space_host}/gradio_api/mcp/sse"
gr.Markdown(f"""
## MCP Server Information
This app is running as an MCP (Model Context Protocol) server on Hugging Face Spaces.
**MCP Endpoint**: `{mcp_endpoint}`
**Available Functions**:
- `letter_counter`: Count occurrences of a letter in text
- `word_stats`: Get comprehensive text statistics
**Usage with MCP Client**:
```json
{{
"model": "your-model",
"provider": "your-provider",
"servers": [
{{
"type": "sse",
"config": {{
"url": "{mcp_endpoint}"
}}
}}
]
}}
```
**Note**: Replace `your-username-your-space-name.hf.space` with your actual Space URL.
""")
# Launch the app
if __name__ == "__main__":
# For Hugging Face Spaces, we need to modify the launch parameters
demo.launch(
mcp_server=True,
server_name="0.0.0.0", # Required for Spaces
server_port=7860, # Default port for Spaces
show_api=True,
share=False # Spaces handles sharing
) |