OsGo commited on
Commit
bbebdeb
Β·
verified Β·
1 Parent(s): b3a834b

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +305 -11
app.py CHANGED
@@ -3,18 +3,312 @@
3
  import gradio as gr
4
  from server import read_word, create_word
5
 
6
- with gr.Blocks() as demo:
7
- gr.Markdown("# Gradio MCP Server - Word Reader/Writer")
8
- with gr.Tab("Read Word"):
9
- read_path = gr.Textbox(label="Local path of Word file to read")
10
- read_output = gr.Textbox(label="Word file content", lines=10)
11
- btn_read = gr.Button("Read file")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
12
  btn_read.click(read_word, inputs=[read_path], outputs=[read_output])
13
- with gr.Tab("Create Word"):
14
- create_path = gr.Textbox(label="Local path to save Word file")
15
- create_content = gr.Textbox(label="Content for Word file", lines=10)
16
- create_output = gr.Textbox(label="Creation result")
17
- btn_create = gr.Button("Create file")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
18
  btn_create.click(create_word, inputs=[create_path, create_content], outputs=[create_output])
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
19
 
20
  demo.launch(mcp_server=True)
 
3
  import gradio as gr
4
  from server import read_word, create_word
5
 
6
+ # Custom CSS for a more beautiful interface
7
+ custom_css = """
8
+ .gradio-container {
9
+ font-family: 'Inter', sans-serif;
10
+ }
11
+ .main-header {
12
+ text-align: center;
13
+ padding: 2rem;
14
+ background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
15
+ color: white;
16
+ border-radius: 10px;
17
+ margin-bottom: 2rem;
18
+ }
19
+ .info-box {
20
+ background: #f8f9fa;
21
+ border-left: 4px solid #667eea;
22
+ padding: 1rem;
23
+ border-radius: 5px;
24
+ margin: 1rem 0;
25
+ }
26
+ .step-box {
27
+ background: #ffffff;
28
+ border: 2px solid #e9ecef;
29
+ padding: 1rem;
30
+ border-radius: 8px;
31
+ margin: 0.5rem 0;
32
+ }
33
+ """
34
+
35
+ with gr.Blocks(css=custom_css, theme=gr.themes.Soft()) as demo:
36
+
37
+ # Welcome Tab
38
+ with gr.Tab("🏠 Welcome & Setup"):
39
+ gr.Markdown("""
40
+ <div class="main-header">
41
+ <h1>πŸ“„ Gradio MCP Server - Word Reader/Writer</h1>
42
+ <p style="font-size: 1.2em; margin-top: 1rem;">Connect your LLM to Word documents using the Model Context Protocol</p>
43
+ </div>
44
+ """)
45
+
46
+ gr.Markdown("""
47
+ ## 🎯 About This Project
48
+
49
+ This project demonstrates how to use **Gradio MCP** integration to create an MCP Server that allows AI assistants
50
+ to read and create Word documents. Built for the **MCP 1st Birthday Hackathon**, it showcases the power of combining:
51
+
52
+ - **Gradio**: Easy-to-use Python library for creating web interfaces
53
+ - **MCP (Model Context Protocol)**: Standardized way for LLMs to interact with external tools
54
+ - **python-docx**: Library for working with Word documents
55
+
56
+ ---
57
+
58
+ ## πŸš€ How This Project Uses Gradio MCP
59
+
60
+ This project leverages Gradio's MCP integration to automatically expose Python functions as MCP tools.
61
+ Here's how it works:
62
+ """)
63
+
64
+ with gr.Accordion("πŸ“š Understanding the Architecture", open=False):
65
+ gr.Markdown("""
66
+ ### Server Architecture (`server.py`)
67
+
68
+ The core functionality is in `server.py`, which contains:
69
+ - `read_word(path)`: Reads content from Word files
70
+ - `create_word(path, content)`: Creates new Word documents
71
+
72
+ ### Gradio Interface (`app.py`)
73
+
74
+ The Gradio app imports these functions and:
75
+ 1. **Creates a web UI** for human interaction
76
+ 2. **Automatically exposes them as MCP tools** by setting `mcp_server=True`
77
+
78
+ ```python
79
+ demo.launch(mcp_server=True)
80
+ ```
81
+
82
+ This single parameter converts your Gradio functions into MCP-compatible tools that AI assistants can call!
83
+ """)
84
+
85
+ gr.Markdown("""
86
+ ---
87
+
88
+ ## πŸ”§ How to Connect This Server to Your AI Assistant
89
+
90
+ ### Option 1: Using MCPHost (Recommended)
91
+
92
+ **MCPHost** is a client application that can connect to MCP servers. Here's how to connect:
93
+ """)
94
+
95
+ with gr.Accordion("πŸ“ Step-by-Step MCPHost Configuration", open=True):
96
+ gr.Markdown("""
97
+ <div class="step-box">
98
+
99
+ **Step 1: Get Your MCP Server URL**
100
+
101
+ If hosted on Hugging Face Spaces, your MCP endpoint is:
102
+ ```
103
+ https://YOUR-SPACE-NAME.hf.space/gradio_api/mcp/
104
+ ```
105
+
106
+ For this project:
107
+ ```
108
+ https://mcp-1st-birthday-mcp-docx-tools.hf.space/gradio_api/mcp/
109
+ ```
110
+ </div>
111
+
112
+ <div class="step-box">
113
+
114
+ **Step 2: Configure MCPHost**
115
+
116
+ Add this configuration to your MCPHost settings file:
117
+
118
+ **For JSON format (`config.json`):**
119
+ ```json
120
+ {
121
+ "mcpServers": {
122
+ "word-docx-server": {
123
+ "type": "remote",
124
+ "url": "https://mcp-1st-birthday-mcp-docx-tools.hf.space/gradio_api/mcp/",
125
+ "options": {
126
+ "allowed_directories": ["C:/Users/YourUser/Documents"]
127
+ }
128
+ }
129
+ }
130
+ }
131
+ ```
132
+
133
+ **For YAML format (`config.yaml`):**
134
+ ```yaml
135
+ mcpServers:
136
+ word-docx-server:
137
+ type: "remote"
138
+ url: "https://mcp-1st-birthday-mcp-docx-tools.hf.space/gradio_api/mcp/"
139
+ options:
140
+ allowed_directories:
141
+ - "C:/Users/YourUser/Documents"
142
+ ```
143
+ </div>
144
+
145
+ <div class="step-box">
146
+
147
+ **Step 3: Set Allowed Directories**
148
+
149
+ ⚠️ **Important**: For security, specify which directories the MCP server can access:
150
+ - Add the paths where your Word documents are located
151
+ - Use absolute paths
152
+ - Multiple directories can be added
153
+ </div>
154
+
155
+ <div class="step-box">
156
+
157
+ **Step 4: Restart Your MCP Client**
158
+
159
+ After configuration, restart your MCP client (MCPHost, Claude Desktop, etc.) to load the new tools.
160
+ </div>
161
+ """)
162
+
163
+ gr.Markdown("""
164
+ ### Option 2: Using Claude Desktop
165
+
166
+ Edit your `claude_desktop_config.json` file (usually in `~/Library/Application Support/Claude/` on Mac
167
+ or `%APPDATA%/Claude/` on Windows):
168
+ """)
169
+
170
+ with gr.Accordion("βš™οΈ Claude Desktop Configuration", open=False):
171
+ gr.Markdown("""
172
+ ```json
173
+ {
174
+ "mcpServers": {
175
+ "word-docx-tools": {
176
+ "command": "npx",
177
+ "args": [
178
+ "mcp-remote",
179
+ "https://mcp-1st-birthday-mcp-docx-tools.hf.space/gradio_api/mcp/sse"
180
+ ]
181
+ }
182
+ }
183
+ }
184
+ ```
185
+
186
+ Then restart Claude Desktop.
187
+ """)
188
+
189
+ gr.Markdown("""
190
+ ---
191
+
192
+ ## πŸŽ“ Learn More About Gradio MCP
193
+
194
+ This project is based on the official Gradio MCP documentation. To learn more:
195
+
196
+ πŸ“– **[Gradio MCP Integration Guide](https://huggingface.co/learn/mcp-course/unit1/gradio-mcp)**
197
+
198
+ Key concepts covered:
199
+ - How Gradio automatically converts functions to MCP tools
200
+ - Type hints and docstrings for proper tool definitions
201
+ - Hosting MCP servers on Hugging Face Spaces
202
+ - Troubleshooting connection issues
203
+
204
+ ---
205
+
206
+ ## πŸ§ͺ Try It Out
207
+
208
+ Once connected, you can ask your AI assistant:
209
+
210
+ - *"Read the content of C:/Documents/report.docx"*
211
+ - *"Create a new Word document at C:/Documents/summary.docx with this content: [your text]"*
212
+ - *"Extract text from my meeting notes document"*
213
+
214
+ The AI will automatically use the MCP tools to interact with your Word files!
215
+ """)
216
+
217
+ # Read Word Tab
218
+ with gr.Tab("πŸ“– Read Word Document"):
219
+ gr.Markdown("""
220
+ ### Extract content from Word files
221
+ Enter the full path to a Word document to read its content.
222
+ """)
223
+
224
+ with gr.Row():
225
+ with gr.Column(scale=2):
226
+ read_path = gr.Textbox(
227
+ label="πŸ“ Word File Path",
228
+ placeholder="e.g., C:/Users/YourName/Documents/document.docx",
229
+ info="Enter the absolute path to your Word file"
230
+ )
231
+ btn_read = gr.Button("πŸ” Read Document", variant="primary", size="lg")
232
+
233
+ read_output = gr.Textbox(
234
+ label="πŸ“„ Document Content",
235
+ lines=15,
236
+ placeholder="The content of your Word document will appear here...",
237
+ show_copy_button=True
238
+ )
239
+
240
  btn_read.click(read_word, inputs=[read_path], outputs=[read_output])
241
+
242
+ gr.Markdown("""
243
+ πŸ’‘ **Tip**: You can also copy the output using the copy button in the top-right corner of the output box.
244
+ """)
245
+
246
+ # Create Word Tab
247
+ with gr.Tab("✍️ Create Word Document"):
248
+ gr.Markdown("""
249
+ ### Generate new Word documents
250
+ Enter the path where you want to save the document and the content you want to include.
251
+ """)
252
+
253
+ with gr.Row():
254
+ with gr.Column():
255
+ create_path = gr.Textbox(
256
+ label="πŸ’Ύ Save Path",
257
+ placeholder="e.g., C:/Users/YourName/Documents/new_document.docx",
258
+ info="Enter the absolute path where the file will be created"
259
+ )
260
+ create_content = gr.Textbox(
261
+ label="πŸ“ Document Content",
262
+ lines=12,
263
+ placeholder="Enter the content for your Word document...\nYou can use multiple lines.",
264
+ info="Each line will be a separate paragraph in the document"
265
+ )
266
+ btn_create = gr.Button("πŸ“„ Create Document", variant="primary", size="lg")
267
+
268
+ create_output = gr.Textbox(
269
+ label="βœ… Status",
270
+ lines=3,
271
+ placeholder="Creation status will appear here..."
272
+ )
273
+
274
  btn_create.click(create_word, inputs=[create_path, create_content], outputs=[create_output])
275
+
276
+ gr.Markdown("""
277
+ πŸ’‘ **Tip**: Each line in the content will become a separate paragraph in the Word document.
278
+ """)
279
+
280
+ # About Tab
281
+ with gr.Tab("ℹ️ About"):
282
+ gr.Markdown("""
283
+ ## 🎈 MCP 1st Birthday Hackathon Project
284
+
285
+ This project was created for the **MCP 1st Birthday Hackathon** to demonstrate:
286
+ - Integration between LLMs and productivity tools
287
+ - Simplicity of Gradio MCP for creating MCP servers
288
+ - Practical use case for document processing
289
+
290
+ ### πŸ› οΈ Technologies Used
291
+ - **Python 3.9+**
292
+ - **Gradio with MCP support** (`gradio[mcp]`)
293
+ - **python-docx** for Word file manipulation
294
+ - **Model Context Protocol** for standardized AI tool integration
295
+
296
+ ### πŸ“‘ MCP Server Endpoint
297
+ ```
298
+ https://mcp-1st-birthday-mcp-docx-tools.hf.space/gradio_api/mcp/
299
+ ```
300
+
301
+ ### πŸ”— Links
302
+ - [Gradio MCP Documentation](https://huggingface.co/learn/mcp-course/unit1/gradio-mcp)
303
+ - [MCP Protocol Specification](https://spec.modelcontextprotocol.io/)
304
+ - [This Space on Hugging Face](https://huggingface.co/spaces/mcp-1st-birthday/mcp-docx-tools)
305
+
306
+ ### πŸ“ License
307
+ Apache License 2.0
308
+
309
+ ---
310
+
311
+ Made with ❀️ for the MCP-1st-Birthday Hackathon
312
+ """)
313
 
314
  demo.launch(mcp_server=True)