randusertry commited on
Commit
e0c74ce
·
verified ·
1 Parent(s): c617f3a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +29 -0
app.py CHANGED
@@ -1,6 +1,8 @@
1
  from fastapi import FastAPI, File, UploadFile
2
  import pdfplumber
3
  import io
 
 
4
 
5
  app = FastAPI()
6
 
@@ -33,6 +35,33 @@ async def extract(file: UploadFile = File(...)):
33
  "stream": stream
34
  }
35
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
36
  @app.get("/health")
37
  async def health():
38
  return {
 
1
  from fastapi import FastAPI, File, UploadFile
2
  import pdfplumber
3
  import io
4
+ from markitdown import MarkItDown
5
+ import shutil
6
 
7
  app = FastAPI()
8
 
 
35
  "stream": stream
36
  }
37
 
38
+ md = MarkItDown()
39
+
40
+ @app.post("/convert")
41
+ async def convert_to_markdown(file: UploadFile = File(...)):
42
+ temp_path = f"temp_{file.filename}"
43
+
44
+ try:
45
+ with open(temp_path, "wb") as buffer:
46
+ content = await file.read()
47
+ buffer.write(content)
48
+
49
+ # MarkItDown conversion
50
+ result = md.convert(temp_path)
51
+
52
+ return {
53
+ "filename": file.filename,
54
+ "markdown": result.text_content
55
+ }
56
+
57
+ except Exception as e:
58
+ # Added missing HTTPException import
59
+ raise HTTPException(status_code=500, detail=str(e))
60
+
61
+ finally:
62
+ if os.path.exists(temp_path):
63
+ os.remove(temp_path)
64
+
65
  @app.get("/health")
66
  async def health():
67
  return {