@@ -66,41 +66,47 @@ async def processing_with_progress():
6666 return
6767
6868
69- yield "data: 10 \n \n "
69+ yield "data: 0 \n \n "
7070
7171 now = time .time ()
7272 result = None
7373
7474 if is_image :
7575 # Start image processing and stream progress
76- result = queryImage ([str (extracted_file_path )], DATASET_DIR )
77- # generator = imageProcessing(DATASET_DIR, [str(extracted_file_path)])
78- # while True:
79- # try:
80- # progress = next(generator) # Get the next progress value
81- # yield f"data: {progress}\n\n"
82- # except StopIteration as stop_result:
83- # # Capture the final result from the generator
84- # result = stop_result.value
85- # break
76+ # result = queryImage([str(extracted_file_path)], DATASET_DIR)
77+ generator = queryImage ([str (extracted_file_path )], DATASET_DIR )
78+ while True :
79+ try :
80+ progress = next (generator ) # Get the next progress value
81+
82+ yield f"data: { progress } \n \n "
83+ except StopIteration as stop_result :
84+ # Capture the final result from the generator
85+ result = stop_result .value
86+ break
8687
8788 else :
8889 # Load the preprocessed database if it exists, otherwise preprocess and save it
8990 database_path = DATASET_DIR / "preprocessed_database.pkl"
9091 if database_path .exists ():
9192 database = joblib .load (database_path )
93+ generator = process_query (str (extracted_file_path ), database )
9294
93- print (len (database ))
94- result = process_query (str (extracted_file_path ), database )
95- print (result [:limit ])
96- # print(result)
95+ while True :
96+ try :
97+ progress = next (generator ) # Get the next progress value
9798
99+ yield f"data: { progress } \n \n "
100+ except StopIteration as stop_result :
101+ # Capture the final result from the generator
102+ result = stop_result .value
103+ break
98104 time_taken = time .time () - now
99105 query_file_path = QUERY_RESULT_DIR / "result.txt"
100106
101107 with open (query_file_path , "w" , encoding = "utf-8" ) as query_file :
102108 for res in result [:limit ]:
103- query_file .write (f"{ res [" filename" ]} { res [" score" ]} \n " )
109+ query_file .write (f"{ res [' filename' ]} { res [' score' ]} \n " )
104110 query_file .write (str (time_taken ) + "\n " )
105111
106112 yield "data: 100\n \n "
@@ -197,44 +203,48 @@ async def create_upload_dataset(file_upload: UploadFile, is_image: str = Form(..
197203
198204 try :
199205 zip_data = await file_upload .read ()
206+ progress = 10
207+ async def processing_with_progress ():
208+ nonlocal progress
209+ yield "data: 10\n \n "
210+ # extract zip file
211+ with zipfile .ZipFile (io .BytesIO (zip_data )) as zip_ref :
212+ increment = 70 / len (zip_ref .infolist ())
213+ for file_info in zip_ref .infolist ():
214+ # Skip directories
215+ if file_info .is_dir ():
216+ continue
200217
201- # extract zip file
202- with zipfile .ZipFile (io .BytesIO (zip_data )) as zip_ref :
203- for file_info in zip_ref .infolist ():
204- # Skip directories
205- if file_info .is_dir ():
206- continue
207-
208- file_name = file_info .filename
209- # check jika file adalah file gambar atau file audio
210- if (is_image_file (file_name ) and is_image ) or (is_midi_file (file_name ) and not is_image ):
211- extracted_file_path = DATASET_DIR / Path (file_name ).name
212- try :
213- with open (extracted_file_path , "wb" ) as extracted_file :
214- extracted_file .write (zip_ref .read (file_name ))
215- except :
216- print (f"Failed to write file: { file_name } " )
218+ file_name = file_info .filename
219+ # check jika file adalah file gambar atau file audio
220+ if (is_image_file (file_name ) and is_image ) or (is_midi_file (file_name ) and not is_image ):
221+ extracted_file_path = DATASET_DIR / Path (file_name ).name
222+ try :
223+ with open (extracted_file_path , "wb" ) as extracted_file :
224+ extracted_file .write (zip_ref .read (file_name ))
225+ except :
226+ print (f"Failed to write file: { file_name } " )
227+ continue
228+
229+ # file bukan file gambar atau file audio
230+ else :
217231 continue
218-
219- # file bukan file gambar atau file audio
220- else :
221- continue
232+ progress += increment
233+ yield f"data: { progress } \n \n "
222234
223- if (is_image ):
224- image_paths , projected_data , eigenvectors , dataMean = preProcessingDataSet (DATASET_DIR )
225- # Save the processed data using joblib
226- path = DATASET_DIR / "processed_data.pkl"
227- joblib .dump ((image_paths , projected_data , eigenvectors , dataMean ), path )
228- else :
229- database = preprocess_database (DATASET_DIR )
230- path = DATASET_DIR / "preprocessed_database.pkl"
231- joblib .dump (database , path )
232-
233- data_urls = [
234- f"http://localhost:8000/uploads/dataset/{ file_name } "
235- for file_name in os .listdir (DATASET_DIR )
236- ]
237- return {"uploaded_images" : data_urls }
235+ if (is_image ):
236+ image_paths , projected_data , eigenvectors , dataMean = preProcessingDataSet (DATASET_DIR )
237+ # Save the processed data using joblib
238+ path = DATASET_DIR / "processed_data.pkl"
239+ joblib .dump ((image_paths , projected_data , eigenvectors , dataMean ), path )
240+ else :
241+ database = preprocess_database (DATASET_DIR )
242+ path = DATASET_DIR / "preprocessed_database.pkl"
243+ joblib .dump (database , path )
244+
245+ yield f"data: 100\n \n "
246+
247+ return StreamingResponse (processing_with_progress (), media_type = "text/event-stream" )
238248
239249 except zipfile .BadZipFile :
240250 raise HTTPException (status_code = 400 , detail = "Invalid zip file" )
0 commit comments