Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add ability for user to specify their docling server #263

Merged
merged 3 commits into from
Jan 7, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 60 additions & 1 deletion server/app/routes/convert.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@
import os
import aiohttp
from pathlib import Path
from urllib.parse import urljoin
from azure.ai.documentintelligence.models import AnalyzeDocumentRequest, AnalyzeResult, DocumentContentFormat
from azure.ai.documentintelligence import DocumentIntelligenceClient
from azure.core.credentials import AzureKeyCredential
import asyncio
from concurrent.futures import ThreadPoolExecutor
from dotenv import load_dotenv
import base64

from docling.datamodel.base_models import InputFormat
from docling.document_converter import DocumentConverter, PdfFormatOption
Expand Down Expand Up @@ -48,8 +50,63 @@ def process_document_with_azure(file_path: str, endpoint: str, key: str) -> str:
return f"Error processing document: {str(e)}"

@router.post("/api/convert-documents")
async def convert_documents(files: List[UploadFile] = File(...), use_docetl_server: str = "false"):
async def convert_documents(
files: List[UploadFile] = File(...),
use_docetl_server: str = "false",
custom_docling_url: Optional[str] = Header(None)
):
use_docetl_server = use_docetl_server.lower() == "true" # TODO: make this a boolean


# If custom Docling URL is provided, forward the request there
if custom_docling_url:
try:
async with aiohttp.ClientSession() as session:
results = []
for file in files:
# Read file content and encode as base64
content = await file.read()
base64_content = base64.b64encode(content).decode('utf-8')

# Prepare request payload according to Docling server spec
payload = {
"file_source": {
"base64_string": base64_content,
"filename": file.filename
},
"options": {
"output_docling_document": False,
"output_markdown": True,
"output_html": False,
"do_ocr": True,
"do_table_structure": True,
"include_images": False
}
}

async with session.post(
urljoin(custom_docling_url, 'convert'),
json=payload,
timeout=120
) as response:
if response.status == 200:
result = await response.json()
if result["status"] in ("success", '4'):
results.append({
"filename": file.filename,
"markdown": result["document"]["markdown"]
})
else:
return {"error": f"Docling server failed to convert {file.filename}: {result.get('errors', [])}"}
else:
error_msg = await response.text()
return {"error": f"Custom Docling server returned error for {file.filename}: {error_msg}"}

return {"documents": results}

except Exception as e:
print(f"Custom Docling server failed: {str(e)}. Falling back to local processing...")

# Only try Modal endpoint if use_docetl_server is true and there are no txt files
all_txt_files = all(file.filename.lower().endswith('.txt') or file.filename.lower().endswith('.md') for file in files)
if use_docetl_server and not all_txt_files:
Expand All @@ -58,6 +115,8 @@ async def convert_documents(files: List[UploadFile] = File(...), use_docetl_serv
# Prepare files for multipart upload
data = aiohttp.FormData()
for file in files:
# Reset file position since we might have read it in the custom Docling attempt
await file.seek(0)
data.add_field('files',
await file.read(),
filename=file.filename,
Expand Down
45 changes: 24 additions & 21 deletions website/src/app/api/convertDocuments/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,37 +19,43 @@ export async function POST(request: NextRequest) {
// Get Azure credentials from headers if they exist
const azureEndpoint = request.headers.get("azure-endpoint");
const azureKey = request.headers.get("azure-key");

// Determine which endpoint to use
const endpoint =
azureEndpoint && azureKey
? "/api/azure-convert-documents"
: "/api/convert-documents";
const customDoclingUrl = request.headers.get("custom-docling-url");

// Prepare headers for the backend request
const headers: HeadersInit = {};
if (azureEndpoint && azureKey) {
headers["azure-endpoint"] = azureEndpoint;
headers["azure-key"] = azureKey;
}
if (customDoclingUrl) {
headers["custom-docling-url"] = customDoclingUrl;
}

// Create FormData since FastAPI expects multipart/form-data
const backendFormData = new FormData();
for (const file of files) {
backendFormData.append("files", file);
}

// Forward the request to the Python backend
const response = await fetch(
`${FASTAPI_URL}${endpoint}?use_docetl_server=${
conversionMethod === "docetl" ? "true" : "false"
}`,
{
method: "POST",
body: backendFormData,
headers,
}
);
// Determine which endpoint to use and construct the URL
let targetUrl: string;
if (azureEndpoint && azureKey) {
targetUrl = `${FASTAPI_URL}/api/azure-convert-documents`;
} else if (customDoclingUrl) {
targetUrl = `${FASTAPI_URL}/api/convert-documents`;
} else {
targetUrl = `${FASTAPI_URL}/api/convert-documents${
conversionMethod === "docetl" ? "?use_docetl_server=true" : ""
}`;
}


// Forward the request to the appropriate backend
const response = await fetch(targetUrl, {
method: "POST",
body: backendFormData,
headers,
});

if (!response.ok) {
const errorData = await response.json().catch(() => ({}));
Expand All @@ -66,10 +72,7 @@ export async function POST(request: NextRequest) {
console.error("Error converting documents:", error);
return NextResponse.json(
{
error:
error instanceof Error
? error.message
: "Failed to convert documents",
error: error instanceof Error ? error.message : String(error),
},
{ status: 500 }
);
Expand Down
113 changes: 80 additions & 33 deletions website/src/components/FileExplorer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ async function getAllFiles(entry: FileSystemEntry): Promise<FileWithPath[]> {
return files;
}

type ConversionMethod = "local" | "azure" | "docetl";
type ConversionMethod = "local" | "azure" | "docetl" | "custom-docling";

async function validateJsonDataset(file: Blob): Promise<void> {
const text = await file.text();
Expand Down Expand Up @@ -216,6 +216,7 @@ export const FileExplorer: React.FC<FileExplorerProps> = ({
useState<ConversionMethod>("local");
const [azureEndpoint, setAzureEndpoint] = useState("");
const [azureKey, setAzureKey] = useState("");
const [customDoclingUrl, setCustomDoclingUrl] = useState("");

const { uploadingFiles, uploadDataset } = useDatasetUpload({
namespace,
Expand Down Expand Up @@ -358,6 +359,8 @@ export const FileExplorer: React.FC<FileExplorerProps> = ({
if (conversionMethod === "azure") {
headers["azure-endpoint"] = azureEndpoint;
headers["azure-key"] = azureKey;
} else if (conversionMethod === "custom-docling") {
headers["custom-docling-url"] = customDoclingUrl;
}

// Then proceed with conversion
Expand All @@ -368,7 +371,8 @@ export const FileExplorer: React.FC<FileExplorerProps> = ({
});

if (!response.ok) {
throw new Error("Failed to convert documents");
const errorData = await response.json();
throw new Error(errorData.error || "Internal Server Error");
}

const result = await response.json();
Expand Down Expand Up @@ -430,7 +434,7 @@ export const FileExplorer: React.FC<FileExplorerProps> = ({
toast({
variant: "destructive",
title: "Error",
description: "Failed to process files. Please try again.",
description: error instanceof Error ? error.message : String(error),
});
} finally {
setIsConverting(false);
Expand Down Expand Up @@ -687,7 +691,7 @@ export const FileExplorer: React.FC<FileExplorerProps> = ({
onValueChange={(value) =>
setConversionMethod(value as ConversionMethod)
}
className="mt-2 grid grid-cols-3 gap-2"
className="mt-2 grid grid-cols-4 gap-2"
>
<div className="flex flex-col space-y-1 p-2 rounded-md transition-colors hover:bg-gray-50 cursor-pointer border border-gray-100">
<div className="flex items-start space-x-2.5">
Expand Down Expand Up @@ -778,6 +782,33 @@ export const FileExplorer: React.FC<FileExplorerProps> = ({
Enterprise-grade cloud processing
</p>
</div>

<div className="flex flex-col space-y-1 p-2 rounded-md transition-colors hover:bg-gray-50 cursor-pointer border border-gray-100">
<div className="flex items-start space-x-2.5">
<RadioGroupItem
value="custom-docling"
id="custom-docling"
className="mt-0.5"
/>
<Label
htmlFor="custom-docling"
className="text-sm font-medium cursor-pointer"
>
Custom Docling Server{" "}
<a
href="https://github.com/DS4SD/docling-serve"
target="_blank"
rel="noopener noreferrer"
className="text-xs text-blue-600 hover:underline"
>
(GitHub ↗)
</a>
</Label>
</div>
<p className="text-xs text-muted-foreground pl-6">
Connect to your own Docling server instance
</p>
</div>
</RadioGroup>
</div>

Expand Down Expand Up @@ -826,13 +857,30 @@ export const FileExplorer: React.FC<FileExplorerProps> = ({
</div>
)}

{conversionMethod === "custom-docling" && (
<div className="grid gap-2 animate-in fade-in slide-in-from-top-1">
<div className="space-y-1">
<Label htmlFor="docling-url" className="text-sm">
Docling Server URL
</Label>
<Input
id="docling-url"
placeholder="http://hostname:port"
value={customDoclingUrl}
onChange={(e) => setCustomDoclingUrl(e.target.value)}
className="h-8"
/>
</div>
</div>
)}

<div
className={`
border-2 border-dashed rounded-lg transition-colors relative flex-shrink-0
border border-dashed rounded-lg transition-colors relative flex-shrink-0
${
selectedFiles && selectedFiles.length > 0
? "border-border bg-accent/50 p-6"
: "border-border p-8 hover:border-primary"
? "border-border bg-accent/50 p-3"
: "border-border p-3 hover:border-primary"
}
`}
onDragOver={(e) => {
Expand Down Expand Up @@ -883,33 +931,30 @@ export const FileExplorer: React.FC<FileExplorerProps> = ({
)}

<div className="text-center">
<Upload className="w-10 h-10 mx-auto text-gray-400 mb-4" />
<div className="space-y-2">
<p className="text-sm text-gray-600">
Drag and drop your documents here or
</p>
<label>
<input
type="file"
multiple
className="hidden"
accept={SUPPORTED_EXTENSIONS.join(",")}
onChange={(e) => {
if (e.target.files) {
handleFolderUpload(e.target.files);
}
}}
/>
<span className="text-sm text-primary hover:text-blue-600 cursor-pointer">
browse files
</span>
</label>
<p className="text-xs text-gray-500">
<Upload className="w-5 h-5 mx-auto text-gray-400 mb-1" />
<div className="flex flex-col items-center">
<div className="flex items-center text-sm text-gray-500">
<span>Drag and drop your documents here or</span>
<label className="ml-0.5">
<input
type="file"
multiple
className="hidden"
accept={SUPPORTED_EXTENSIONS.join(",")}
onChange={(e) => {
if (e.target.files) {
handleFolderUpload(e.target.files);
}
}}
/>
<span className="text-primary hover:text-blue-600 cursor-pointer">
browse files
</span>
</label>
</div>
<p className="mt-0.5 text-[10px] text-gray-400">
Supported formats: PDF, DOCX, DOC, TXT, HTML, PPTX, MD
</p>
<p className="text-xs text-gray-500">
Processing may take up to 2 minutes
</p>
</div>
</div>
</div>
Expand Down Expand Up @@ -978,7 +1023,9 @@ export const FileExplorer: React.FC<FileExplorerProps> = ({
disabled={
isConverting ||
(conversionMethod === "azure" &&
(!azureEndpoint || !azureKey))
(!azureEndpoint || !azureKey)) ||
(conversionMethod === "custom-docling" &&
!customDoclingUrl)
}
className="min-w-[100px]"
>
Expand Down
Loading