Update apis/V1/prompts/translate_detail.py
Browse files
apis/V1/prompts/translate_detail.py
CHANGED
|
@@ -1,88 +1,88 @@
|
|
| 1 |
-
import os
|
| 2 |
-
from langchain_core.prompts import PromptTemplate
|
| 3 |
-
import google.generativeai as genai
|
| 4 |
-
from pdf2image import convert_from_path
|
| 5 |
-
|
| 6 |
-
t_short = """
|
| 7 |
-
Requirement:
|
| 8 |
-
1. Identify the word: "{word}".
|
| 9 |
-
2. Understand the context, topic, or field of the document based on the provided information.
|
| 10 |
-
3. Provide an explanation of the word in Vietnamese based on the context, topic, or field of the document.
|
| 11 |
-
4. Output only the explanation content without any additional formatting.
|
| 12 |
-
|
| 13 |
-
Examples:
|
| 14 |
-
For the word "loss function":
|
| 15 |
-
Explanation: "Hàm mất mát là một hàm số đo lường sự khác biệt giữa giá trị dự đoán và giá trị thực tế của một mô hình học máy. Hàm mất mát càng nhỏ thì mô hình càng tốt."
|
| 16 |
-
|
| 17 |
-
For the word "neural network":
|
| 18 |
-
Explanation: "Mạng nơ-ron là một hệ thống các đơn vị tính toán kết nối với nhau được mô phỏng theo cách hoạt động
|
| 19 |
-
|
| 20 |
-
For the word "gradient descent":
|
| 21 |
-
Explanation: "Thuật toán gradient descent là một phương pháp tối ưu hóa được sử dụng để tìm giá trị cực tiểu của hàm số. Nó thực hiện điều này bằng cách di chuyển từng bước nhỏ theo hướng ngược lại của gradient của hàm số."
|
| 22 |
-
|
| 23 |
-
For the word "overfitting":
|
| 24 |
-
Explanation: "Overfitting là hiện tượng một mô hình học máy biểu hiện quá mức các dữ liệu huấn luyện, làm giảm khả năng dự đoán chính xác dữ liệu mới. Điều này thường xảy ra khi mô hình quá phức tạp so với dữ liệu."
|
| 25 |
-
|
| 26 |
-
Now, follow the steps and provide the explanation for the word "{word}".
|
| 27 |
-
"""
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
def GetIndexContext(numPage: int, currentPage: int):
|
| 31 |
-
compulsoryContext = (0, 3)
|
| 32 |
-
threadContext = 3
|
| 33 |
-
start = currentPage - threadContext
|
| 34 |
-
end = currentPage + threadContext
|
| 35 |
-
|
| 36 |
-
start = 0 if start < compulsoryContext[1] else start
|
| 37 |
-
end = numPage - 1 if end > numPage - 1 else end
|
| 38 |
-
|
| 39 |
-
if start == 0:
|
| 40 |
-
indexs = [i for i in range(0, end + 1)]
|
| 41 |
-
else:
|
| 42 |
-
indexs = [i for i in range(threadContext)]
|
| 43 |
-
indexs.extend([j for j in range(start, end + 1)])
|
| 44 |
-
|
| 45 |
-
return indexs
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
class AskImage:
|
| 49 |
-
def __init__(self) -> None:
|
| 50 |
-
genai.configure(api_key=os.environ["GOOGLE_API_KEY"])
|
| 51 |
-
self.model = genai.GenerativeModel("gemini-1.5-flash")
|
| 52 |
-
self.prompt = PromptTemplate.from_template(t_short)
|
| 53 |
-
self.all_context = []
|
| 54 |
-
self.context = []
|
| 55 |
-
|
| 56 |
-
def uploaded(self, path: str, user_name: str):
|
| 57 |
-
try:
|
| 58 |
-
self.all_context = convert_from_path(
|
| 59 |
-
pdf_path=path, first_page=0, last_page=200, size=(850, 1000), thread_count=100)
|
| 60 |
-
print("Converted", len(self.all_context),
|
| 61 |
-
"pages for", user_name, "successfully")
|
| 62 |
-
return True
|
| 63 |
-
except:
|
| 64 |
-
print("Error converting pages for", user_name)
|
| 65 |
-
return False
|
| 66 |
-
|
| 67 |
-
def explain_word(self, word: str, current_page: int):
|
| 68 |
-
prompt = self.prompt.format(word=word)
|
| 69 |
-
|
| 70 |
-
self.context = [
|
| 71 |
-
self.all_context[idx]
|
| 72 |
-
for idx in GetIndexContext(len(self.all_context), current_page)
|
| 73 |
-
]
|
| 74 |
-
print("Got context from page", GetIndexContext(
|
| 75 |
-
len(self.all_context), current_page))
|
| 76 |
-
input_data = self.context + [prompt]
|
| 77 |
-
result = self.model.generate_content(input_data)
|
| 78 |
-
return result.text
|
| 79 |
-
|
| 80 |
-
def ask(self, question: str, current_page: int):
|
| 81 |
-
self.context = [
|
| 82 |
-
self.all_context[idx]
|
| 83 |
-
for idx in GetIndexContext(len(self.all_context), current_page)
|
| 84 |
-
]
|
| 85 |
-
print("Got context from page", GetIndexContext(
|
| 86 |
-
len(self.all_context), current_page))
|
| 87 |
-
result = self.model.generate_content([question] + self.all_context)
|
| 88 |
-
return result.text
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
from langchain_core.prompts import PromptTemplate
|
| 3 |
+
import google.generativeai as genai
|
| 4 |
+
from pdf2image import convert_from_path
|
| 5 |
+
|
| 6 |
+
t_short = """
|
| 7 |
+
Requirement:
|
| 8 |
+
1. Identify the word: "{word}".
|
| 9 |
+
2. Understand the context, topic, or field of the document based on the provided information.
|
| 10 |
+
3. Provide an explanation of the word in Vietnamese based on the context, topic, or field of the document.
|
| 11 |
+
4. Output only the explanation content without any additional formatting. Ensure the explanation is clear, concise, and in Vietnamese.
|
| 12 |
+
|
| 13 |
+
Examples:
|
| 14 |
+
For the word "loss function":
|
| 15 |
+
Explanation: "Hàm mất mát là một hàm số đo lường sự khác biệt giữa giá trị dự đoán và giá trị thực tế của một mô hình học máy. Hàm mất mát càng nhỏ thì mô hình càng tốt."
|
| 16 |
+
|
| 17 |
+
For the word "neural network":
|
| 18 |
+
Explanation: "Mạng nơ-ron là một hệ thống các đơn vị tính toán kết nối với nhau được mô phỏng theo cách hoạt động c��a bộ não con người. Mạng nơ-ron được sử dụng trong học máy để phát hiện các mẫu và đưa ra dự đoán."
|
| 19 |
+
|
| 20 |
+
For the word "gradient descent":
|
| 21 |
+
Explanation: "Thuật toán gradient descent là một phương pháp tối ưu hóa được sử dụng để tìm giá trị cực tiểu của hàm số. Nó thực hiện điều này bằng cách di chuyển từng bước nhỏ theo hướng ngược lại của gradient của hàm số."
|
| 22 |
+
|
| 23 |
+
For the word "overfitting":
|
| 24 |
+
Explanation: "Overfitting là hiện tượng một mô hình học máy biểu hiện quá mức các dữ liệu huấn luyện, làm giảm khả năng dự đoán chính xác dữ liệu mới. Điều này thường xảy ra khi mô hình quá phức tạp so với dữ liệu."
|
| 25 |
+
|
| 26 |
+
Now, follow the steps and provide the explanation for the word "{word}".
|
| 27 |
+
"""
|
| 28 |
+
|
| 29 |
+
|
| 30 |
+
def GetIndexContext(numPage: int, currentPage: int):
|
| 31 |
+
compulsoryContext = (0, 3)
|
| 32 |
+
threadContext = 3
|
| 33 |
+
start = currentPage - threadContext
|
| 34 |
+
end = currentPage + threadContext
|
| 35 |
+
|
| 36 |
+
start = 0 if start < compulsoryContext[1] else start
|
| 37 |
+
end = numPage - 1 if end > numPage - 1 else end
|
| 38 |
+
|
| 39 |
+
if start == 0:
|
| 40 |
+
indexs = [i for i in range(0, end + 1)]
|
| 41 |
+
else:
|
| 42 |
+
indexs = [i for i in range(threadContext)]
|
| 43 |
+
indexs.extend([j for j in range(start, end + 1)])
|
| 44 |
+
|
| 45 |
+
return indexs
|
| 46 |
+
|
| 47 |
+
|
| 48 |
+
class AskImage:
|
| 49 |
+
def __init__(self) -> None:
|
| 50 |
+
genai.configure(api_key=os.environ["GOOGLE_API_KEY"])
|
| 51 |
+
self.model = genai.GenerativeModel("gemini-1.5-flash")
|
| 52 |
+
self.prompt = PromptTemplate.from_template(t_short)
|
| 53 |
+
self.all_context = []
|
| 54 |
+
self.context = []
|
| 55 |
+
|
| 56 |
+
def uploaded(self, path: str, user_name: str):
|
| 57 |
+
try:
|
| 58 |
+
self.all_context = convert_from_path(
|
| 59 |
+
pdf_path=path, first_page=0, last_page=200, size=(850, 1000), thread_count=100)
|
| 60 |
+
print("Converted", len(self.all_context),
|
| 61 |
+
"pages for", user_name, "successfully")
|
| 62 |
+
return True
|
| 63 |
+
except:
|
| 64 |
+
print("Error converting pages for", user_name)
|
| 65 |
+
return False
|
| 66 |
+
|
| 67 |
+
def explain_word(self, word: str, current_page: int):
|
| 68 |
+
prompt = self.prompt.format(word=word)
|
| 69 |
+
|
| 70 |
+
self.context = [
|
| 71 |
+
self.all_context[idx]
|
| 72 |
+
for idx in GetIndexContext(len(self.all_context), current_page)
|
| 73 |
+
]
|
| 74 |
+
print("Got context from page", GetIndexContext(
|
| 75 |
+
len(self.all_context), current_page))
|
| 76 |
+
input_data = self.context + [prompt]
|
| 77 |
+
result = self.model.generate_content(input_data)
|
| 78 |
+
return result.text
|
| 79 |
+
|
| 80 |
+
def ask(self, question: str, current_page: int):
|
| 81 |
+
self.context = [
|
| 82 |
+
self.all_context[idx]
|
| 83 |
+
for idx in GetIndexContext(len(self.all_context), current_page)
|
| 84 |
+
]
|
| 85 |
+
print("Got context from page", GetIndexContext(
|
| 86 |
+
len(self.all_context), current_page))
|
| 87 |
+
result = self.model.generate_content([question] + self.all_context)
|
| 88 |
+
return result.text
|