sesharaghav commited on
Commit
ac57c76
·
1 Parent(s): 4c793dd

Add Docling document intelligence

Browse files
Files changed (3) hide show
  1. app.py +12 -5
  2. document_processor.py +16 -22
  3. requirements.txt +3 -2
app.py CHANGED
@@ -12,13 +12,15 @@ def process_document(file):
12
 
13
  if file is None:
14
  return {
15
- "error": "Please upload a PDF."
16
  }
17
 
18
  try:
 
19
  return processor.process(file)
20
 
21
  except Exception as e:
 
22
  return {
23
  "error": str(e)
24
  }
@@ -29,8 +31,13 @@ demo = gr.Interface(
29
  fn=process_document,
30
 
31
  inputs=gr.File(
32
- label="Upload PDF",
33
- file_types=[".pdf"],
 
 
 
 
 
34
  type="filepath"
35
  ),
36
 
@@ -41,8 +48,8 @@ demo = gr.Interface(
41
  title="AI Tutor Document Processor",
42
 
43
  description=(
44
- "Upload a PDF and extract "
45
- "text and page structure."
46
  )
47
  )
48
 
 
12
 
13
  if file is None:
14
  return {
15
+ "error": "Please upload a document."
16
  }
17
 
18
  try:
19
+
20
  return processor.process(file)
21
 
22
  except Exception as e:
23
+
24
  return {
25
  "error": str(e)
26
  }
 
31
  fn=process_document,
32
 
33
  inputs=gr.File(
34
+ label="Upload Document",
35
+ file_types=[
36
+ ".pdf",
37
+ ".docx",
38
+ ".pptx",
39
+ ".html"
40
+ ],
41
  type="filepath"
42
  ),
43
 
 
48
  title="AI Tutor Document Processor",
49
 
50
  description=(
51
+ "Convert documents into structured "
52
+ "content using Docling."
53
  )
54
  )
55
 
document_processor.py CHANGED
@@ -1,40 +1,34 @@
1
  from pathlib import Path
2
- import pymupdf
 
 
3
 
4
 
5
  class DocumentProcessor:
6
 
 
 
 
7
  def process(self, file_path):
8
 
9
  path = Path(file_path)
10
 
11
- if path.suffix.lower() != ".pdf":
12
- raise ValueError(
13
- f"Unsupported file type: {path.suffix}"
14
  )
15
 
16
- return self.process_pdf(path)
17
-
18
- def process_pdf(self, path):
19
-
20
- document = pymupdf.open(path)
21
-
22
- pages = []
23
 
24
- for page_number, page in enumerate(document):
25
-
26
- pages.append({
27
- "page_number": page_number + 1,
28
- "text": page.get_text()
29
- })
30
-
31
- document.close()
32
 
33
  return {
34
  "document": {
35
  "filename": path.name,
36
- "format": "pdf",
37
- "page_count": len(pages)
38
  },
39
- "pages": pages
 
 
 
40
  }
 
1
  from pathlib import Path
2
+ import json
3
+
4
+ from docling.document_converter import DocumentConverter
5
 
6
 
7
  class DocumentProcessor:
8
 
9
+ def __init__(self):
10
+ self.converter = DocumentConverter()
11
+
12
  def process(self, file_path):
13
 
14
  path = Path(file_path)
15
 
16
+ if not path.exists():
17
+ raise FileNotFoundError(
18
+ f"File not found: {path}"
19
  )
20
 
21
+ result = self.converter.convert(str(path))
 
 
 
 
 
 
22
 
23
+ document = result.document
 
 
 
 
 
 
 
24
 
25
  return {
26
  "document": {
27
  "filename": path.name,
28
+ "format": path.suffix.lower().replace(".", ""),
 
29
  },
30
+
31
+ "markdown": document.export_to_markdown(),
32
+
33
+ "json": document.export_to_dict()
34
  }
requirements.txt CHANGED
@@ -1,3 +1,4 @@
1
  gradio
2
- pymupdf
3
- spaces
 
 
1
  gradio
2
+ spaces
3
+ docling
4
+ pymupdf