Create doc_maker.py
Browse files- doc_maker.py +99 -0
doc_maker.py
ADDED
|
@@ -0,0 +1,99 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from fastapi import APIRouter, HTTPException
|
| 2 |
+
from fastapi.responses import FileResponse
|
| 3 |
+
from pydantic import BaseModel
|
| 4 |
+
from typing import Dict, Optional
|
| 5 |
+
import json
|
| 6 |
+
import os
|
| 7 |
+
from datetime import datetime
|
| 8 |
+
import uuid
|
| 9 |
+
|
| 10 |
+
router = APIRouter()
|
| 11 |
+
|
| 12 |
+
BASE_URL = "https://pvanand-doc-maker.hf.space/api/v1/"
|
| 13 |
+
|
| 14 |
+
class DocumentPart(BaseModel):
|
| 15 |
+
pageNo: int
|
| 16 |
+
content: str
|
| 17 |
+
docId: Optional[str] = None
|
| 18 |
+
|
| 19 |
+
class DocumentResponse(BaseModel):
|
| 20 |
+
docId: str
|
| 21 |
+
message: str
|
| 22 |
+
totalPages: int
|
| 23 |
+
download_url: Optional[str] = None
|
| 24 |
+
|
| 25 |
+
def get_download_url(file_id: str) -> str:
|
| 26 |
+
return f"{BASE_URL}download/{file_id}"
|
| 27 |
+
|
| 28 |
+
def get_document_path(doc_id: str) -> str:
|
| 29 |
+
"""Get the full path for a document file"""
|
| 30 |
+
os.makedirs("documents", exist_ok=True)
|
| 31 |
+
return f"documents/{doc_id}.json"
|
| 32 |
+
|
| 33 |
+
def update_document(doc_id: str, page_no: int, content: str) -> Dict:
|
| 34 |
+
"""Update or create document with new page content"""
|
| 35 |
+
filepath = get_document_path(doc_id)
|
| 36 |
+
|
| 37 |
+
if os.path.exists(filepath):
|
| 38 |
+
with open(filepath, 'r', encoding='utf-8') as f:
|
| 39 |
+
doc = json.load(f)
|
| 40 |
+
doc['updatedAt'] = datetime.utcnow().isoformat()
|
| 41 |
+
else:
|
| 42 |
+
doc = {
|
| 43 |
+
'docId': doc_id,
|
| 44 |
+
'createdAt': datetime.utcnow().isoformat(),
|
| 45 |
+
'updatedAt': datetime.utcnow().isoformat(),
|
| 46 |
+
'content': {}
|
| 47 |
+
}
|
| 48 |
+
|
| 49 |
+
doc['content'][str(page_no)] = content
|
| 50 |
+
|
| 51 |
+
with open(filepath, 'w', encoding='utf-8') as f:
|
| 52 |
+
json.dump(doc, f, ensure_ascii=False, indent=2)
|
| 53 |
+
|
| 54 |
+
return doc
|
| 55 |
+
|
| 56 |
+
@router.post("/document/part", response_model=DocumentResponse)
|
| 57 |
+
async def add_document_part(part: DocumentPart):
|
| 58 |
+
doc_id = part.docId or str(uuid.uuid4())
|
| 59 |
+
|
| 60 |
+
try:
|
| 61 |
+
doc = update_document(doc_id, part.pageNo, part.content)
|
| 62 |
+
return DocumentResponse(
|
| 63 |
+
docId=doc_id,
|
| 64 |
+
message=f"Page {part.pageNo} added successfully",
|
| 65 |
+
totalPages=len(doc['content']),
|
| 66 |
+
download_url=get_download_url(doc_id)
|
| 67 |
+
)
|
| 68 |
+
except Exception as e:
|
| 69 |
+
raise HTTPException(status_code=500, detail=f"Error updating document: {str(e)}")
|
| 70 |
+
|
| 71 |
+
@router.get("/document/{doc_id}", response_model=Dict)
|
| 72 |
+
async def get_document(doc_id: str):
|
| 73 |
+
filepath = get_document_path(doc_id)
|
| 74 |
+
if not os.path.exists(filepath):
|
| 75 |
+
raise HTTPException(status_code=404, detail="Document not found")
|
| 76 |
+
|
| 77 |
+
with open(filepath, 'r', encoding='utf-8') as f:
|
| 78 |
+
return json.load(f)
|
| 79 |
+
|
| 80 |
+
@router.get("/document/download/{doc_id}")
|
| 81 |
+
async def download_document(doc_id: str):
|
| 82 |
+
filepath = get_document_path(doc_id)
|
| 83 |
+
if not os.path.exists(filepath):
|
| 84 |
+
raise HTTPException(status_code=404, detail="Document not found")
|
| 85 |
+
|
| 86 |
+
return FileResponse(
|
| 87 |
+
filepath,
|
| 88 |
+
media_type='application/json',
|
| 89 |
+
filename=f"document_{doc_id}.json"
|
| 90 |
+
)
|
| 91 |
+
|
| 92 |
+
@router.delete("/document/{doc_id}")
|
| 93 |
+
async def delete_document(doc_id: str):
|
| 94 |
+
filepath = get_document_path(doc_id)
|
| 95 |
+
if not os.path.exists(filepath):
|
| 96 |
+
raise HTTPException(status_code=404, detail="Document not found")
|
| 97 |
+
|
| 98 |
+
os.remove(filepath)
|
| 99 |
+
return {"message": "Document deleted successfully"}
|