Ines2R commited on
Commit
7b444a3
·
1 Parent(s): 9152963

Add MCP text processor server

Browse files
Files changed (3) hide show
  1. app.py +119 -0
  2. requirements.txt +2 -0
  3. server.py +110 -0
app.py ADDED
@@ -0,0 +1,119 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import json
3
+
4
+ def analyze_text(text: str) -> str:
5
+ """Analyze text and return statistics.
6
+
7
+ Args:
8
+ text: The input text to analyze
9
+
10
+ Returns:
11
+ JSON string with analysis results
12
+ """
13
+ words = text.split()
14
+ chars = len(text)
15
+ chars_no_spaces = len(text.replace(" ", ""))
16
+ sentences = text.count(".") + text.count("!") + text.count("?")
17
+
18
+ avg_word_length = round(chars_no_spaces / len(words), 2) if words else 0
19
+ avg_sentence_length = round(len(words) / max(sentences, 1), 2)
20
+
21
+ return json.dumps({
22
+ "total_characters": chars,
23
+ "characters_without_spaces": chars_no_spaces,
24
+ "total_words": len(words),
25
+ "total_sentences": max(sentences, 1),
26
+ "average_word_length": avg_word_length,
27
+ "average_sentence_length": avg_sentence_length
28
+ }, indent=2)
29
+
30
+ def extract_keywords(text: str, count: int = 5) -> str:
31
+ """Extract keywords (most common words) from text.
32
+
33
+ Args:
34
+ text: The input text
35
+ count: Number of keywords to return (default 5)
36
+
37
+ Returns:
38
+ JSON string with keywords and frequencies
39
+ """
40
+ stopwords = {
41
+ "the", "a", "an", "and", "or", "but", "in", "on", "at", "to", "for",
42
+ "of", "with", "is", "are", "was", "were", "be", "been", "by", "from"
43
+ }
44
+
45
+ words = text.lower().split()
46
+ filtered = [w.strip(".,!?;:") for w in words if w.lower() not in stopwords]
47
+
48
+ from collections import Counter
49
+ word_freq = Counter(filtered)
50
+ top_words = word_freq.most_common(count)
51
+
52
+ return json.dumps({
53
+ "keywords": [{"word": w, "frequency": f} for w, f in top_words]
54
+ }, indent=2)
55
+
56
+ def check_reading_level(text: str) -> str:
57
+ """Estimate reading difficulty level.
58
+
59
+ Args:
60
+ text: The input text
61
+
62
+ Returns:
63
+ JSON string with reading level estimate
64
+ """
65
+ sentences = max(text.count(".") + text.count("!") + text.count("?"), 1)
66
+ words = len(text.split())
67
+ vowels = "aeiou"
68
+ syllables = sum(1 for c in text.lower() if c in vowels)
69
+
70
+ if words == 0:
71
+ return json.dumps({"error": "No text to analyze"})
72
+
73
+ grade = max(0, (0.39 * (words / sentences)) + (11.8 * (syllables / words)) - 15.59)
74
+
75
+ if grade < 6:
76
+ level = "Elementary School"
77
+ elif grade < 9:
78
+ level = "Middle School"
79
+ elif grade < 13:
80
+ level = "High School"
81
+ else:
82
+ level = "College/Academic"
83
+
84
+ return json.dumps({
85
+ "grade_level": round(grade, 1),
86
+ "reading_level": level
87
+ }, indent=2)
88
+
89
+ # Create web UI
90
+ with gr.Blocks(title="Text Processor") as demo:
91
+ gr.Markdown("# Text Processing Tools")
92
+ gr.Markdown("Analyze text statistics, extract keywords, and check reading difficulty.")
93
+
94
+ with gr.Tab("Analyze Text"):
95
+ text_input1 = gr.Textbox(
96
+ label="Enter text",
97
+ lines=8,
98
+ placeholder="Paste your text here..."
99
+ )
100
+ analysis_output = gr.Textbox(label="Analysis Results", lines=8)
101
+ gr.Button("Analyze", size="lg").click(analyze_text, text_input1, analysis_output)
102
+
103
+ with gr.Tab("Extract Keywords"):
104
+ text_input2 = gr.Textbox(label="Enter text", lines=8)
105
+ count_input = gr.Slider(1, 20, value=5, step=1, label="Number of keywords")
106
+ keywords_output = gr.Textbox(label="Keywords", lines=8)
107
+ gr.Button("Extract", size="lg").click(
108
+ extract_keywords,
109
+ [text_input2, count_input],
110
+ keywords_output
111
+ )
112
+
113
+ with gr.Tab("Reading Level"):
114
+ text_input3 = gr.Textbox(label="Enter text", lines=8)
115
+ level_output = gr.Textbox(label="Reading Level Analysis", lines=5)
116
+ gr.Button("Check Level", size="lg").click(check_reading_level, text_input3, level_output)
117
+
118
+ if __name__ == "__main__":
119
+ demo.launch(mcp_server=True)
requirements.txt ADDED
@@ -0,0 +1,2 @@
 
 
 
1
+ gradio[mcp]
2
+ mcp
server.py ADDED
@@ -0,0 +1,110 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from mcp.server.fastmcp import FastMCP
2
+ import json
3
+
4
+ mcp = FastMCP("text-processor")
5
+
6
+ @mcp.tool()
7
+ def analyze_text(text: str) -> str:
8
+ """Analyze text and return statistics.
9
+
10
+ Args:
11
+ text: The input text to analyze
12
+
13
+ Returns:
14
+ JSON string with analysis results
15
+ """
16
+ words = text.split()
17
+ chars = len(text)
18
+ chars_no_spaces = len(text.replace(" ", ""))
19
+ sentences = text.count(".") + text.count("!") + text.count("?")
20
+
21
+ avg_word_length = round(chars_no_spaces / len(words), 2) if words else 0
22
+ avg_sentence_length = round(len(words) / max(sentences, 1), 2)
23
+
24
+ return json.dumps({
25
+ "total_characters": chars,
26
+ "characters_without_spaces": chars_no_spaces,
27
+ "total_words": len(words),
28
+ "total_sentences": max(sentences, 1),
29
+ "average_word_length": avg_word_length,
30
+ "average_sentence_length": avg_sentence_length,
31
+ "unique_words": len(set(word.lower() for word in words))
32
+ })
33
+
34
+ @mcp.tool()
35
+ def extract_keywords(text: str, count: int = 5) -> str:
36
+ """Extract keywords (most common words) from text.
37
+
38
+ Args:
39
+ text: The input text
40
+ count: Number of keywords to return (default 5)
41
+
42
+ Returns:
43
+ JSON string with keywords and frequencies
44
+ """
45
+ # Remove common words
46
+ stopwords = {
47
+ "the", "a", "an", "and", "or", "but", "in", "on", "at", "to", "for",
48
+ "of", "with", "is", "are", "was", "were", "be", "been", "by", "from"
49
+ }
50
+
51
+ words = text.lower().split()
52
+ filtered = [w.strip(".,!?;:") for w in words if w.lower() not in stopwords]
53
+
54
+ from collections import Counter
55
+ word_freq = Counter(filtered)
56
+ top_words = word_freq.most_common(count)
57
+
58
+ return json.dumps({
59
+ "keywords": [{"word": w, "frequency": f} for w, f in top_words]
60
+ })
61
+
62
+ @mcp.tool()
63
+ def check_reading_level(text: str) -> str:
64
+ """Estimate reading difficulty level.
65
+
66
+ Args:
67
+ text: The input text
68
+
69
+ Returns:
70
+ JSON string with reading level estimate
71
+ """
72
+ sentences = max(text.count(".") + text.count("!") + text.count("?"), 1)
73
+ words = len(text.split())
74
+ syllables = text.count("a") + text.count("e") + text.count("i") + text.count("o") + text.count("u")
75
+
76
+ if words == 0:
77
+ return json.dumps({"error": "No text to analyze"})
78
+
79
+ # Flesch Kincaid Grade
80
+ grade = (0.39 * (words / sentences)) + (11.8 * (syllables / words)) - 15.59
81
+ grade = max(0, round(grade, 1))
82
+
83
+ if grade < 6:
84
+ level = "Elementary School"
85
+ elif grade < 9:
86
+ level = "Middle School"
87
+ elif grade < 13:
88
+ level = "High School"
89
+ else:
90
+ level = "College/Academic"
91
+
92
+ return json.dumps({
93
+ "grade_level": grade,
94
+ "reading_level": level
95
+ })
96
+
97
+ @mcp.tool()
98
+ def reverse_text(text: str) -> str:
99
+ """Reverse a string.
100
+
101
+ Args:
102
+ text: The input text
103
+
104
+ Returns:
105
+ The reversed text
106
+ """
107
+ return text[::-1]
108
+
109
+ if __name__ == "__main__":
110
+ mcp.run()