Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue #121: Replace form by inspection #123

Merged
merged 5 commits into from
Aug 12, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 16 additions & 16 deletions app.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,29 +61,29 @@ def ping():
def verify_password(user_id, password):
return user_id

@app.route('/forms', methods=['POST'])
@app.route('/inspections', methods=['POST'])
@auth.login_required
@swag_from('docs/swagger/create_form.yaml')
def create_form():
form_id = uuid.uuid4()
return jsonify({"message": "Form created successfully", "form_id": form_id}), HTTPStatus.CREATED
@swag_from('docs/swagger/create_inspection.yaml')
def create_inspection():
inspection_id = uuid.uuid4()
return jsonify({"message": "Form created successfully", "inspection_id": inspection_id}), HTTPStatus.CREATED

@app.route('/forms/<form_id>', methods=['PUT'])
@app.route('/inspections/<inspection_id>', methods=['PUT'])
@auth.login_required
@swag_from('docs/swagger/update_form.yaml')
def update_form(form_id):
@swag_from('docs/swagger/update_inspection.yaml')
def update_inspection(inspection_id):
return jsonify(error="Not yet implemented!"), HTTPStatus.SERVICE_UNAVAILABLE

@app.route('/forms/<form_id>', methods=['DELETE'])
@app.route('/inspections/<inspection_id>', methods=['DELETE'])
@auth.login_required
@swag_from('docs/swagger/discard_form.yaml')
def discard_form(form_id):
@swag_from('docs/swagger/discard_inspection.yaml')
def discard_inspection(inspection_id):
return jsonify(error="Not yet implemented!"), HTTPStatus.SERVICE_UNAVAILABLE

@app.route('/forms/<form_id>', methods=['GET'])
@app.route('/inspections/<inspection_id>', methods=['GET'])
@auth.login_required
@swag_from('docs/swagger/get_form.yaml')
def get_form(form_id):
@swag_from('docs/swagger/get_inspection.yaml')
def get_inspection(inspection_id):
return jsonify(error="Not yet implemented!"), HTTPStatus.SERVICE_UNAVAILABLE

@app.route('/analyze', methods=['POST'])
Expand All @@ -105,10 +105,10 @@ def analyze_document():
file.save(file_path)
label_storage.add_image(file_path)

form = analyze(label_storage, ocr, gpt)
inspection = analyze(label_storage, ocr, gpt)

return app.response_class(
response=form.model_dump_json(indent=2),
response=inspection.model_dump_json(indent=2),
status=HTTPStatus.OK,
mimetype="application/json"
)
Expand Down
14 changes: 7 additions & 7 deletions docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ The `/analyze` route serves to:
- **Extract and Structure Data**: Utilizes Azure Document Intelligence to
extract text and layout information from the uploaded documents.
- **Generate Forms**: Uses OpenAI's GPT-4 to generate a structured JSON
form containing all the necessary information extracted from the document.
- **Provide Responses**: Returns the generated form to the client,
inspection containing all the necessary information extracted from the document.
- **Provide Responses**: Returns the generated inspection to the client,
facilitating the inspection and validation processes
by providing all relevant data in a structured format.

Expand Down Expand Up @@ -49,31 +49,31 @@ Check if the service is still alive.

### `POST /analyze`

Upload images for analysis and get the results as a JSON form.
Upload images for analysis and get the results as a JSON inspection.

![analyze](../out/analyze_dss/Analyze%20DSS.png)

### `POST /forms`

Create a new form and add it to the database.
Create a new inspection and add it to the database.

![create](../out/create_form_dss/FertiScan%20Sequence%20Diagram.png)

### `PUT /forms`

Send the latest state of a form to the database.
Send the latest state of a inspection to the database.

![submit](../out/submit_form_dss/FertiScan%20Sequence%20Diagram.png)

### `DELETE /forms`

Remove all transient states of a form.
Remove all transient states of a inspection.

![discard](../out/discard_form_dss/FertiScan%20Sequence%20Diagram.png)

### `GET /forms`

Retrieve the latest state of a form from the database.
Retrieve the latest state of a inspection from the database.

![get](../out/get_form_dss/FertiScan%20Sequence%20Diagram.png)

Expand Down
6 changes: 3 additions & 3 deletions docs/swagger/analyze_document.yaml
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
Analyzes a document by performing OCR and generating a form.
The form is a JSON with all the key informations of a fertiliser label.
Analyzes a document by performing OCR and generating a inspection.
The inspection is a JSON with all the key informations of a fertiliser label.
---
consumes:
- multipart/form-data
- multipart/inspection-data
parameters:
- name: images
in: formData
Expand Down
6 changes: 3 additions & 3 deletions docs/swagger/create_form.yaml
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
Create a new instance of a form in the database.
Create a new instance of a inspection in the database.
---
security:
- basicAuth: []
responses:
201:
description: "Form created successfully"
description: "inspection created successfully"
schema:
type: object
properties:
message:
type: string
example: "Form created successfully"
example: "inspection created successfully"
form_id:
type: string
format: uuid
6 changes: 3 additions & 3 deletions docs/swagger/discard_form.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
Discards a form a user was working on.
This is will remove all transient states of the form from the database.
Discards a inspection a user was working on.
This is will remove all transient states of the inspection from the database.
---
security:
- basicAuth: []
Expand All @@ -8,7 +8,7 @@ parameters:
in: path
type: string
required: true
description: "ID of the form to discard"
description: "ID of the inspection to discard"
responses:
503:
description: "Not yet implemented!"
Expand Down
4 changes: 2 additions & 2 deletions docs/swagger/get_form.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Returns an existing form from the database.
Returns an existing inspection from the database.
---
security:
- basicAuth: []
Expand All @@ -7,7 +7,7 @@ parameters:
in: path
type: string
required: true
description: "ID of the form to get"
description: "ID of the inspection to get"
responses:
503:
description: "Not yet implemented!"
Expand Down
4 changes: 2 additions & 2 deletions docs/swagger/update_form.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Submit an updated version of a form to the database.
Submit an updated version of a inspection to the database.
---
security:
- basicAuth: []
Expand All @@ -7,7 +7,7 @@ parameters:
in: path
type: string
required: true
description: "ID of the form to update"
description: "ID of the inspection to update"
responses:
503:
description: "Not yet implemented!"
Expand Down
30 changes: 15 additions & 15 deletions tests/test_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,24 +17,24 @@ def test_health(self):
response = test_client.get('/health', headers=self.headers)
self.assertEqual(response.status_code, 200)

def test_create_form(self):
response = test_client.post('/forms', headers=self.headers)
def test_create_inspection(self):
response = test_client.post('/inspections', headers=self.headers)
self.assertEqual(response.status_code, 201)
self.assertIn('form_id', response.json)
self.assertIn('inspection_id', response.json)

def test_update_form(self):
form_id = "some_form_id"
response = test_client.put(f'/forms/{form_id}', headers=self.headers, json={"form_data": {"key": "value"}})
def test_update_inspection(self):
inspection_id = "some_inspection_id"
response = test_client.put(f'/inspections/{inspection_id}', headers=self.headers, json={"inspection_data": {"key": "value"}})
self.assertEqual(response.status_code, 503) # Service Unavailable

def test_discard_form(self):
form_id = "some_form_id"
response = test_client.delete(f'/forms/{form_id}', headers=self.headers)
def test_discard_inspection(self):
inspection_id = "some_inspection_id"
response = test_client.delete(f'/inspections/{inspection_id}', headers=self.headers)
self.assertEqual(response.status_code, 503) # Service Unavailable

def test_get_form(self):
form_id = "some_form_id"
response = test_client.get(f'/forms/{form_id}', headers=self.headers)
def test_get_inspection(self):
inspection_id = "some_inspection_id"
response = test_client.get(f'/inspections/{inspection_id}', headers=self.headers)
self.assertEqual(response.status_code, 503) # Service Unavailable

def test_analyze_document_no_files(self):
Expand All @@ -49,15 +49,15 @@ def test_analyze_invalid_document(self):

response = test_client.post(
'/analyze',
content_type='multipart/form-data',
content_type='multipart/inspection-data',
data=data
)

# Document Intelligence throws an error
self.assertEqual(response.status_code, 500)
self.assertEqual(response.status_code, 400)
self.assertIn('error', response.json)

@patch('app.gpt.generate_form')
@patch('app.gpt.create_inspection')
@patch('app.ocr.extract_text')
def test_analyze_document_gpt_error(self, mock_ocr, mock_gpt):
mock_ocr.return_value = MagicMock(content="OCR result")
Expand Down
Loading