Compare commits
3 Commits
Author | SHA1 | Date | |
---|---|---|---|
013c35ac96 | |||
cced2920c1 | |||
d0d19b2049 |
|
@ -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,7 +108,11 @@ 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]):
|
||||
"""
|
||||
این متد، متن ورودی را کلاسبندی می کند
|
||||
|
||||
|
@ -120,11 +127,12 @@ def single_section_classification(id, section_source):
|
|||
"""
|
||||
classification_result = {
|
||||
"best-class": {},
|
||||
"other-classes": []}
|
||||
"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'
|
||||
return {"classification_result": classification_result, "classification_status": True, "desc": 'Classification was successful'}
|
||||
|
||||
qanon_title = section_source['qanon_title']
|
||||
# این متغیر ریشه سکشن اخیر تا رسیدن به قانون را مشخص می کند
|
||||
|
@ -144,23 +152,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
|
||||
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'
|
||||
return {"classification_result": classification_result, "classification_status": True, "desc": 'Classification was successful'}
|
||||
|
||||
def do_classify(sections):
|
||||
print(f'start classification: {datetime.datetime.now()}')
|
||||
|
|
|
@ -2,6 +2,7 @@
|
|||
ایجاد بردار جملات - امبدینگ
|
||||
"""
|
||||
from sentence_transformers import SentenceTransformer
|
||||
from fastapi import FastAPI
|
||||
import json
|
||||
import datetime
|
||||
import numpy as np
|
||||
|
@ -30,18 +31,40 @@ def do_word_embedder(sections):
|
|||
|
||||
return sections
|
||||
|
||||
def single_section_embedder(sentence):
|
||||
"""
|
||||
این متد، متن ورودی را تبدیل به بردار متناظر آن می کند
|
||||
|
||||
**Args:
|
||||
|
||||
|
||||
|
||||
# Create a FastAPI app instance
|
||||
# یک نمونه از برنامه FastAPI ایجاد میکنیم
|
||||
app = FastAPI()
|
||||
|
||||
# Your method, wrapped in an API endpoint
|
||||
# متد شما که در یک نقطه پایانی API قرار گرفته
|
||||
@app.post("/single_section_embedder/")
|
||||
def single_section_embedder(sentence: str):
|
||||
"""
|
||||
این نقطه پایانی، متن ورودی را تبدیل به بردار متناظر آن می کند.
|
||||
|
||||
**Args:**
|
||||
sentence (str): متن یک سکشن
|
||||
|
||||
**Returns:
|
||||
embeddings: لیست بردار متناظر با متن ورودی
|
||||
**Returns:**
|
||||
list: لیست بردار متناظر با متن ورودی
|
||||
"""
|
||||
# Use the loaded model to encode the sentence
|
||||
# برای تبدیل متن به بردار از مدلی که در بالا لود شده استفاده میکنیم
|
||||
embeddings = model.encode(sentence)
|
||||
return embeddings
|
||||
|
||||
# The output of model.encode is a NumPy array, which needs to be converted
|
||||
# to a list to be returned as a JSON response.
|
||||
# خروجی model.encode یک آرایه NumPy هست که باید به لیست تبدیل بشه
|
||||
# تا به عنوان یک پاسخ JSON برگردانده بشه.
|
||||
return {"embeddings": embeddings.tolist()}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
def cosine_similarity(vec1, vec2):
|
||||
dot_product = np.dot(vec1, vec2) # ضرب داخلی دو بردار
|
||||
|
|
Loading…
Reference in New Issue
Block a user