-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathserver.py
More file actions
592 lines (515 loc) · 22.2 KB
/
server.py
File metadata and controls
592 lines (515 loc) · 22.2 KB
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
import anyio
import tempfile
from pathlib import Path
from typing import Optional, List, Dict, Any, Union
import base64
import io
import os
import logging
import hashlib
import json
import yaml
import gc
import click
import uuid
import mcp.types as types
from mcp.server.lowlevel import Server
from docling.document_converter import DocumentConverter
try:
from docling.datamodel.pipeline_options import PdfPipelineOptions, OcrEngine, EasyOcrOptions
from docling.datamodel.base_models import InputFormat
except ImportError:
PdfPipelineOptions = None
OcrEngine = None
EasyOcrOptions = None
InputFormat = None
from docling_sdg.qa.base import GenerateOptions, SampleOptions
from docling_sdg.qa.generate import Generator
from docling_sdg.qa.sample import PassageSampler
# Set up logging
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
# Create a cache directory
CACHE_DIR = Path.home() / ".cache" / "mcp-docling"
CACHE_DIR.mkdir(parents=True, exist_ok=True)
def get_cache_key(source: str, enable_ocr: bool, ocr_language: Optional[List[str]]) -> str:
"""Generate a cache key for the document conversion."""
key_data = {
"source": source,
"enable_ocr": enable_ocr,
"ocr_language": ocr_language or []
}
key_str = json.dumps(key_data, sort_keys=True)
return hashlib.md5(key_str.encode()).hexdigest()
def cleanup_memory():
"""Force garbage collection to free up memory."""
gc.collect()
logger.debug("Performed memory cleanup")
def configure_accelerator():
"""Configure the accelerator device for Docling."""
try:
# Check if the accelerator_device attribute exists
if hasattr(settings.perf, 'accelerator_device'):
# Try to use MPS (Metal Performance Shaders) on macOS
settings.perf.accelerator_device = AcceleratorDevice.MPS
logger.info(f"Configured accelerator device: {settings.perf.accelerator_device}")
else:
logger.info("Accelerator device configuration not supported in this version of Docling")
# Optimize batch processing
settings.perf.doc_batch_size = 1 # Process one document at a time
logger.info(f"Configured batch size: {settings.perf.doc_batch_size}")
return True
except Exception as e:
logger.warning(f"Failed to configure accelerator: {e}")
return False
async def qna_from_document_impl(
source: str,
no_of_qnas: int,
) -> str:
try:
logger.info(f"Processing Q&A generation from source: {source} ({no_of_qnas})")
# Check for required Watson X credentials before proceeding
watsonx_project_id = os.environ.get("WATSONX_PROJECT_ID")
watsonx_api_key = os.environ.get("WATSONX_APIKEY")
watsonx_url = os.environ.get("WATSONX_URL")
# Verify all required credentials are present
missing_credentials = []
if not watsonx_project_id:
missing_credentials.append("WATSONX_PROJECT_ID")
if not watsonx_api_key:
missing_credentials.append("WATSONX_APIKEY")
if not watsonx_url:
missing_credentials.append("WATSONX_URL")
# If any credentials are missing, return a helpful error message
if missing_credentials:
error_message = (
f"Q&A generation requires IBM Watson X credentials which are missing: {', '.join(missing_credentials)}.\n\n"
"To use this functionality, you need to:\n"
"1. Create an IBM Cloud account (https://cloud.ibm.com/registration)\n"
"2. Set up a Watson X project (https://dataplatform.cloud.ibm.com/wx/home)\n"
"3. Set the following environment variables:\n"
" - WATSONX_PROJECT_ID: Your Watson X project ID\n"
" - WATSONX_APIKEY: Your IBM Cloud API key\n"
" - WATSONX_URL: The Watson X API URL (default: https://us-south.ml.cloud.ibm.com)\n\n"
"Alternative: You can still use document conversion and table extraction tools "
"which don't require these credentials."
)
return error_message
# Continue with Q&A generation if all credentials are present
_uuid = uuid.uuid1()
sample_file = f"{Path(source).name}-{_uuid}.jsonl"
passage_sampler = PassageSampler(SampleOptions(sample_file=Path(sample_file)))
passage_sampler.sample([source])
logger.info(f"Created sample file at {sample_file}")
generated_file = f"{Path(source).name}-qac-{_uuid}.jsonl"
options = GenerateOptions(
project_id=watsonx_project_id,
api_key=watsonx_api_key,
url=watsonx_url,
max_qac=no_of_qnas,
generated_file=generated_file,
)
generator = Generator(generate_options=options)
result = generator.generate_from_sample(Path(sample_file))
logger.info(f"Generated Q&A at {result.output}")
with open(result.output, 'r') as file:
qnas = []
for line in file:
json_obj = json.loads(line.strip())
qnas.append({key: json_obj[key] for key in ['question', 'answer']})
qnas = {'question_and_answers': qnas}
return yaml.dump(qnas, default_flow_style=False)
except Exception as e:
logger.exception(f"Error creating Q&A document: {source}")
return f"Error creating Q&A document: {str(e)}"
async def convert_document_impl(
source: str,
enable_ocr: bool = False,
ocr_language: Optional[List[str]] = None
) -> str:
try:
# Remove any quotes from the source string
source = source.strip('"\'')
# Log the cleaned source
logger.info(f"Processing document from source: {source}")
# Generate cache key
cache_key = get_cache_key(source, enable_ocr, ocr_language)
cache_file = CACHE_DIR / f"{cache_key}.md"
# Check if result is cached
if cache_file.exists():
logger.info(f"Using cached result for {source}")
return cache_file.read_text()
# Log the start of processing
logger.info(f"Starting conversion of document: {source}")
# Create converter with simple configuration
converter = DocumentConverter()
# Convert the document
result = converter.convert(source)
# Export to markdown
markdown_output = result.document.export_to_markdown()
# Cache the result
cache_file.write_text(markdown_output)
logger.info(f"Successfully converted document: {source}")
# Clean up memory to free up resources
cleanup_memory()
return markdown_output
except Exception as e:
logger.exception(f"Error converting document: {source}")
return f"Error converting document: {str(e)}"
async def convert_document_with_images_impl(
source: str,
enable_ocr: bool = False,
ocr_language: Optional[List[str]] = None
) -> Dict[str, Any]:
try:
# Remove any quotes from the source string
source = source.strip('"\'')
# Configure OCR if enabled
format_options = {}
if enable_ocr:
ocr_options = EasyOcrOptions(lang=ocr_language or ["en"])
pipeline_options = PdfPipelineOptions(do_ocr=True, ocr_options=ocr_options)
format_options = {
"pdf": {"pipeline_options": pipeline_options}
}
# Create converter and convert document
converter = DocumentConverter(format_options=format_options)
result = converter.convert(source)
# Check for errors - handle different API versions
has_error = False
error_message = ""
# Try different ways to check for errors based on the API version
if hasattr(result, 'status'):
if hasattr(result.status, 'is_error'):
has_error = result.status.is_error
elif hasattr(result.status, 'error'):
has_error = result.status.error
if hasattr(result, 'errors') and result.errors:
has_error = True
error_message = str(result.errors)
if has_error:
error_msg = f"Conversion failed: {error_message}"
raise ValueError(error_msg)
# Export to markdown
markdown_output = result.document.export_to_markdown()
# Extract images
images = []
for item in result.document.items:
if hasattr(item, 'get_image') and callable(getattr(item, 'get_image')):
try:
img = item.get_image(result.document)
if img:
buffered = io.BytesIO()
img.save(buffered, format="PNG")
img_str = base64.b64encode(buffered.getvalue()).decode()
images.append({
"id": item.id,
"data": img_str,
"format": "png"
})
except Exception:
# Skip images that can't be processed
pass
return {
"markdown": markdown_output,
"images": images
}
except Exception as e:
raise ValueError(f"Unexpected error: {str(e)}")
async def extract_tables_impl(source: str) -> List[str]:
source = source.strip('"\'')
# Create converter and convert document
converter = DocumentConverter()
conversion_result = converter.convert(source=source)
tables_results = []
for table in conversion_result.document.tables:
tables_results.append(table.export_to_markdown())
return tables_results
async def convert_batch_impl(
sources: List[str],
enable_ocr: bool = False,
ocr_language: Optional[List[str]] = None
) -> Dict[str, str]:
try:
format_options = {}
if enable_ocr:
ocr_options = EasyOcrOptions(lang=ocr_language or ["en"])
pipeline_options = PdfPipelineOptions(do_ocr=True, ocr_options=ocr_options)
format_options = {
"pdf": {"pipeline_options": pipeline_options}
}
# Create converter
converter = DocumentConverter(format_options=format_options)
# Process each document
results = {}
for source in sources:
# Remove any quotes from the source string
source = source.strip('"\'')
logger.info(f"Processing document from source: {source}")
try:
result = converter.convert(source)
# Check for errors - handle different API versions
has_error = False
error_message = ""
# Try different ways to check for errors based on the API version
if hasattr(result, 'status'):
if hasattr(result.status, 'is_error'):
has_error = result.status.is_error
elif hasattr(result.status, 'error'):
has_error = result.status.error
if hasattr(result, 'errors') and result.errors:
has_error = True
error_message = str(result.errors)
if has_error:
results[source] = f"Error: {error_message}"
else:
results[source] = result.document.export_to_markdown()
except Exception as e:
results[source] = f"Error: {str(e)}"
return results
except Exception as e:
raise ValueError(f"Unexpected error: {str(e)}")
async def get_system_info_impl() -> Dict[str, Any]:
try:
system_info = {
"batch_settings": {
"doc_batch_size": settings.perf.doc_batch_size,
"doc_batch_concurrency": settings.perf.doc_batch_concurrency
},
"cache": {
"enabled": True,
"location": str(CACHE_DIR)
}
}
# Add accelerator info if available
if hasattr(settings.perf, 'accelerator_device'):
system_info["accelerator"] = {
"configured": str(settings.perf.accelerator_device),
"available": ["CPU", "MPS"] # Hardcode the common options
}
else:
system_info["accelerator"] = {
"configured": "Not configured",
"available": ["CPU"] # Default to CPU only
}
return system_info
except Exception as e:
raise ValueError(f"Error getting system info: {str(e)}")
@click.command()
@click.option("--port", default=8000, help="Port to listen on for SSE")
@click.option(
"--transport",
type=click.Choice(["stdio", "sse"]),
default="stdio",
help="Transport type",
)
def main(port: int, transport: str) -> int:
# Configure accelerator
configure_accelerator()
app = Server("docling-processor")
@app.call_tool()
async def call_tool(
name: str, arguments: dict
) -> list[types.TextContent | types.ImageContent | types.EmbeddedResource]:
try:
if name == "qna_from_document":
result = await qna_from_document_impl(
source=arguments.get("source", ""),
no_of_qnas=arguments.get("no_of_qnas", 5),
)
return [types.TextContent(type="text", text=result)]
elif name == "convert_document":
result = await convert_document_impl(
source=arguments.get("source", ""),
enable_ocr=arguments.get("enable_ocr", False),
ocr_language=arguments.get("ocr_language", None)
)
return [types.TextContent(type="text", text=result)]
elif name == "convert_document_with_images":
result = await convert_document_with_images_impl(
source=arguments.get("source", ""),
enable_ocr=arguments.get("enable_ocr", False),
ocr_language=arguments.get("ocr_language", None)
)
# Return markdown text and images
content_items = [types.TextContent(type="text", text=result["markdown"])]
# Add images as embedded resources
for img in result["images"]:
content_items.append(
types.ImageContent(
type="image",
format=img["format"],
data=img["data"]
)
)
return content_items
elif name == "extract_tables":
tables = await extract_tables_impl(source=arguments.get("source", ""))
return [types.TextContent(type="text", text="\n\n".join(tables))]
elif name == "convert_batch":
result = await convert_batch_impl(
sources=arguments.get("sources", []),
enable_ocr=arguments.get("enable_ocr", False),
ocr_language=arguments.get("ocr_language", None)
)
# Format the result as a string
formatted_result = "\n\n".join([f"## {source}\n\n{content}" for source, content in result.items()])
return [types.TextContent(type="text", text=formatted_result)]
elif name == "get_system_info":
result = await get_system_info_impl()
return [types.TextContent(type="text", text=json.dumps(result, indent=2))]
else:
raise ValueError(f"Unknown tool: {name}")
except Exception as e:
logger.exception(f"Error in tool call: {name}")
return [types.TextContent(type="text", text=f"Error: {str(e)}")]
@app.list_tools()
async def list_tools() -> list[types.Tool]:
return [
types.Tool(
name="qna_from_document",
description="Create a Q&A document from a URL or local path to YAML format",
inputSchema={
"type": "object",
"required": ["source"],
"properties": {
"source": {
"type": "string",
"description": "URL or local file path to the document",
},
"no_of_qnas": {
"type": "int",
"description": "Number of Q&A to generate",
},
},
},
),
types.Tool(
name="convert_document",
description="Convert a document from a URL or local path to markdown format",
inputSchema={
"type": "object",
"required": ["source"],
"properties": {
"source": {
"type": "string",
"description": "URL or local file path to the document",
},
"enable_ocr": {
"type": "boolean",
"description": "Whether to enable OCR for scanned documents",
"default": False
},
"ocr_language": {
"type": "array",
"items": {"type": "string"},
"description": "List of language codes for OCR (e.g. [\"en\", \"fr\"])",
}
},
},
),
types.Tool(
name="convert_document_with_images",
description="Convert a document from a URL or local path to markdown format and return embedded images",
inputSchema={
"type": "object",
"required": ["source"],
"properties": {
"source": {
"type": "string",
"description": "URL or local file path to the document",
},
"enable_ocr": {
"type": "boolean",
"description": "Whether to enable OCR for scanned documents",
"default": False
},
"ocr_language": {
"type": "array",
"items": {"type": "string"},
"description": "List of language codes for OCR (e.g. [\"en\", \"fr\"])",
}
},
},
),
types.Tool(
name="extract_tables",
description="Extract tables from a document and return them as structured data",
inputSchema={
"type": "object",
"required": ["source"],
"properties": {
"source": {
"type": "string",
"description": "URL or local file path to the document",
}
},
},
),
types.Tool(
name="convert_batch",
description="Convert multiple documents in batch mode",
inputSchema={
"type": "object",
"required": ["sources"],
"properties": {
"sources": {
"type": "array",
"items": {"type": "string"},
"description": "List of URLs or file paths to documents",
},
"enable_ocr": {
"type": "boolean",
"description": "Whether to enable OCR for scanned documents",
"default": False
},
"ocr_language": {
"type": "array",
"items": {"type": "string"},
"description": "List of language codes for OCR (e.g. [\"en\", \"fr\"])",
}
},
},
),
types.Tool(
name="get_system_info",
description="Get information about the system configuration and acceleration status",
inputSchema={
"type": "object",
"properties": {},
},
),
]
if transport == "sse":
from mcp.server.sse import SseServerTransport
from starlette.applications import Starlette
from starlette.routing import Mount, Route
import uvicorn
sse = SseServerTransport("/messages/")
async def handle_sse(request):
async with sse.connect_sse(
request.scope, request.receive, request._send
) as streams:
await app.run(
streams[0], streams[1], app.create_initialization_options()
)
starlette_app = Starlette(
debug=True,
routes=[
Route("/sse", endpoint=handle_sse),
Mount("/messages/", app=sse.handle_post_message),
],
)
uvicorn.run(starlette_app, host="0.0.0.0", port=port)
return 0
else:
from mcp.server.stdio import stdio_server
async def run_stdio():
async with stdio_server() as streams:
await app.run(
streams[0], streams[1], app.create_initialization_options()
)
anyio.run(run_stdio)
return 0
if __name__ == "__main__":
main()