Skip to content

Commit 83a2df5

Browse files
committed
UI
1 parent a2dca84 commit 83a2df5

File tree

384 files changed

+8711
-1435
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

384 files changed

+8711
-1435
lines changed

Backend/NotesToText.py

Lines changed: 60 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
import os
2-
from fastapi import APIRouter
2+
from fastapi import APIRouter,UploadFile,File
33
from pdf2image import convert_from_path
44
from google.cloud import vision
5+
from typing import List
6+
57

68
# Create an instance of APIRouter
79
router = APIRouter()
@@ -27,10 +29,19 @@ def pdf_to_images(pdf_path, output_folder):
2729
@router.get("/notestotext")
2830
def NotesToText_handler():
2931
substring_to_remove = "Scanned by CamScanner"
30-
for i in range(4):
31-
print(f"converting module-{i+1}....")
32-
pdf_path = f'Local_Storage/notes_pdf/module_{i+1}.pdf'
33-
output_folder = f'images/Notes_images/module_{i+1}'
32+
33+
folder_path = "Local_Storage/notes_pdf"
34+
35+
# Get all files in the folder
36+
mod_files = os.listdir(folder_path)
37+
38+
# Print the file names
39+
for file_name in mod_files:
40+
file_name=file_name.split(".")[0]
41+
42+
print(f"converting {file_name}....")
43+
pdf_path = f'Local_Storage/notes_pdf/{file_name}.pdf'
44+
output_folder = f'images/Notes_images/{file_name}'
3445

3546
# Convert the PDF to images and save them in the output folder
3647
image_paths, noImg = pdf_to_images(pdf_path, output_folder)
@@ -41,9 +52,9 @@ def NotesToText_handler():
4152

4253
# [START vision_python_migration_text_detection]
4354
image_contents = " "
44-
55+
4556
for j in range(noImg):
46-
image_path = f'images/Notes_images/Module_{i+1}/page_{j+1}.jpeg'
57+
image_path = f'images/Notes_images/{file_name}/page_{j+1}.jpeg'
4758
with open(image_path, 'rb') as image_file:
4859
content = image_file.read()
4960
image = vision.Image(content=content)
@@ -53,11 +64,11 @@ def NotesToText_handler():
5364
image_contents += text.replace(substring_to_remove, "")
5465

5566

56-
output_file = f"Local_Storage/notes_txt/module_{i+1}.txt"
67+
output_file = f"Local_Storage/notes_txt/{file_name}.txt"
5768
# Write the text content to the output file
58-
with open(output_file, "w") as file:
69+
with open(output_file, "w",encoding="utf-8") as file:
5970
file.write(image_contents)
60-
print(f"module-{i+1} completed")
71+
print(f"{file_name} completed")
6172

6273
if response.error.message:
6374
raise Exception(
@@ -67,3 +78,42 @@ def NotesToText_handler():
6778

6879

6980

81+
@router.post("/notestotext_modwise")
82+
async def upload_files(files: List[UploadFile] = File(...)):
83+
filenames = []
84+
for file in files:
85+
contents = await file.read()
86+
with open("Local_Storage/notes_pdf/"+file.filename, "wb") as f:
87+
f.write(contents)
88+
filenames.append(file.filename)
89+
return {"filenames": filenames}
90+
91+
@router.post("/notestotext_syllabus")
92+
async def upload_files(files: List[UploadFile] = File(...)):
93+
filenames = []
94+
for file in files:
95+
contents = await file.read()
96+
with open("Local_Storage/syllabus_pdf"+file.filename, "wb") as f:
97+
f.write(contents)
98+
filenames.append(file.filename)
99+
return {"filenames": filenames}
100+
101+
@router.post("/notestotext_pyqs")
102+
async def upload_files(files: List[UploadFile] = File(...)):
103+
filenames = []
104+
for file in files:
105+
contents = await file.read()
106+
with open("Local_Storage/pyqs_pdf"+file.filename, "wb") as f:
107+
f.write(contents)
108+
filenames.append(file.filename)
109+
return {"filenames": filenames}
110+
111+
@router.post("/notestotext_anythingelse")
112+
async def upload_files(files: List[UploadFile] = File(...)):
113+
filenames = []
114+
for file in files:
115+
contents = await file.read()
116+
with open("Local_Storage/anything_else/"+file.filename, "wb") as f:
117+
f.write(contents)
118+
filenames.append(file.filename)
119+
return {"filenames": filenames}
983 Bytes
Binary file not shown.
741 KB
Binary file not shown.
114 KB
Binary file not shown.
299 KB
Binary file not shown.
-1010 KB
Binary file not shown.

0 commit comments

Comments
 (0)