11import json
22import sqlalchemy as sa
33import uuid
4+ import requests
45
56from pathlib import Path
67
@@ -25,8 +26,7 @@ def send_audio_file(file_name):
2526
2627
2728def validate_segmentation (segment ):
28- """Validate the segmentation before accepting the annotation's upload from users
29- """
29+ """Validate the segmentation before accepting the annotation's upload from users"""
3030 required_key = {"start_time" , "end_time" , "transcription" }
3131
3232 if set (required_key ).issubset (segment .keys ()):
@@ -44,8 +44,7 @@ def generate_segmentation(
4444 data_id ,
4545 segmentation_id = None ,
4646):
47- """Generate a Segmentation from the required segment information
48- """
47+ """Generate a Segmentation from the required segment information"""
4948 if segmentation_id is None :
5049 segmentation = Segmentation (
5150 data_id = data_id ,
@@ -190,3 +189,98 @@ def add_data():
190189 ),
191190 201 ,
192191 )
192+
193+
194+ def download_file (url , save_path = None ):
195+ local_filename = url .split ("/" )[- 1 ] if save_path is None else save_path
196+ with requests .get (url , stream = True ) as r :
197+ r .raise_for_status ()
198+ with open (local_filename , "wb" ) as f :
199+ for chunk in r .iter_content (chunk_size = 8192 ):
200+ f .write (chunk )
201+ return local_filename
202+
203+
204+ @api .route ("/dataWithUrl" , methods = ["POST" ])
205+ def add_data_from_url ():
206+ api_key = request .headers .get ("Authorization" , None )
207+
208+ if not api_key :
209+ raise BadRequest (description = "API Key missing from `Authorization` Header" )
210+
211+ project = Project .query .filter_by (api_key = api_key ).first ()
212+
213+ if not project :
214+ raise NotFound (description = "No project exist with given API Key" )
215+
216+ username = request .form .get ("username" , None )
217+ user = User .query .filter_by (username = username ).first ()
218+
219+ if not user :
220+ raise NotFound (description = "No user found with given username" )
221+
222+ segmentations = request .form .get ("segmentations" , "[]" )
223+ reference_transcription = request .form .get ("reference_transcription" , None )
224+ data_url = request .form .get ("data_url" , None )
225+ is_marked_for_review = bool (request .form .get ("is_marked_for_review" , False ))
226+
227+ if data_url is None :
228+ return 404
229+
230+ original_filename = secure_filename (data_url .split ("/" )[- 1 ])
231+
232+ extension = Path (original_filename ).suffix .lower ()
233+
234+ if len (extension ) > 1 and extension [1 :] not in ALLOWED_EXTENSIONS :
235+ raise BadRequest (description = "File format is not supported" )
236+
237+ filename = f"{ str (uuid .uuid4 ().hex )} { extension } "
238+
239+ file_path = Path (app .config ["UPLOAD_FOLDER" ]).joinpath (filename )
240+ download_file (data_url , file_path .as_posix ())
241+
242+ data = Data (
243+ project_id = project .id ,
244+ filename = filename ,
245+ original_filename = original_filename ,
246+ reference_transcription = reference_transcription ,
247+ is_marked_for_review = is_marked_for_review ,
248+ assigned_user_id = user .id ,
249+ )
250+ db .session .add (data )
251+ db .session .flush ()
252+
253+ segmentations = json .loads (segmentations )
254+
255+ new_segmentations = []
256+
257+ for segment in segmentations :
258+ validated = validate_segmentation (segment )
259+
260+ if not validated :
261+ raise BadRequest (description = f"Segmentations have missing keys." )
262+
263+ new_segment = generate_segmentation (
264+ data_id = data .id ,
265+ project_id = project .id ,
266+ end_time = float (segment ["end_time" ]),
267+ start_time = float (segment ["start_time" ]),
268+ annotations = segment .get ("annotations" , {}),
269+ transcription = segment ["transcription" ],
270+ )
271+
272+ new_segmentations .append (new_segment )
273+
274+ data .set_segmentations (new_segmentations )
275+
276+ db .session .commit ()
277+ db .session .refresh (data )
278+
279+ return (
280+ jsonify (
281+ data_id = data .id ,
282+ message = f"Data uploaded, created and assigned successfully" ,
283+ type = "DATA_CREATED" ,
284+ ),
285+ 201 ,
286+ )
0 commit comments