Adding Fast API to P3
This commit is contained in:
parent
6318c7ce1e
commit
d0d19b2049
|
@ -2,6 +2,7 @@
|
||||||
ایجاد بردار جملات - امبدینگ
|
ایجاد بردار جملات - امبدینگ
|
||||||
"""
|
"""
|
||||||
from sentence_transformers import SentenceTransformer
|
from sentence_transformers import SentenceTransformer
|
||||||
|
from fastapi import FastAPI
|
||||||
import json
|
import json
|
||||||
import datetime
|
import datetime
|
||||||
import numpy as np
|
import numpy as np
|
||||||
|
@ -30,18 +31,40 @@ def do_word_embedder(sections):
|
||||||
|
|
||||||
return 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): متن یک سکشن
|
sentence (str): متن یک سکشن
|
||||||
|
|
||||||
**Returns:
|
**Returns:**
|
||||||
embeddings: لیست بردار متناظر با متن ورودی
|
list: لیست بردار متناظر با متن ورودی
|
||||||
"""
|
"""
|
||||||
|
# Use the loaded model to encode the sentence
|
||||||
|
# برای تبدیل متن به بردار از مدلی که در بالا لود شده استفاده میکنیم
|
||||||
embeddings = model.encode(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):
|
def cosine_similarity(vec1, vec2):
|
||||||
dot_product = np.dot(vec1, vec2) # ضرب داخلی دو بردار
|
dot_product = np.dot(vec1, vec2) # ضرب داخلی دو بردار
|
||||||
|
|
Loading…
Reference in New Issue
Block a user