MinerU / app.py
Vladyslav Nalyvaiko
Fast API update 2
7b36638
raw
history blame
1.64 kB
#!/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)