|
|
|
import os
|
|
import shutil
|
|
import logging
|
|
import uvicorn
|
|
from fastapi import FastAPI, File, UploadFile
|
|
from fastapi.middleware.cors import CORSMiddleware
|
|
|
|
|
|
|
|
os.system("python download_models_hf.py")
|
|
|
|
from mineru_single import to_markdown
|
|
|
|
|
|
app = FastAPI()
|
|
logging.basicConfig(level=logging.INFO)
|
|
|
|
|
|
app.add_middleware(
|
|
CORSMiddleware,
|
|
allow_origins=["*"],
|
|
allow_credentials=True,
|
|
allow_methods=["*"],
|
|
allow_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)
|
|
|
|
|
|
markdown_text = to_markdown(file_path)
|
|
|
|
return {
|
|
"message": "Processing completed",
|
|
"code": 200,
|
|
"content": markdown_text
|
|
}
|
|
|
|
|
|
if __name__ == "__main__":
|
|
uvicorn.run(app, host="0.0.0.0", port=7860) |