|
3 | 3 | """ |
4 | 4 |
|
5 | 5 | import os |
6 | | -import json |
7 | | -from flask import jsonify, make_response, request |
8 | | -from flask_restful import Resource |
| 6 | +from sanic import json as json_response |
| 7 | +import socketio |
9 | 8 |
|
10 | 9 |
|
11 | 10 | from assistant import AssistantException, call_asssistant_api |
|
17 | 16 | os.makedirs(UPLOAD_DIRECTORY) |
18 | 17 |
|
19 | 18 |
|
20 | | -class RunAssistant(Resource): |
| 19 | +def run_assistant(sid, file, sio: socketio.AsyncServer): |
21 | 20 | """ |
22 | | - Represents a resource for handling file uploads. |
23 | | - Methods: |
24 | | - - post: Handles the POST request for file uploads. |
| 21 | + Run the assistant with the uploaded file |
25 | 22 | """ |
26 | | - |
27 | | - def __init__(self, **kwargs): |
28 | | - self.socketio = kwargs.get("socketio") |
29 | | - |
30 | | - def post(self): |
31 | | - """ |
32 | | - Handles the POST request for file uploads. |
33 | | - Returns: |
34 | | - - JSON response containing the status of the upload: |
35 | | - - If successful, returns {"message": "File successfully uploaded", "path": file_path}. |
36 | | - - If there is an error, returns {"error": "No file part"} or {"error": "No selected file"}. |
37 | | - """ |
38 | | - sid = request.form.get("sid") |
39 | | - |
40 | | - if "file" not in request.files: |
41 | | - return jsonify({"error": "No file part"}) |
42 | | - file = request.files["file"] |
43 | | - if file.filename == "": |
44 | | - return jsonify({"error": "No selected file"}) |
45 | | - if file: |
46 | | - file_path = os.path.join(UPLOAD_DIRECTORY, file.filename) |
47 | | - file.save(file_path) |
48 | | - try: |
49 | | - result = call_asssistant_api(file_path, sid, self.socketio) |
50 | | - |
51 | | - # print("result : ", json.dumps(result)) |
52 | | - self.socketio.emit( |
53 | | - "status", |
54 | | - {"status": "Fetching all features...", "progress": 0}, |
55 | | - to=sid, |
56 | | - ) |
57 | | - |
58 | | - response_data = { |
59 | | - "message": "File successfully uploaded", |
60 | | - "file_name": file.filename, |
61 | | - "experiments": result["experiments"], |
62 | | - } |
63 | | - |
64 | | - # Delete the uploaded |
65 | | - if os.path.isfile(file_path): |
66 | | - os.remove(file_path) |
67 | | - print("File removed from local storage successfully") |
68 | | - else: |
69 | | - # If it fails, inform the user. |
70 | | - print(f"Error: {file_path} file not found") |
71 | | - |
72 | | - response = make_response(jsonify(response_data)) |
73 | | - response.status_code = 200 |
74 | | - return response |
75 | | - except AssistantException as e: |
76 | | - response_data = {"error": str(e)} |
77 | | - response = make_response(jsonify(response_data)) |
78 | | - response.status_code = 500 |
79 | | - return response |
80 | | - else: |
81 | | - response_data = {"error": "File upload failed"} |
82 | | - response = make_response(jsonify(response_data)) |
83 | | - response.status_code = 400 |
84 | | - return response |
| 23 | + if file: |
| 24 | + file_path = os.path.join(UPLOAD_DIRECTORY, file.filename) |
| 25 | + file.save(file_path) |
| 26 | + try: |
| 27 | + result = call_asssistant_api(file_path, sid, sio) |
| 28 | + |
| 29 | + # print("result : ", json.dumps(result)) |
| 30 | + sio.emit( |
| 31 | + "status", |
| 32 | + {"status": "Fetching all features...", "progress": 0}, |
| 33 | + to=sid, |
| 34 | + ) |
| 35 | + |
| 36 | + response_data = { |
| 37 | + "message": "File successfully uploaded", |
| 38 | + "file_name": file.filename, |
| 39 | + "experiments": result["experiments"], |
| 40 | + } |
| 41 | + |
| 42 | + # Delete the uploaded |
| 43 | + if os.path.isfile(file_path): |
| 44 | + os.remove(file_path) |
| 45 | + print("File removed from local storage successfully") |
| 46 | + else: |
| 47 | + # If it fails, inform the user. |
| 48 | + print(f"Error: {file_path} file not found") |
| 49 | + |
| 50 | + return json_response(body=response_data, status=200) |
| 51 | + except AssistantException as e: |
| 52 | + response_data = {"error": str(e)} |
| 53 | + return json_response(body=response_data, status=500) |
| 54 | + else: |
| 55 | + response_data = {"error": "File upload failed"} |
| 56 | + return json_response(body=response_data, status=400) |
0 commit comments