Skip to content

Commit

Permalink
update api to accept file as part of form body and return json response
Browse files Browse the repository at this point in the history
  • Loading branch information
akshaykarle committed May 30, 2024
1 parent f4bb867 commit 56d1452
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 36 deletions.
39 changes: 17 additions & 22 deletions app.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,36 +31,31 @@ def health() -> str:
@self.app.route("/analyze", methods=["POST"])
def analyze() -> Tuple[str, int]:
"""Execute the analyzer function."""
# Parse the request params
try:
req_json = request.get_json()
if not req_json.get("text"):
raise Exception("No text provided")
file = request.files['file']
if file.filename == '':
return jsonify({'error': 'No selected file'}), 400

if not req_json.get("language"):
raise Exception("No language provided")
filepath = f'uploads/{file.filename}'
file.save(filepath)
self.logger.info(f"Successfully saved file: {filepath}")

recognizer_result_list = self.engine.analyze_text(
text=req_json.get("text"),
language=req_json.get("language")
analyzer_result_list = self.engine.analyze_csv(
csv_full_path=filepath,
language="en"
)

return Response(
json.dumps(
recognizer_result_list,
resp = {}
for result in analyzer_result_list:
resp['key'] = result.key
resp['value'] = result.value
resp['recognizer_results'] = json.dumps(
result.recognizer_results,
default=lambda o: o.to_dict(),
sort_keys=True,
),
content_type="application/json",
)
except TypeError as te:
error_msg = (
f"Failed to parse /analyze request "
f"for AnalyzerEngine.analyze(). {te.args[0]}"
)
self.logger.error(error_msg)
return jsonify(error=error_msg), 400
)

return jsonify(resp), 200
except Exception as e:
self.logger.error(
f"A fatal error occurred during execution of "
Expand Down
15 changes: 1 addition & 14 deletions src/analyzer_engine/csv_analyzer_engine.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,17 +28,4 @@ def analyze_csv(
csv_list = list(csv.reader(csv_file))
csv_dict = {header: list(map(str, values)) for header, *values in zip(*csv_list)}
analyzer_results = self.analyze_dict(csv_dict, language, keys_to_skip)
return list(analyzer_results)

def analyze_text(
self,
text: str,
language: str,
keys_to_skip: Optional[List[str]] = None,
**kwargs,
) -> Iterable[DictAnalyzerResult]:
d = text.split('\\n')
csv_list = csv.DictReader(d)
csv_dict = {header: list(map(str, values)) for header, *values in zip(*csv_list)}
analyzer_results = self.analyze_dict(csv_dict, language, keys_to_skip)
return list(analyzer_results)
return analyzer_results

0 comments on commit 56d1452

Please sign in to comment.