1
1
import os
2
- from fastapi import APIRouter
2
+ from fastapi import APIRouter , UploadFile , File
3
3
from pdf2image import convert_from_path
4
4
from google .cloud import vision
5
+ from typing import List
6
+
5
7
6
8
# Create an instance of APIRouter
7
9
router = APIRouter ()
@@ -27,10 +29,19 @@ def pdf_to_images(pdf_path, output_folder):
27
29
@router .get ("/notestotext" )
28
30
def NotesToText_handler ():
29
31
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 } '
34
45
35
46
# Convert the PDF to images and save them in the output folder
36
47
image_paths , noImg = pdf_to_images (pdf_path , output_folder )
@@ -41,9 +52,9 @@ def NotesToText_handler():
41
52
42
53
# [START vision_python_migration_text_detection]
43
54
image_contents = " "
44
-
55
+
45
56
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'
47
58
with open (image_path , 'rb' ) as image_file :
48
59
content = image_file .read ()
49
60
image = vision .Image (content = content )
@@ -53,11 +64,11 @@ def NotesToText_handler():
53
64
image_contents += text .replace (substring_to_remove , "" )
54
65
55
66
56
- output_file = f"Local_Storage/notes_txt/module_ { i + 1 } .txt"
67
+ output_file = f"Local_Storage/notes_txt/{ file_name } .txt"
57
68
# 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 :
59
70
file .write (image_contents )
60
- print (f"module- { i + 1 } completed" )
71
+ print (f"{ file_name } completed" )
61
72
62
73
if response .error .message :
63
74
raise Exception (
@@ -67,3 +78,42 @@ def NotesToText_handler():
67
78
68
79
69
80
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 }
0 commit comments