File size: 1,644 Bytes
11551ca
 
 
 
 
7b36638
 
11551ca
 
 
 
 
ca05b65
11551ca
 
 
 
 
7b36638
 
 
 
 
 
 
 
 
11551ca
 
 
 
 
7b36638
 
 
 
 
11551ca
ca05b65
11551ca
ca05b65
 
 
11551ca
ca05b65
 
11551ca
 
ca05b65
 
 
11551ca
 
 
 
 
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
#!/usr/bin/env python3
import os
import shutil
import logging
import uvicorn
from fastapi import FastAPI, File, UploadFile
from fastapi.middleware.cors import CORSMiddleware

# This ensures our models are downloaded and config is set before anything
# Alternatively you can do this in a "startup" event handler
os.system("python download_models_hf.py")

from mineru_single import to_markdown
# Or if you want single-file approach, from miner_single import to_markdown

app = FastAPI()
logging.basicConfig(level=logging.INFO)

# Add CORS middleware to allow requests from any origin
app.add_middleware(
    CORSMiddleware,
    allow_origins=["*"],  # Allows all origins
    allow_credentials=True,
    allow_methods=["*"],  # Allows all methods
    allow_headers=["*"],  # Allows all headers
)

INBOX_DIR = "./inbox"
OUTPUT_DIR = "./output"
os.makedirs(INBOX_DIR, exist_ok=True)
os.makedirs(OUTPUT_DIR, exist_ok=True)

@app.get("/")
async def root():
    """Health check endpoint"""
    return {"status": "ok", "message": "API is running"}

@app.post("/process")
async def process_pdf(file: UploadFile = File(...)):

    file_path = os.path.join(INBOX_DIR, file.filename)
    with open(file_path, "wb") as out_file:
        shutil.copyfileobj(file.file, out_file)

    # Process the file and wait for completion
    markdown_text = to_markdown(file_path)

    return {
        "message": "Processing completed",
        "code": 200,
        "content": markdown_text
    }

# If you want to run locally or for debug:
if __name__ == "__main__":
    uvicorn.run(app, host="0.0.0.0", port=7860)