gth commited on
Commit
038cc08
·
0 Parent(s):
Files changed (3) hide show
  1. README.md +60 -0
  2. app.py +162 -0
  3. requirements.txt +4 -0
README.md ADDED
@@ -0,0 +1,60 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Document Converter Pro
2
+
3
+ Une application web moderne et efficace pour convertir vos documents dans différents formats.
4
+
5
+ ## Fonctionnalités
6
+
7
+ - Interface utilisateur intuitive et ergonomique
8
+ - Support de multiples formats de documents
9
+ - Conversion rapide et efficace
10
+ - Gestion des erreurs robuste
11
+
12
+ ## Installation
13
+
14
+ 1. Clonez ce dépôt :
15
+ ```bash
16
+ git clone <repository-url>
17
+ cd convert-anything
18
+ ```
19
+
20
+ 2. Installez les dépendances :
21
+ ```bash
22
+ pip install -r requirements.txt
23
+ ```
24
+
25
+ ## Utilisation
26
+
27
+ 1. Lancez l'application :
28
+ ```bash
29
+ python app.py
30
+ ```
31
+
32
+ 2. Ouvrez votre navigateur et accédez à l'URL locale affichée dans le terminal
33
+
34
+ 3. Utilisez l'interface pour :
35
+ - Télécharger votre document
36
+ - Sélectionner le format de sortie souhaité
37
+ - Cliquer sur "Convert Document"
38
+ - Télécharger le document converti
39
+
40
+ ## Formats supportés
41
+
42
+ ### Documents
43
+ - PDF
44
+ - DOCX
45
+ - DOC
46
+ - ODT
47
+ - RTF
48
+ - TXT
49
+
50
+ ### Images
51
+ - PNG
52
+ - JPEG
53
+ - TIFF
54
+ - BMP
55
+
56
+ ## Technologies utilisées
57
+
58
+ - Gradio : Interface utilisateur web
59
+ - Docling : Moteur de conversion de documents
60
+ - Python-Magic : Détection des types de fichiers
app.py ADDED
@@ -0,0 +1,162 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import tempfile
3
+ import os
4
+ import json
5
+ import yaml
6
+ from pathlib import Path
7
+ from docling.datamodel.base_models import InputFormat
8
+ from docling.datamodel.pipeline_options import (
9
+ PdfPipelineOptions,
10
+ TesseractCliOcrOptions
11
+ )
12
+ from docling.document_converter import (
13
+ DocumentConverter,
14
+ PdfFormatOption,
15
+ WordFormatOption
16
+ )
17
+ from docling.pipeline.standard_pdf_pipeline import StandardPdfPipeline
18
+ from docling.pipeline.simple_pipeline import SimplePipeline
19
+ from docling.backend.pypdfium2_backend import PyPdfiumDocumentBackend
20
+
21
+ # Initialize the document converter with advanced options
22
+ doc_converter = DocumentConverter(
23
+ allowed_formats=[
24
+ InputFormat.PDF,
25
+ InputFormat.IMAGE,
26
+ InputFormat.DOCX,
27
+ InputFormat.HTML,
28
+ InputFormat.PPTX,
29
+ InputFormat.ASCIIDOC,
30
+ InputFormat.MD,
31
+ InputFormat.XLSX,
32
+ ],
33
+ format_options={
34
+ InputFormat.PDF: PdfFormatOption(
35
+ pipeline_cls=StandardPdfPipeline,
36
+ backend=PyPdfiumDocumentBackend,
37
+ pipeline_options=PdfPipelineOptions(
38
+ do_ocr=True,
39
+ ocr_options=TesseractCliOcrOptions(lang=["auto"])
40
+ )
41
+ ),
42
+ InputFormat.DOCX: WordFormatOption(
43
+ pipeline_cls=SimplePipeline
44
+ ),
45
+ }
46
+ )
47
+
48
+ def convert_document(input_file, output_format):
49
+ try:
50
+ # Get the input file path
51
+ temp_dir = tempfile.mkdtemp()
52
+ input_path = Path(input_file.name)
53
+
54
+ # Generate output path
55
+ output_filename = f"converted_document.{output_format.lower()}"
56
+ output_path = Path(temp_dir) / output_filename
57
+
58
+ # Convert the document
59
+ result = doc_converter.convert(str(input_path))
60
+
61
+ # Export to desired format
62
+ if output_format.lower() == "html":
63
+ with open(output_path, "w", encoding="utf-8") as f:
64
+ f.write(result.document.export_to_html())
65
+ elif output_format.lower() == "text":
66
+ with open(output_path, "w", encoding="utf-8") as f:
67
+ f.write(result.document.export_to_text())
68
+ elif output_format.lower() in ["md", "markdown"]:
69
+ with open(output_path, "w", encoding="utf-8") as f:
70
+ f.write(result.document.export_to_markdown())
71
+ elif output_format.lower() == "json":
72
+ with open(output_path, "w", encoding="utf-8") as f:
73
+ json.dump(result.document.export_to_dict(), f, indent=2)
74
+ elif output_format.lower() == "yaml":
75
+ with open(output_path, "w", encoding="utf-8") as f:
76
+ yaml.safe_dump(result.document.export_to_dict(), f)
77
+ elif output_format.lower() == "doctags":
78
+ with open(output_path, "w", encoding="utf-8") as f:
79
+ f.write(result.document.export_to_document_tokens())
80
+ else:
81
+ raise ValueError(f"Format de sortie non supporté: {output_format}")
82
+
83
+ return str(output_path)
84
+ except Exception as e:
85
+ return f"Erreur lors de la conversion: {str(e)}"
86
+
87
+ # Define available formats
88
+ SUPPORTED_OUTPUT_FORMATS = [
89
+ "HTML", "Markdown", "JSON", "Text", "YAML", "Doctags"
90
+ ]
91
+
92
+ # Create the Gradio interface
93
+ with gr.Blocks(theme=gr.themes.Soft(primary_hue="blue")) as demo:
94
+ gr.Markdown(
95
+ """
96
+ # 📄 Document Converter Pro
97
+
98
+ Convertissez facilement vos documents dans différents formats avec support OCR automatique.
99
+ """
100
+ )
101
+
102
+ with gr.Row():
103
+ with gr.Column(scale=1):
104
+ input_file = gr.File(
105
+ label="Télécharger un document",
106
+ file_types=[
107
+ "pdf", "docx", "doc", "pptx", "xlsx",
108
+ "md", "html", "htm", "txt", "rtf",
109
+ "png", "jpg", "jpeg", "tiff", "bmp",
110
+ "adoc", "asciidoc"
111
+ ],
112
+ )
113
+ output_format = gr.Dropdown(
114
+ choices=SUPPORTED_OUTPUT_FORMATS,
115
+ value="HTML",
116
+ label="Convertir en",
117
+ info="Sélectionnez le format de sortie"
118
+ )
119
+ convert_btn = gr.Button("Convertir le document", variant="primary")
120
+
121
+ with gr.Column(scale=1):
122
+ output = gr.File(label="Document converti")
123
+
124
+ convert_btn.click(
125
+ fn=convert_document,
126
+ inputs=[input_file, output_format],
127
+ outputs=output
128
+ )
129
+
130
+ gr.Markdown(
131
+ """
132
+ ### 📝 Instructions
133
+ 1. Téléchargez votre document
134
+ 2. Choisissez le format de sortie souhaité
135
+ 3. Cliquez sur "Convertir le document"
136
+
137
+ ### ℹ️ Formats supportés
138
+
139
+ #### Formats d'entrée
140
+ - Documents Office : PDF, DOCX, XLSX, PPTX
141
+ - Documents Web : HTML, XHTML
142
+ - Documents texte : Markdown, AsciiDoc, TXT, RTF
143
+ - Images : PNG, JPEG, TIFF, BMP
144
+
145
+ #### Formats de sortie
146
+ - HTML (avec support des images intégrées)
147
+ - Markdown
148
+ - Text (texte brut sans formatage)
149
+ - JSON (sérialisation sans perte)
150
+ - YAML
151
+ - Doctags
152
+
153
+ ### 🔍 Fonctionnalités
154
+ - Détection automatique de la langue pour l'OCR
155
+ - Support complet des tableaux
156
+ - Extraction des images
157
+ - Conversion multi-format
158
+ """
159
+ )
160
+
161
+ if __name__ == "__main__":
162
+ demo.launch(share=False)
requirements.txt ADDED
@@ -0,0 +1,4 @@
 
 
 
 
 
1
+ gradio>=4.19.2
2
+ docling>=2.0.0
3
+ pyyaml>=6.0.1
4
+ tesseract-ocr>=5.3.3