Update app.py
Browse files
app.py
CHANGED
|
@@ -9,8 +9,10 @@ from fastapi.responses import StreamingResponse
|
|
| 9 |
from typing import List
|
| 10 |
|
| 11 |
app = FastAPI()
|
| 12 |
-
|
| 13 |
-
|
|
|
|
|
|
|
| 14 |
|
| 15 |
def extract_text_from_image(image_stream):
|
| 16 |
image = Image.open(image_stream)
|
|
@@ -35,13 +37,13 @@ async def upload_pdfs(files: List[UploadFile] = File(...)):
|
|
| 35 |
for file in files:
|
| 36 |
pdf_content = await file.read()
|
| 37 |
text = extract_text_from_pdf(pdf_content)
|
| 38 |
-
txt_filename = os.path.join(
|
| 39 |
with open(txt_filename, "w", encoding="utf-8") as text_file:
|
| 40 |
text_file.write(text)
|
| 41 |
response_files.append(txt_filename)
|
| 42 |
|
| 43 |
# Create a ZIP file of the text files
|
| 44 |
-
zip_filename = os.path.join(
|
| 45 |
with zipfile.ZipFile(zip_filename, "w") as zip_file:
|
| 46 |
for txt_filename in response_files:
|
| 47 |
zip_file.write(txt_filename, os.path.basename(txt_filename))
|
|
@@ -51,4 +53,4 @@ async def upload_pdfs(files: List[UploadFile] = File(...)):
|
|
| 51 |
open(zip_filename, "rb"),
|
| 52 |
media_type="application/zip",
|
| 53 |
headers={"Content-Disposition": f"attachment; filename=converted_texts.zip"}
|
| 54 |
-
)
|
|
|
|
| 9 |
from typing import List
|
| 10 |
|
| 11 |
app = FastAPI()
|
| 12 |
+
|
| 13 |
+
UPLOADS_DIR = "uploads"
|
| 14 |
+
if not os.path.exists(UPLOADS_DIR):
|
| 15 |
+
os.makedirs(UPLOADS_DIR)
|
| 16 |
|
| 17 |
def extract_text_from_image(image_stream):
|
| 18 |
image = Image.open(image_stream)
|
|
|
|
| 37 |
for file in files:
|
| 38 |
pdf_content = await file.read()
|
| 39 |
text = extract_text_from_pdf(pdf_content)
|
| 40 |
+
txt_filename = os.path.join(UPLOADS_DIR, f"{os.path.splitext(file.filename)[0]}.txt")
|
| 41 |
with open(txt_filename, "w", encoding="utf-8") as text_file:
|
| 42 |
text_file.write(text)
|
| 43 |
response_files.append(txt_filename)
|
| 44 |
|
| 45 |
# Create a ZIP file of the text files
|
| 46 |
+
zip_filename = os.path.join(UPLOADS_DIR, "converted_texts.zip")
|
| 47 |
with zipfile.ZipFile(zip_filename, "w") as zip_file:
|
| 48 |
for txt_filename in response_files:
|
| 49 |
zip_file.write(txt_filename, os.path.basename(txt_filename))
|
|
|
|
| 53 |
open(zip_filename, "rb"),
|
| 54 |
media_type="application/zip",
|
| 55 |
headers={"Content-Disposition": f"attachment; filename=converted_texts.zip"}
|
| 56 |
+
)
|