Gleb Tsyganov commited on
Commit
0cf6d6b
·
1 Parent(s): c90275c

add server

Browse files
Files changed (4) hide show
  1. .codex/config.toml +2 -0
  2. __pycache__/server.cpython-314.pyc +0 -0
  3. app.py +1 -1
  4. server.py +110 -0
.codex/config.toml ADDED
@@ -0,0 +1,2 @@
 
 
 
1
+ [mcp_servers.text-processor]
2
+ url = "https://crtd-text-processor-mcp.hf.space/gradio_api/mcp/"
__pycache__/server.cpython-314.pyc ADDED
Binary file (5.65 kB). View file
 
app.py CHANGED
@@ -116,4 +116,4 @@ with gr.Blocks(title="Text Processor") as demo:
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)
 
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, )
server.py ADDED
@@ -0,0 +1,110 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from mcp.server.mcpserver import MCPServer
2
+ import json
3
+
4
+ mcp = MCPServer("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()