Spaces:
Sleeping
Sleeping
Update main.py
Browse files
main.py
CHANGED
|
@@ -1,5 +1,7 @@
|
|
| 1 |
from fastapi import FastAPI, UploadFile, HTTPException
|
| 2 |
-
|
|
|
|
|
|
|
| 3 |
import pdfplumber
|
| 4 |
import io
|
| 5 |
|
|
@@ -7,7 +9,22 @@ app = FastAPI()
|
|
| 7 |
|
| 8 |
@app.get("/")
|
| 9 |
async def health_check():
|
| 10 |
-
return {"status": "ok"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
|
| 12 |
@app.post("/extract-text")
|
| 13 |
async def extract_text(file: UploadFile):
|
|
@@ -15,8 +32,11 @@ async def extract_text(file: UploadFile):
|
|
| 15 |
filename = file.filename.lower()
|
| 16 |
|
| 17 |
if filename.endswith('.docx'):
|
| 18 |
-
|
| 19 |
-
|
|
|
|
|
|
|
|
|
|
| 20 |
|
| 21 |
elif filename.endswith('.pdf'):
|
| 22 |
text_parts = []
|
|
@@ -25,12 +45,17 @@ async def extract_text(file: UploadFile):
|
|
| 25 |
page_text = page.extract_text()
|
| 26 |
if page_text:
|
| 27 |
text_parts.append(page_text)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 28 |
text = "\n".join(text_parts)
|
| 29 |
|
| 30 |
else:
|
| 31 |
raise HTTPException(400, "Unsupported file type. Only .docx and .pdf allowed.")
|
| 32 |
|
| 33 |
if not text.strip():
|
| 34 |
-
raise HTTPException(422, "Could not extract any text from file
|
| 35 |
|
| 36 |
return {"filename": file.filename, "extractedText": text}
|
|
|
|
| 1 |
from fastapi import FastAPI, UploadFile, HTTPException
|
| 2 |
+
import subprocess
|
| 3 |
+
import tempfile
|
| 4 |
+
import os
|
| 5 |
import pdfplumber
|
| 6 |
import io
|
| 7 |
|
|
|
|
| 9 |
|
| 10 |
@app.get("/")
|
| 11 |
async def health_check():
|
| 12 |
+
return {"status": "ok"}
|
| 13 |
+
|
| 14 |
+
def convert_with_libreoffice(input_path: str, output_dir: str) -> str:
|
| 15 |
+
subprocess.run(
|
| 16 |
+
[
|
| 17 |
+
"libreoffice", "--headless", "--convert-to", "txt:Text",
|
| 18 |
+
"--outdir", output_dir, input_path
|
| 19 |
+
],
|
| 20 |
+
check=True, timeout=60
|
| 21 |
+
)
|
| 22 |
+
txt_path = os.path.join(
|
| 23 |
+
output_dir,
|
| 24 |
+
os.path.splitext(os.path.basename(input_path))[0] + ".txt"
|
| 25 |
+
)
|
| 26 |
+
with open(txt_path, "r", encoding="utf-8") as f:
|
| 27 |
+
return f.read()
|
| 28 |
|
| 29 |
@app.post("/extract-text")
|
| 30 |
async def extract_text(file: UploadFile):
|
|
|
|
| 32 |
filename = file.filename.lower()
|
| 33 |
|
| 34 |
if filename.endswith('.docx'):
|
| 35 |
+
with tempfile.TemporaryDirectory() as tmp:
|
| 36 |
+
input_path = os.path.join(tmp, file.filename)
|
| 37 |
+
with open(input_path, "wb") as f:
|
| 38 |
+
f.write(content)
|
| 39 |
+
text = convert_with_libreoffice(input_path, tmp)
|
| 40 |
|
| 41 |
elif filename.endswith('.pdf'):
|
| 42 |
text_parts = []
|
|
|
|
| 45 |
page_text = page.extract_text()
|
| 46 |
if page_text:
|
| 47 |
text_parts.append(page_text)
|
| 48 |
+
for table in page.extract_tables():
|
| 49 |
+
for row in table:
|
| 50 |
+
row_text = " | ".join(str(c) for c in row if c)
|
| 51 |
+
if row_text:
|
| 52 |
+
text_parts.append(row_text)
|
| 53 |
text = "\n".join(text_parts)
|
| 54 |
|
| 55 |
else:
|
| 56 |
raise HTTPException(400, "Unsupported file type. Only .docx and .pdf allowed.")
|
| 57 |
|
| 58 |
if not text.strip():
|
| 59 |
+
raise HTTPException(422, "Could not extract any text from file.")
|
| 60 |
|
| 61 |
return {"filename": file.filename, "extractedText": text}
|