Spaces:
Sleeping
Sleeping
Upload app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,94 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import json
|
| 3 |
+
|
| 4 |
+
@gr.mcp.resource("config://text-tools")
|
| 5 |
+
def text_tools_documentation() -> str:
|
| 6 |
+
"""Documentation for all text tools."""
|
| 7 |
+
return """# Text Tools Documentation
|
| 8 |
+
|
| 9 |
+
## Available Tools
|
| 10 |
+
- analyze_text: Returns word count, character count, average word length
|
| 11 |
+
- reverse_text: Reverses any string
|
| 12 |
+
- count_vowels: Counts vowels in text
|
| 13 |
+
|
| 14 |
+
## Usage
|
| 15 |
+
Pass any string to get instant results.
|
| 16 |
+
"""
|
| 17 |
+
|
| 18 |
+
def analyze_text(text: str) -> str:
|
| 19 |
+
"""Analyze text and compute statistics.
|
| 20 |
+
|
| 21 |
+
Args:
|
| 22 |
+
text: The input text to analyze
|
| 23 |
+
|
| 24 |
+
Returns:
|
| 25 |
+
JSON with analysis results
|
| 26 |
+
"""
|
| 27 |
+
words = text.split()
|
| 28 |
+
chars = len(text)
|
| 29 |
+
return json.dumps({
|
| 30 |
+
"words": len(words),
|
| 31 |
+
"characters": chars,
|
| 32 |
+
"average_word_length": round(chars / len(words), 2) if words else 0
|
| 33 |
+
})
|
| 34 |
+
|
| 35 |
+
def reverse_text(text: str) -> str:
|
| 36 |
+
"""Reverse a string.
|
| 37 |
+
|
| 38 |
+
Args:
|
| 39 |
+
text: Input text
|
| 40 |
+
|
| 41 |
+
Returns:
|
| 42 |
+
The reversed text
|
| 43 |
+
"""
|
| 44 |
+
return text[::-1]
|
| 45 |
+
|
| 46 |
+
def count_vowels(text: str) -> int:
|
| 47 |
+
"""Count vowels in text.
|
| 48 |
+
|
| 49 |
+
Args:
|
| 50 |
+
text: Input text
|
| 51 |
+
|
| 52 |
+
Returns:
|
| 53 |
+
Number of vowels
|
| 54 |
+
"""
|
| 55 |
+
vowels = "aeiouAEIOU"
|
| 56 |
+
return sum(1 for char in text if char in vowels)
|
| 57 |
+
|
| 58 |
+
def word_frequency(text: str) -> str:
|
| 59 |
+
"""Count frequency of each word in text. MCP only.
|
| 60 |
+
|
| 61 |
+
Args:
|
| 62 |
+
text: Input text to analyze
|
| 63 |
+
|
| 64 |
+
Returns:
|
| 65 |
+
JSON with word frequencies
|
| 66 |
+
"""
|
| 67 |
+
words = text.lower().split()
|
| 68 |
+
freq = {}
|
| 69 |
+
for word in words:
|
| 70 |
+
freq[word] = freq.get(word, 0) + 1
|
| 71 |
+
return json.dumps(freq)
|
| 72 |
+
|
| 73 |
+
with gr.Blocks(title="Text Tools") as demo:
|
| 74 |
+
gr.Markdown("# Text Processing Tools")
|
| 75 |
+
|
| 76 |
+
with gr.Tab("Analyze Text"):
|
| 77 |
+
text_input1 = gr.Textbox(label="Enter text", lines=5)
|
| 78 |
+
analysis_output = gr.Textbox(label="Analysis", lines=5)
|
| 79 |
+
gr.Button("Analyze").click(analyze_text, text_input1, analysis_output)
|
| 80 |
+
|
| 81 |
+
with gr.Tab("Reverse Text"):
|
| 82 |
+
text_input2 = gr.Textbox(label="Enter text", lines=5)
|
| 83 |
+
reverse_output = gr.Textbox(label="Reversed", lines=5)
|
| 84 |
+
gr.Button("Reverse").click(reverse_text, text_input2, reverse_output)
|
| 85 |
+
|
| 86 |
+
with gr.Tab("Count Vowels"):
|
| 87 |
+
text_input3 = gr.Textbox(label="Enter text")
|
| 88 |
+
vowel_output = gr.Number(label="Vowel Count")
|
| 89 |
+
gr.Button("Count").click(count_vowels, text_input3, vowel_output)
|
| 90 |
+
|
| 91 |
+
gr.api(word_frequency)
|
| 92 |
+
|
| 93 |
+
if __name__ == "__main__":
|
| 94 |
+
demo.launch(mcp_server=True)
|