Adding Fast API to def single_section_classification in P3
This commit is contained in:
parent
d0d19b2049
commit
cced2920c1
|
@ -4,6 +4,9 @@
|
||||||
"""
|
"""
|
||||||
from transformers import pipeline
|
from transformers import pipeline
|
||||||
from normalizer import cleaning
|
from normalizer import cleaning
|
||||||
|
from fastapi import FastAPI
|
||||||
|
from pydantic import BaseModel
|
||||||
|
from typing import Dict, Any, List
|
||||||
import transformers
|
import transformers
|
||||||
import json
|
import json
|
||||||
import datetime
|
import datetime
|
||||||
|
@ -105,26 +108,22 @@ def full_path_text_maker(full_path):
|
||||||
full_path_text = full_path_text.strip()
|
full_path_text = full_path_text.strip()
|
||||||
return full_path_text
|
return full_path_text
|
||||||
|
|
||||||
def single_section_classification(id, section_source):
|
|
||||||
|
app = FastAPI()
|
||||||
|
|
||||||
|
@app.post("/single_section_classification/")
|
||||||
|
def single_section_classification(id: str, section_source: Dict[str, Any]):
|
||||||
"""
|
"""
|
||||||
این متد، متن ورودی را کلاسبندی می کند
|
این نقطه پایانی، متن یک سکشن را با توجه به محتوا و عنوان قانون، دستهبندی میکند.
|
||||||
|
|
||||||
**Args:
|
|
||||||
id (str): شناسه سکشن
|
|
||||||
section_source (obj): سورس یک سکشن که شامل متن قانون و متادیتاهای مربوط به آن می شود
|
|
||||||
|
|
||||||
**Returns:
|
|
||||||
classification_result(obj): چند کلاس پیشنهادی به ترتیب اولویت
|
|
||||||
classification_status(bool): بیان می کند که عملیات کلاس بندی موفق بوده یا خیر
|
|
||||||
desc(str): توضیحی در مورد موفقیت یا خطای عملیات ارائه می دهد
|
|
||||||
"""
|
"""
|
||||||
classification_result = {
|
classification_result = {
|
||||||
"best-class": {},
|
"best-class": {},
|
||||||
"other-classes": []}
|
"other-classes": []
|
||||||
|
}
|
||||||
content = section_source['content']
|
content = section_source['content']
|
||||||
# اگر نوع سکشن، عنوان یا موخره یا امضاء باشد، نیازی به فرایند کلاسبندی نیست و لیست کلاس های مربوط به این سکشن را خالی قرار می دهیم
|
# اگر نوع سکشن، عنوان یا موخره یا امضاء باشد، نیازی به فرایند کلاسبندی نیست و لیست کلاس های مربوط به این سکشن را خالی قرار می دهیم
|
||||||
if content == '' or section_source['other_info']['full_path'] == 'عنوان' or section_source['other_info']['full_path'] == 'موخره' or section_source['other_info']['full_path'] == 'امضاء':
|
if content == '' or section_source['other_info']['full_path'] == 'عنوان' or section_source['other_info']['full_path'] == 'موخره' or section_source['other_info']['full_path'] == 'امضاء':
|
||||||
return classification_result, True, 'Classification was successful'
|
return {"classification_result": classification_result, "classification_status": True, "desc": 'Classification was successful'}
|
||||||
|
|
||||||
qanon_title = section_source['qanon_title']
|
qanon_title = section_source['qanon_title']
|
||||||
# این متغیر ریشه سکشن اخیر تا رسیدن به قانون را مشخص می کند
|
# این متغیر ریشه سکشن اخیر تا رسیدن به قانون را مشخص می کند
|
||||||
|
@ -144,23 +143,22 @@ def single_section_classification(id, section_source):
|
||||||
with open('./data/cleaning_content_log.txt', 'a', encoding='utf-8') as output_file:
|
with open('./data/cleaning_content_log.txt', 'a', encoding='utf-8') as output_file:
|
||||||
output_file.write(id + " >> " + str(error) + "\n")
|
output_file.write(id + " >> " + str(error) + "\n")
|
||||||
print('cleaning content error!')
|
print('cleaning content error!')
|
||||||
return classification_result, False, str(error)
|
return {"classification_result": classification_result, "classification_status": False, "desc": str(error)}
|
||||||
try:
|
try:
|
||||||
# دریافت کلاس های مربوط به یک سکشن
|
# دریافت کلاس های مربوط به یک سکشن
|
||||||
section_classes = get_window_classes(f"{pre_content} {content}")
|
section_classes = get_window_classes(f"{pre_content} {content}")
|
||||||
|
|
||||||
except Exception as error:
|
except Exception as error:
|
||||||
error_content = f"{id} -- Classification Error Content:{error}\n"
|
error_content = f"{id} -- Classification Error Content:{error}\n"
|
||||||
with open('./data/classification/errors.txt', 'a', encoding='utf-8') as output_file:
|
with open('./data/classification/errors.txt', 'a', encoding='utf-8') as output_file:
|
||||||
output_file.write(error_content)
|
output_file.write(error_content)
|
||||||
print(error_content)
|
print(error_content)
|
||||||
return classification_result, False, error_content
|
return {"classification_result": classification_result, "classification_status": False, "desc": error_content}
|
||||||
|
|
||||||
classification_result = {
|
classification_result = {
|
||||||
"best-class": section_classes[0],
|
"best-class": section_classes[0],
|
||||||
"other-classes": section_classes[1:]
|
"other-classes": section_classes[1:]
|
||||||
}
|
}
|
||||||
return classification_result, True, 'Classification was successful'
|
return {"classification_result": classification_result, "classification_status": True, "desc": 'Classification was successful'}
|
||||||
|
|
||||||
def do_classify(sections):
|
def do_classify(sections):
|
||||||
print(f'start classification: {datetime.datetime.now()}')
|
print(f'start classification: {datetime.datetime.now()}')
|
||||||
|
|
Loading…
Reference in New Issue
Block a user