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 normalizer import cleaning
|
||||
from fastapi import FastAPI
|
||||
from pydantic import BaseModel
|
||||
from typing import Dict, Any, List
|
||||
import transformers
|
||||
import json
|
||||
import datetime
|
||||
|
@ -105,26 +108,22 @@ def full_path_text_maker(full_path):
|
|||
full_path_text = full_path_text.strip()
|
||||
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 ={
|
||||
"best-class":{},
|
||||
"other-classes": []}
|
||||
classification_result = {
|
||||
"best-class": {},
|
||||
"other-classes": []
|
||||
}
|
||||
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'] == 'امضاء':
|
||||
return classification_result, True, 'Classification was successful'
|
||||
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": classification_result, "classification_status": True, "desc": 'Classification was successful'}
|
||||
|
||||
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:
|
||||
output_file.write(id + " >> " + str(error) + "\n")
|
||||
print('cleaning content error!')
|
||||
return classification_result, False, str(error)
|
||||
return {"classification_result": classification_result, "classification_status": False, "desc": str(error)}
|
||||
try:
|
||||
# دریافت کلاس های مربوط به یک سکشن
|
||||
section_classes = get_window_classes(f"{pre_content} {content}")
|
||||
|
||||
except Exception as error:
|
||||
error_content = f"{id} -- Classification Error Content:{error}\n"
|
||||
with open('./data/classification/errors.txt', 'a', encoding='utf-8') as output_file:
|
||||
output_file.write(error_content)
|
||||
print(error_content)
|
||||
return classification_result, False, error_content
|
||||
print(error_content)
|
||||
return {"classification_result": classification_result, "classification_status": False, "desc": error_content}
|
||||
|
||||
classification_result ={
|
||||
"best-class" : section_classes[0],
|
||||
"other-classes" : section_classes[1:]
|
||||
}
|
||||
return classification_result, True, 'Classification was successful'
|
||||
classification_result = {
|
||||
"best-class": section_classes[0],
|
||||
"other-classes": section_classes[1:]
|
||||
}
|
||||
return {"classification_result": classification_result, "classification_status": True, "desc": 'Classification was successful'}
|
||||
|
||||
def do_classify(sections):
|
||||
print(f'start classification: {datetime.datetime.now()}')
|
||||
|
|
Loading…
Reference in New Issue
Block a user