[feat][abaoxomtieu] threading code, and llm refactor
Browse files- apis/V1/configs/__pycache__/database_config.cpython-311.pyc +0 -0
- apis/V1/configs/__pycache__/firebase_config.cpython-311.pyc +0 -0
- apis/V1/configs/__pycache__/llm_config.cpython-311.pyc +0 -0
- apis/V1/configs/__pycache__/thread_config.cpython-311.pyc +0 -0
- apis/V1/configs/llm_config.py +1 -1
- apis/V1/configs/thread_config.py +20 -0
- apis/V1/controllers/__pycache__/auth_controller.cpython-311.pyc +0 -0
- apis/V1/controllers/__pycache__/word_controller.cpython-311.pyc +0 -0
- apis/V1/interfaces/__pycache__/auth_interface.cpython-311.pyc +0 -0
- apis/V1/interfaces/__pycache__/llm_interface.cpython-311.pyc +0 -0
- apis/V1/middlewares/__pycache__/auth_middleware.cpython-311.pyc +0 -0
- apis/V1/models/__pycache__/projects.cpython-311.pyc +0 -0
- apis/V1/models/__pycache__/users.cpython-311.pyc +0 -0
- apis/V1/models/__pycache__/words.cpython-311.pyc +0 -0
- apis/V1/prompts/__pycache__/document_type_clf.cpython-311.pyc +0 -0
- apis/V1/prompts/__pycache__/grammarly.cpython-311.pyc +0 -0
- apis/V1/prompts/__pycache__/prompt.cpython-311.pyc +0 -0
- apis/V1/prompts/__pycache__/summarize_doc.cpython-311.pyc +0 -0
- apis/V1/prompts/__pycache__/translate_abstract_BIZ.cpython-311.pyc +0 -0
- apis/V1/prompts/__pycache__/translate_abstract_RnD.cpython-311.pyc +0 -0
- apis/V1/prompts/__pycache__/translate_detail.cpython-311.pyc +0 -0
- apis/V1/prompts/__pycache__/translate_test.cpython-311.pyc +0 -0
- apis/V1/prompts/prompt.py +107 -0
- apis/V1/prompts/translate.py +1 -1
- apis/V1/prompts/translate_abstract_BIZ.py +8 -8
- apis/V1/prompts/translate_abstract_RnD.py +8 -8
- apis/V1/providers/__pycache__/__init__.cpython-311.pyc +0 -0
- apis/V1/providers/__pycache__/firebase_provider.cpython-311.pyc +0 -0
- apis/V1/providers/__pycache__/jwt_provider.cpython-311.pyc +0 -0
- apis/V1/providers/__pycache__/llm_provider.cpython-311.pyc +0 -0
- apis/V1/routes/__pycache__/auth.cpython-311.pyc +0 -0
- apis/V1/routes/__pycache__/chat.cpython-311.pyc +0 -0
- apis/V1/routes/__pycache__/project.cpython-311.pyc +0 -0
- apis/V1/routes/__pycache__/upload.cpython-311.pyc +0 -0
- apis/V1/routes/__pycache__/word.cpython-311.pyc +0 -0
- apis/V1/routes/auth.py +4 -5
- apis/V1/routes/chat.py +80 -0
- apis/V1/routes/project.py +48 -17
- apis/V1/routes/upload.py +52 -15
- apis/V1/routes/word.py +11 -9
- apis/V1/schemas/__pycache__/user_schema.cpython-311.pyc +0 -0
- apis/V1/utils/__pycache__/response_fmt.cpython-311.pyc +0 -0
- apis/V1/utils/__pycache__/utils.cpython-311.pyc +0 -0
- apis/__init__.py +2 -0
- apis/__pycache__/__init__.cpython-310.pyc +0 -0
- apis/__pycache__/__init__.cpython-311.pyc +0 -0
- apis/__pycache__/create_app.cpython-311.pyc +0 -0
- apis/create_app.py +4 -3
apis/V1/configs/__pycache__/database_config.cpython-311.pyc
ADDED
|
Binary file (1.03 kB). View file
|
|
|
apis/V1/configs/__pycache__/firebase_config.cpython-311.pyc
ADDED
|
Binary file (1.98 kB). View file
|
|
|
apis/V1/configs/__pycache__/llm_config.cpython-311.pyc
ADDED
|
Binary file (619 Bytes). View file
|
|
|
apis/V1/configs/__pycache__/thread_config.cpython-311.pyc
ADDED
|
Binary file (1.24 kB). View file
|
|
|
apis/V1/configs/llm_config.py
CHANGED
|
@@ -16,4 +16,4 @@ llm = GoogleGenerativeAI(
|
|
| 16 |
verbose=True,
|
| 17 |
google_api_key=GOOGLE_API_KEY,
|
| 18 |
)
|
| 19 |
-
set_llm_cache(InMemoryCache())
|
|
|
|
| 16 |
verbose=True,
|
| 17 |
google_api_key=GOOGLE_API_KEY,
|
| 18 |
)
|
| 19 |
+
# set_llm_cache(InMemoryCache())
|
apis/V1/configs/thread_config.py
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import asyncio
|
| 2 |
+
import functools
|
| 3 |
+
import threading
|
| 4 |
+
from concurrent.futures import ThreadPoolExecutor
|
| 5 |
+
|
| 6 |
+
# Create a ThreadPoolExecutor
|
| 7 |
+
executor = ThreadPoolExecutor()
|
| 8 |
+
|
| 9 |
+
def run_in_thread(func, *args, **kwargs):
|
| 10 |
+
loop = asyncio.get_event_loop()
|
| 11 |
+
func_name = func.__name__
|
| 12 |
+
|
| 13 |
+
# Function to wrap the original function to print the thread ID
|
| 14 |
+
def wrapper(*args, **kwargs):
|
| 15 |
+
thread_id = threading.get_ident()
|
| 16 |
+
print(f"[Running function '{func_name}' in thread ID: {thread_id}]")
|
| 17 |
+
return func(*args, **kwargs)
|
| 18 |
+
|
| 19 |
+
# Run the wrapped function in the executor
|
| 20 |
+
return loop.run_in_executor(executor, functools.partial(wrapper, *args, **kwargs))
|
apis/V1/controllers/__pycache__/auth_controller.cpython-311.pyc
ADDED
|
Binary file (1.77 kB). View file
|
|
|
apis/V1/controllers/__pycache__/word_controller.cpython-311.pyc
ADDED
|
Binary file (4.47 kB). View file
|
|
|
apis/V1/interfaces/__pycache__/auth_interface.cpython-311.pyc
ADDED
|
Binary file (1.73 kB). View file
|
|
|
apis/V1/interfaces/__pycache__/llm_interface.cpython-311.pyc
ADDED
|
Binary file (2.11 kB). View file
|
|
|
apis/V1/middlewares/__pycache__/auth_middleware.cpython-311.pyc
ADDED
|
Binary file (1.93 kB). View file
|
|
|
apis/V1/models/__pycache__/projects.cpython-311.pyc
ADDED
|
Binary file (5.42 kB). View file
|
|
|
apis/V1/models/__pycache__/users.cpython-311.pyc
ADDED
|
Binary file (4.8 kB). View file
|
|
|
apis/V1/models/__pycache__/words.cpython-311.pyc
ADDED
|
Binary file (6 kB). View file
|
|
|
apis/V1/prompts/__pycache__/document_type_clf.cpython-311.pyc
ADDED
|
Binary file (3.8 kB). View file
|
|
|
apis/V1/prompts/__pycache__/grammarly.cpython-311.pyc
ADDED
|
Binary file (3.43 kB). View file
|
|
|
apis/V1/prompts/__pycache__/prompt.cpython-311.pyc
ADDED
|
Binary file (4.54 kB). View file
|
|
|
apis/V1/prompts/__pycache__/summarize_doc.cpython-311.pyc
ADDED
|
Binary file (1.01 kB). View file
|
|
|
apis/V1/prompts/__pycache__/translate_abstract_BIZ.cpython-311.pyc
ADDED
|
Binary file (3.59 kB). View file
|
|
|
apis/V1/prompts/__pycache__/translate_abstract_RnD.cpython-311.pyc
ADDED
|
Binary file (3.58 kB). View file
|
|
|
apis/V1/prompts/__pycache__/translate_detail.cpython-311.pyc
ADDED
|
Binary file (6.75 kB). View file
|
|
|
apis/V1/prompts/__pycache__/translate_test.cpython-311.pyc
ADDED
|
Binary file (4.42 kB). View file
|
|
|
apis/V1/prompts/prompt.py
ADDED
|
@@ -0,0 +1,107 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
def classify_document_prompt(document):
|
| 2 |
+
prompt = f"""
|
| 3 |
+
Document: {document}
|
| 4 |
+
|
| 5 |
+
You must return in JSON format, field is mandatory:
|
| 6 |
+
{{
|
| 7 |
+
type: must be "Business" or "RnD" based on document content
|
| 8 |
+
}}
|
| 9 |
+
"""
|
| 10 |
+
return prompt
|
| 11 |
+
|
| 12 |
+
|
| 13 |
+
def translate_BIZ_prompt(
|
| 14 |
+
abstract,
|
| 15 |
+
sentence,
|
| 16 |
+
word,
|
| 17 |
+
language_1="English",
|
| 18 |
+
language_2="Vietnamese",
|
| 19 |
+
):
|
| 20 |
+
prompt = f"""
|
| 21 |
+
# Introduction: You are an expert in translating {language_1} words into {language_2} for Finance and Economics topics. Translate the given word or phrase based on the context of the abstract and sentence.
|
| 22 |
+
# System instructions:
|
| 23 |
+
0. No yapping
|
| 24 |
+
1. Read the abstract, surrounding sentences which contain word/phase to understand the context of paper/article.
|
| 25 |
+
2. Translate the given word or phrase into {language_2} using the context.
|
| 26 |
+
3. If the word or phrase is not in the dictionary, return "not in dictionary" with {language_2} language.
|
| 27 |
+
4. If the word or phrase is an acronym, expand it and provide the translation.
|
| 28 |
+
5. Output must be in Vietnamese.
|
| 29 |
+
# Examples:
|
| 30 |
+
Abstract: "Inflation is the rate at which the general level of prices for goods and services rises, eroding purchasing power."
|
| 31 |
+
Surrounding sentences: "Inflation is the rate at which the general level of prices for goods and services rises."
|
| 32 |
+
Word: "Inflation"
|
| 33 |
+
Answer: lạm phát
|
| 34 |
+
|
| 35 |
+
Abstract: "GDP, or Gross Domestic Product, measures the total value of all goods and services produced within a country in a given period."
|
| 36 |
+
Surrounding sentences: "GDP, or Gross Domestic Product, measures the total value of all goods and services produced within a country."
|
| 37 |
+
Word: "GDP"
|
| 38 |
+
Answer: Tổng sản phẩm quốc nội
|
| 39 |
+
|
| 40 |
+
# Here is my question:
|
| 41 |
+
Abstract: {abstract}
|
| 42 |
+
Surrounding sentences: {sentence}
|
| 43 |
+
Word: {word}
|
| 44 |
+
Answer:
|
| 45 |
+
|
| 46 |
+
# Output must be returned in Vietnamese only:
|
| 47 |
+
|
| 48 |
+
"""
|
| 49 |
+
return prompt
|
| 50 |
+
|
| 51 |
+
|
| 52 |
+
def translate_RnD_prompt(
|
| 53 |
+
abstract,
|
| 54 |
+
sentence,
|
| 55 |
+
word,
|
| 56 |
+
language_1="English",
|
| 57 |
+
language_2="Vietnamese",
|
| 58 |
+
):
|
| 59 |
+
prompt = f"""
|
| 60 |
+
# Introduction: You are an expert in translating {language_1} words into {language_2} for Finance and Economics topics. Translate the given word or phrase based on the context of the abstract and sentence.
|
| 61 |
+
# System instructions:
|
| 62 |
+
0. No yapping
|
| 63 |
+
1. Read the abstract, surrounding sentences which contain word/phase to understand the context of paper/article.
|
| 64 |
+
2. Translate the given word or phrase into {language_2} using the context.
|
| 65 |
+
3. If the word or phrase is not in the dictionary, return "not in dictionary" with {language_2} language.
|
| 66 |
+
4. If the word or phrase is an acronym, expand it and provide the translation.
|
| 67 |
+
5. Output must be in Vietnamese.
|
| 68 |
+
# Examples:
|
| 69 |
+
Abstract: "In recent years, there has been significant progress in the development of Transformer-based models for natural language processing tasks. These models, such as BERT, have set new benchmarks in various language understanding tasks."
|
| 70 |
+
Surrounding sentence: "outperforms a Transformer-based BERT baseline on the GLUE language benchmark"
|
| 71 |
+
Word: "outperforms"
|
| 72 |
+
Answer: vượt trội
|
| 73 |
+
|
| 74 |
+
Abstract: "The integration of machine learning techniques in medical diagnosis has shown promising results. Our study focuses on the application of deep learning to detect early signs of diseases."
|
| 75 |
+
Surrounding sentence: "The integration of machine learning techniques in medical diagnosis has shown promising results."
|
| 76 |
+
Word: "integration"
|
| 77 |
+
Answer: tích hợp
|
| 78 |
+
|
| 79 |
+
# Here is my question:
|
| 80 |
+
Abstract: {abstract}
|
| 81 |
+
Surrounding sentences: {sentence}
|
| 82 |
+
Word: {word}
|
| 83 |
+
Answer:
|
| 84 |
+
|
| 85 |
+
# Output must be returned in Vietnamese only:
|
| 86 |
+
"""
|
| 87 |
+
return prompt
|
| 88 |
+
|
| 89 |
+
|
| 90 |
+
def summarizeNclassify_doc_prompt(docs):
|
| 91 |
+
prompt = f"""
|
| 92 |
+
# Introduction: You are an expert in summarizing and classify document/paper type.
|
| 93 |
+
# System instructions:
|
| 94 |
+
0. No yapping
|
| 95 |
+
1. You are given a first part of document/paper.
|
| 96 |
+
2. You must understand the document/paper
|
| 97 |
+
3. Return a concise summary about the document/paper, and the classifing document type.
|
| 98 |
+
|
| 99 |
+
# Here is my document/paper: {docs}
|
| 100 |
+
|
| 101 |
+
You must return in JSON format, field is mandatory:
|
| 102 |
+
{{
|
| 103 |
+
summary: a concise summary about the document/paper
|
| 104 |
+
type: must be "Business" or "RnD" based on document content
|
| 105 |
+
}}
|
| 106 |
+
"""
|
| 107 |
+
return prompt
|
apis/V1/prompts/translate.py
CHANGED
|
@@ -57,4 +57,4 @@ prompt = PromptTemplate(
|
|
| 57 |
)
|
| 58 |
|
| 59 |
# Chain the prompt with the LLM and parser
|
| 60 |
-
|
|
|
|
| 57 |
)
|
| 58 |
|
| 59 |
# Chain the prompt with the LLM and parser
|
| 60 |
+
chain_translate = prompt | llm | parser
|
apis/V1/prompts/translate_abstract_BIZ.py
CHANGED
|
@@ -11,6 +11,7 @@ system_instructions = SystemMessage(
|
|
| 11 |
2. Translate the given word or phrase into Vietnamese using the context.
|
| 12 |
3. If the word or phrase is not in the dictionary, return "Không tìm thấy từ này trong từ điển".
|
| 13 |
4. If the word or phrase is an acronym, expand it and provide the translation.
|
|
|
|
| 14 |
"""
|
| 15 |
)
|
| 16 |
|
|
@@ -22,25 +23,26 @@ system_examples = SystemMessage(
|
|
| 22 |
Abstract: "The efficient market hypothesis (EMH) suggests that financial markets are informationally efficient, meaning that asset prices fully reflect all available information."
|
| 23 |
Sentence: "The efficient market hypothesis (EMH) suggests that financial markets are informationally efficient."
|
| 24 |
Word: "efficient"
|
| 25 |
-
|
| 26 |
|
| 27 |
Abstract: "Quantitative easing (QE) is a monetary policy whereby a central bank purchases government securities or other securities from the market in order to increase the money supply and encourage lending and investment."
|
| 28 |
Sentence: "Quantitative easing (QE) is a monetary policy whereby a central bank purchases government securities."
|
| 29 |
Word: "Quantitative easing"
|
| 30 |
-
|
| 31 |
|
| 32 |
Abstract: "Inflation is the rate at which the general level of prices for goods and services rises, eroding purchasing power."
|
| 33 |
Sentence: "Inflation is the rate at which the general level of prices for goods and services rises."
|
| 34 |
Word: "Inflation"
|
| 35 |
-
|
| 36 |
|
| 37 |
Abstract: "GDP, or Gross Domestic Product, measures the total value of all goods and services produced within a country in a given period."
|
| 38 |
Sentence: "GDP, or Gross Domestic Product, measures the total value of all goods and services produced within a country."
|
| 39 |
Word: "GDP"
|
| 40 |
-
|
| 41 |
"""
|
| 42 |
)
|
| 43 |
|
|
|
|
| 44 |
# Function to create the prompt template dynamically based on input
|
| 45 |
def create_prompt(abstract, sentence, word):
|
| 46 |
user_message = HumanMessage(
|
|
@@ -52,7 +54,6 @@ Word: {word}
|
|
| 52 |
Translate the word to Vietnamese based on the context of the sentence and abstract.
|
| 53 |
If the word is not in the dictionary, return "Không tìm thấy từ này trong từ điển".
|
| 54 |
If it is an acronym, expand and translate it.
|
| 55 |
-
{{format_instructions}}
|
| 56 |
"""
|
| 57 |
)
|
| 58 |
messages = [system_instructions, system_examples, user_message]
|
|
@@ -60,15 +61,14 @@ If it is an acronym, expand and translate it.
|
|
| 60 |
|
| 61 |
return chat_prompt.format()
|
| 62 |
|
|
|
|
| 63 |
# Use the JSON output parser
|
| 64 |
-
parser = JsonOutputParser(pydantic_object=TranslateOutput)
|
| 65 |
|
| 66 |
# Create the prompt template
|
| 67 |
prompt = PromptTemplate(
|
| 68 |
input_variables=["abstract", "sentence", "word"],
|
| 69 |
template=create_prompt("{abstract}", "{sentence}", "{word}"),
|
| 70 |
-
partial_variables={"format_instructions": parser.get_format_instructions()},
|
| 71 |
)
|
| 72 |
|
| 73 |
# Chain the prompt with the LLM and parser
|
| 74 |
-
chain_BIZ = prompt | llm
|
|
|
|
| 11 |
2. Translate the given word or phrase into Vietnamese using the context.
|
| 12 |
3. If the word or phrase is not in the dictionary, return "Không tìm thấy từ này trong từ điển".
|
| 13 |
4. If the word or phrase is an acronym, expand it and provide the translation.
|
| 14 |
+
5. Output must be in Vietnamese.
|
| 15 |
"""
|
| 16 |
)
|
| 17 |
|
|
|
|
| 23 |
Abstract: "The efficient market hypothesis (EMH) suggests that financial markets are informationally efficient, meaning that asset prices fully reflect all available information."
|
| 24 |
Sentence: "The efficient market hypothesis (EMH) suggests that financial markets are informationally efficient."
|
| 25 |
Word: "efficient"
|
| 26 |
+
hiệu quả
|
| 27 |
|
| 28 |
Abstract: "Quantitative easing (QE) is a monetary policy whereby a central bank purchases government securities or other securities from the market in order to increase the money supply and encourage lending and investment."
|
| 29 |
Sentence: "Quantitative easing (QE) is a monetary policy whereby a central bank purchases government securities."
|
| 30 |
Word: "Quantitative easing"
|
| 31 |
+
Nới lỏng định lượng
|
| 32 |
|
| 33 |
Abstract: "Inflation is the rate at which the general level of prices for goods and services rises, eroding purchasing power."
|
| 34 |
Sentence: "Inflation is the rate at which the general level of prices for goods and services rises."
|
| 35 |
Word: "Inflation"
|
| 36 |
+
lạm phát
|
| 37 |
|
| 38 |
Abstract: "GDP, or Gross Domestic Product, measures the total value of all goods and services produced within a country in a given period."
|
| 39 |
Sentence: "GDP, or Gross Domestic Product, measures the total value of all goods and services produced within a country."
|
| 40 |
Word: "GDP"
|
| 41 |
+
Tổng sản phẩm quốc nội
|
| 42 |
"""
|
| 43 |
)
|
| 44 |
|
| 45 |
+
|
| 46 |
# Function to create the prompt template dynamically based on input
|
| 47 |
def create_prompt(abstract, sentence, word):
|
| 48 |
user_message = HumanMessage(
|
|
|
|
| 54 |
Translate the word to Vietnamese based on the context of the sentence and abstract.
|
| 55 |
If the word is not in the dictionary, return "Không tìm thấy từ này trong từ điển".
|
| 56 |
If it is an acronym, expand and translate it.
|
|
|
|
| 57 |
"""
|
| 58 |
)
|
| 59 |
messages = [system_instructions, system_examples, user_message]
|
|
|
|
| 61 |
|
| 62 |
return chat_prompt.format()
|
| 63 |
|
| 64 |
+
|
| 65 |
# Use the JSON output parser
|
|
|
|
| 66 |
|
| 67 |
# Create the prompt template
|
| 68 |
prompt = PromptTemplate(
|
| 69 |
input_variables=["abstract", "sentence", "word"],
|
| 70 |
template=create_prompt("{abstract}", "{sentence}", "{word}"),
|
|
|
|
| 71 |
)
|
| 72 |
|
| 73 |
# Chain the prompt with the LLM and parser
|
| 74 |
+
chain_BIZ = prompt | llm
|
apis/V1/prompts/translate_abstract_RnD.py
CHANGED
|
@@ -10,6 +10,7 @@ system_instructions = SystemMessage(
|
|
| 10 |
2. Translate the given word or phrase into Vietnamese using the context.
|
| 11 |
3. If the word or phrase is not in the dictionary, return "Không tìm thấy từ này trong từ điển".
|
| 12 |
4. If the word or phrase is an acronym, expand it and provide the translation.
|
|
|
|
| 13 |
"""
|
| 14 |
)
|
| 15 |
|
|
@@ -20,25 +21,26 @@ system_examples = SystemMessage(
|
|
| 20 |
Abstract: "In recent years, there has been significant progress in the development of Transformer-based models for natural language processing tasks. These models, such as BERT, have set new benchmarks in various language understanding tasks."
|
| 21 |
Sentence: "outperforms a Transformer-based BERT baseline on the GLUE language benchmark"
|
| 22 |
Word: "outperforms"
|
| 23 |
-
|
| 24 |
|
| 25 |
Abstract: "This paper explores the use of reinforcement learning in optimizing neural network architectures. We demonstrate that our approach achieves state-of-the-art results on several benchmark datasets."
|
| 26 |
Sentence: "Our approach achieves state-of-the-art results on several benchmark datasets."
|
| 27 |
Word: "approach"
|
| 28 |
-
|
| 29 |
|
| 30 |
Abstract: "The integration of machine learning techniques in medical diagnosis has shown promising results. Our study focuses on the application of deep learning to detect early signs of diseases."
|
| 31 |
Sentence: "The integration of machine learning techniques in medical diagnosis has shown promising results."
|
| 32 |
Word: "integration"
|
| 33 |
-
|
| 34 |
|
| 35 |
Abstract: "Transformer-based models like BERT have transformed NLP tasks by achieving new state-of-the-art results."
|
| 36 |
Sentence: "Transformer-based models like BERT have transformed NLP tasks."
|
| 37 |
Word: "BERT"
|
| 38 |
-
|
| 39 |
"""
|
| 40 |
)
|
| 41 |
|
|
|
|
| 42 |
def create_prompt(abstract, sentence, word):
|
| 43 |
user_message = HumanMessage(
|
| 44 |
content=f"""
|
|
@@ -49,7 +51,6 @@ Word: {word}
|
|
| 49 |
Translate the word to Vietnamese based on the context of the sentence and abstract.
|
| 50 |
If the word is not in the dictionary, return "Không tìm thấy từ này trong từ điển".
|
| 51 |
If it is an acronym, expand and translate it.
|
| 52 |
-
{{format_instructions}}
|
| 53 |
"""
|
| 54 |
)
|
| 55 |
messages = [system_instructions, system_examples, user_message]
|
|
@@ -57,15 +58,14 @@ If it is an acronym, expand and translate it.
|
|
| 57 |
|
| 58 |
return chat_prompt.format()
|
| 59 |
|
|
|
|
| 60 |
# Use the JSON output parser
|
| 61 |
-
parser = JsonOutputParser(pydantic_object=TranslateOutput)
|
| 62 |
|
| 63 |
# Create the prompt template
|
| 64 |
prompt = PromptTemplate(
|
| 65 |
input_variables=["abstract", "sentence", "word"],
|
| 66 |
template=create_prompt("{abstract}", "{sentence}", "{word}"),
|
| 67 |
-
partial_variables={"format_instructions": parser.get_format_instructions()},
|
| 68 |
)
|
| 69 |
|
| 70 |
# Chain the prompt with the LLM and parser
|
| 71 |
-
chain_RnD = prompt | llm
|
|
|
|
| 10 |
2. Translate the given word or phrase into Vietnamese using the context.
|
| 11 |
3. If the word or phrase is not in the dictionary, return "Không tìm thấy từ này trong từ điển".
|
| 12 |
4. If the word or phrase is an acronym, expand it and provide the translation.
|
| 13 |
+
5. Output must be in Vietnamese.
|
| 14 |
"""
|
| 15 |
)
|
| 16 |
|
|
|
|
| 21 |
Abstract: "In recent years, there has been significant progress in the development of Transformer-based models for natural language processing tasks. These models, such as BERT, have set new benchmarks in various language understanding tasks."
|
| 22 |
Sentence: "outperforms a Transformer-based BERT baseline on the GLUE language benchmark"
|
| 23 |
Word: "outperforms"
|
| 24 |
+
vượt trội
|
| 25 |
|
| 26 |
Abstract: "This paper explores the use of reinforcement learning in optimizing neural network architectures. We demonstrate that our approach achieves state-of-the-art results on several benchmark datasets."
|
| 27 |
Sentence: "Our approach achieves state-of-the-art results on several benchmark datasets."
|
| 28 |
Word: "approach"
|
| 29 |
+
phương pháp
|
| 30 |
|
| 31 |
Abstract: "The integration of machine learning techniques in medical diagnosis has shown promising results. Our study focuses on the application of deep learning to detect early signs of diseases."
|
| 32 |
Sentence: "The integration of machine learning techniques in medical diagnosis has shown promising results."
|
| 33 |
Word: "integration"
|
| 34 |
+
tích hợp
|
| 35 |
|
| 36 |
Abstract: "Transformer-based models like BERT have transformed NLP tasks by achieving new state-of-the-art results."
|
| 37 |
Sentence: "Transformer-based models like BERT have transformed NLP tasks."
|
| 38 |
Word: "BERT"
|
| 39 |
+
Bidirectional Encoder Representations from Transformers
|
| 40 |
"""
|
| 41 |
)
|
| 42 |
|
| 43 |
+
|
| 44 |
def create_prompt(abstract, sentence, word):
|
| 45 |
user_message = HumanMessage(
|
| 46 |
content=f"""
|
|
|
|
| 51 |
Translate the word to Vietnamese based on the context of the sentence and abstract.
|
| 52 |
If the word is not in the dictionary, return "Không tìm thấy từ này trong từ điển".
|
| 53 |
If it is an acronym, expand and translate it.
|
|
|
|
| 54 |
"""
|
| 55 |
)
|
| 56 |
messages = [system_instructions, system_examples, user_message]
|
|
|
|
| 58 |
|
| 59 |
return chat_prompt.format()
|
| 60 |
|
| 61 |
+
|
| 62 |
# Use the JSON output parser
|
|
|
|
| 63 |
|
| 64 |
# Create the prompt template
|
| 65 |
prompt = PromptTemplate(
|
| 66 |
input_variables=["abstract", "sentence", "word"],
|
| 67 |
template=create_prompt("{abstract}", "{sentence}", "{word}"),
|
|
|
|
| 68 |
)
|
| 69 |
|
| 70 |
# Chain the prompt with the LLM and parser
|
| 71 |
+
chain_RnD = prompt | llm
|
apis/V1/providers/__pycache__/__init__.cpython-311.pyc
ADDED
|
Binary file (332 Bytes). View file
|
|
|
apis/V1/providers/__pycache__/firebase_provider.cpython-311.pyc
ADDED
|
Binary file (3.04 kB). View file
|
|
|
apis/V1/providers/__pycache__/jwt_provider.cpython-311.pyc
ADDED
|
Binary file (2.14 kB). View file
|
|
|
apis/V1/providers/__pycache__/llm_provider.cpython-311.pyc
ADDED
|
Binary file (2.73 kB). View file
|
|
|
apis/V1/routes/__pycache__/auth.cpython-311.pyc
ADDED
|
Binary file (3.14 kB). View file
|
|
|
apis/V1/routes/__pycache__/chat.cpython-311.pyc
ADDED
|
Binary file (4.99 kB). View file
|
|
|
apis/V1/routes/__pycache__/project.cpython-311.pyc
ADDED
|
Binary file (6.45 kB). View file
|
|
|
apis/V1/routes/__pycache__/upload.cpython-311.pyc
ADDED
|
Binary file (8.04 kB). View file
|
|
|
apis/V1/routes/__pycache__/word.cpython-311.pyc
ADDED
|
Binary file (3.41 kB). View file
|
|
|
apis/V1/routes/auth.py
CHANGED
|
@@ -9,8 +9,8 @@ from ..utils.response_fmt import jsonResponseFmt
|
|
| 9 |
from ..middlewares.auth_middleware import get_current_user
|
| 10 |
from ..utils.utils import get_current_time
|
| 11 |
from ..models.users import UserSchema
|
|
|
|
| 12 |
|
| 13 |
-
print(get_current_time())
|
| 14 |
router = APIRouter(prefix="/auth", tags=["Authentications"])
|
| 15 |
|
| 16 |
collection_name = db["user"]
|
|
@@ -22,9 +22,8 @@ user_dependency = Annotated[User, Depends(get_current_user)]
|
|
| 22 |
"/login", status_code=status.HTTP_200_OK, response_model=_LoginResponseInterface
|
| 23 |
)
|
| 24 |
async def login(credential: Credential):
|
| 25 |
-
|
| 26 |
-
token = login_control
|
| 27 |
-
print("token", token)
|
| 28 |
return jsonResponseFmt({"token": token})
|
| 29 |
|
| 30 |
|
|
@@ -35,7 +34,7 @@ async def get_me(user: user_dependency):
|
|
| 35 |
return jsonResponseFmt(user)
|
| 36 |
|
| 37 |
|
| 38 |
-
@router.
|
| 39 |
async def logout(user: user_dependency, response: Response):
|
| 40 |
if user is None:
|
| 41 |
return jsonResponseFmt(None, msg="Authentication failed", code=401)
|
|
|
|
| 9 |
from ..middlewares.auth_middleware import get_current_user
|
| 10 |
from ..utils.utils import get_current_time
|
| 11 |
from ..models.users import UserSchema
|
| 12 |
+
from ..configs.thread_config import run_in_thread
|
| 13 |
|
|
|
|
| 14 |
router = APIRouter(prefix="/auth", tags=["Authentications"])
|
| 15 |
|
| 16 |
collection_name = db["user"]
|
|
|
|
| 22 |
"/login", status_code=status.HTTP_200_OK, response_model=_LoginResponseInterface
|
| 23 |
)
|
| 24 |
async def login(credential: Credential):
|
| 25 |
+
|
| 26 |
+
token = await run_in_thread(login_control, credential.credential)
|
|
|
|
| 27 |
return jsonResponseFmt({"token": token})
|
| 28 |
|
| 29 |
|
|
|
|
| 34 |
return jsonResponseFmt(user)
|
| 35 |
|
| 36 |
|
| 37 |
+
@router.post("/logout", status_code=status.HTTP_200_OK)
|
| 38 |
async def logout(user: user_dependency, response: Response):
|
| 39 |
if user is None:
|
| 40 |
return jsonResponseFmt(None, msg="Authentication failed", code=401)
|
apis/V1/routes/chat.py
ADDED
|
@@ -0,0 +1,80 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from fastapi import APIRouter, status
|
| 2 |
+
from ..utils.response_fmt import jsonResponseFmt
|
| 3 |
+
from ..configs.thread_config import run_in_thread
|
| 4 |
+
from ..prompts.document_type_clf import chain_type_classify
|
| 5 |
+
from ..prompts.summarize_doc import chain_summarize
|
| 6 |
+
from ..prompts.translate_abstract_BIZ import chain_BIZ
|
| 7 |
+
from ..prompts.translate_abstract_RnD import chain_RnD
|
| 8 |
+
from ..configs.llm_config import llm
|
| 9 |
+
from ..prompts.prompt import (
|
| 10 |
+
classify_document_prompt,
|
| 11 |
+
summarizeNclassify_doc_prompt,
|
| 12 |
+
translate_BIZ_prompt,
|
| 13 |
+
translate_RnD_prompt,
|
| 14 |
+
)
|
| 15 |
+
from pydantic import BaseModel, Field
|
| 16 |
+
from langchain_core.output_parsers import JsonOutputParser
|
| 17 |
+
|
| 18 |
+
router = APIRouter(prefix="/llm", tags=["LLM"])
|
| 19 |
+
|
| 20 |
+
# @router.post("/classify_document_type", status_code=status.HTTP_200_OK)
|
| 21 |
+
# async def classify_document_type(text: str):
|
| 22 |
+
# def type_classify(text):
|
| 23 |
+
# response = chain_type_classify.invoke(text)
|
| 24 |
+
# return response
|
| 25 |
+
# result = await run_in_thread(type_classify, text)
|
| 26 |
+
# return jsonResponseFmt(result)
|
| 27 |
+
|
| 28 |
+
class SummaryNClassifyDocument(BaseModel):
|
| 29 |
+
text: str
|
| 30 |
+
@router.post("/summary", status_code=status.HTTP_200_OK)
|
| 31 |
+
async def summarize_doc(body: SummaryNClassifyDocument):
|
| 32 |
+
import asyncio
|
| 33 |
+
await asyncio.sleep(5)
|
| 34 |
+
prompt = summarizeNclassify_doc_prompt(body.text)
|
| 35 |
+
result = await run_in_thread(llm.invoke, prompt)
|
| 36 |
+
result = JsonOutputParser().parse(result)
|
| 37 |
+
return jsonResponseFmt(result)
|
| 38 |
+
|
| 39 |
+
|
| 40 |
+
class TranslateAbstract(BaseModel):
|
| 41 |
+
abstract: str = Field(description="Abstract of paper", min_length=1)
|
| 42 |
+
sentence: str = Field(description="Surrounding sentence to translate", min_length=1)
|
| 43 |
+
word: str = Field(description="Word to translate", min_length=1)
|
| 44 |
+
|
| 45 |
+
|
| 46 |
+
@router.post("/translate_RnD", status_code=status.HTTP_200_OK)
|
| 47 |
+
async def translate_RnD(body: TranslateAbstract):
|
| 48 |
+
|
| 49 |
+
result = await run_in_thread(
|
| 50 |
+
llm.invoke, translate_RnD_prompt(body.abstract, body.sentence, body.word)
|
| 51 |
+
)
|
| 52 |
+
# result = JsonOutputParser().parse(result)
|
| 53 |
+
print(result)
|
| 54 |
+
return jsonResponseFmt(result)
|
| 55 |
+
|
| 56 |
+
|
| 57 |
+
@router.post("/translate_BIZ", status_code=status.HTTP_200_OK)
|
| 58 |
+
async def translate_BIZ(body: TranslateAbstract):
|
| 59 |
+
|
| 60 |
+
result = await run_in_thread(
|
| 61 |
+
translate_BIZ_prompt, body.abstract, body.sentence, body.word
|
| 62 |
+
)
|
| 63 |
+
# result = JsonOutputParser().parse(result)
|
| 64 |
+
print(result)
|
| 65 |
+
return jsonResponseFmt(result)
|
| 66 |
+
|
| 67 |
+
|
| 68 |
+
class ClassifyDocumentType(BaseModel):
|
| 69 |
+
text: str = Field(description="Text to classify", min_length=1)
|
| 70 |
+
|
| 71 |
+
|
| 72 |
+
@router.post("/classify_document_type", status_code=status.HTTP_200_OK)
|
| 73 |
+
async def classify_document_type(body: ClassifyDocumentType):
|
| 74 |
+
# def type_classify(text):
|
| 75 |
+
# response = chain_type_classify.invoke(text)
|
| 76 |
+
# return response
|
| 77 |
+
|
| 78 |
+
result = await run_in_thread(llm.invoke, summarize_doc(body.text))
|
| 79 |
+
result = JsonOutputParser().parse(result)
|
| 80 |
+
return jsonResponseFmt(result)
|
apis/V1/routes/project.py
CHANGED
|
@@ -1,4 +1,15 @@
|
|
| 1 |
-
from fastapi import
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
from fastapi.responses import FileResponse
|
| 3 |
import os
|
| 4 |
import re
|
|
@@ -14,53 +25,71 @@ from ..providers.firebase_provider import (
|
|
| 14 |
delete_file_from_storage,
|
| 15 |
download_file_from_storage,
|
| 16 |
list_all_files_in_storage,
|
| 17 |
-
upload_file_to_storage
|
|
|
|
| 18 |
from ..utils.utils import get_current_time
|
|
|
|
|
|
|
|
|
|
| 19 |
router = APIRouter(prefix="/project", tags=["Projects"])
|
| 20 |
collection = db["project"]
|
| 21 |
|
| 22 |
user_dependency = Annotated[User, Depends(get_current_user)]
|
| 23 |
|
| 24 |
|
| 25 |
-
@router.
|
| 26 |
async def list_projects(user: user_dependency):
|
| 27 |
if user is None:
|
| 28 |
return jsonResponseFmt(None, "Authentication failed", 401)
|
| 29 |
user_id = user.get("id")
|
| 30 |
-
|
| 31 |
-
return jsonResponseFmt(
|
|
|
|
| 32 |
|
|
|
|
|
|
|
| 33 |
|
| 34 |
-
|
| 35 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 36 |
if user is None:
|
| 37 |
return jsonResponseFmt(None, "Authentication failed", 401)
|
| 38 |
user_id = user.get("id")
|
| 39 |
-
project_data =
|
|
|
|
|
|
|
| 40 |
if project_data is None:
|
| 41 |
jsonResponseFmt(None, "Project not found", 404)
|
| 42 |
file_name = project_data.get("file")
|
| 43 |
-
sanitized_file_name = re.sub(r'[<>:"/\\|?*]',
|
| 44 |
destination = "storage/" + sanitized_file_name
|
| 45 |
-
download_file_from_storage
|
| 46 |
-
file_name=file_name, destination_path=destination)
|
| 47 |
|
| 48 |
def remove_file(path: str):
|
| 49 |
os.remove(path)
|
|
|
|
| 50 |
background_tasks.add_task(remove_file, destination)
|
| 51 |
-
return FileResponse(
|
|
|
|
|
|
|
| 52 |
|
| 53 |
|
| 54 |
-
@router.post(
|
| 55 |
async def create_project(
|
| 56 |
user: user_dependency,
|
| 57 |
title: str = Form(min_length=1, max_length=100),
|
| 58 |
-
file: UploadFile = File(...)
|
| 59 |
):
|
| 60 |
if user is None:
|
| 61 |
return jsonResponseFmt(None, "Authentication failed", 401)
|
| 62 |
if file.content_type != "application/pdf":
|
| 63 |
-
return jsonResponseFmt(
|
|
|
|
|
|
|
| 64 |
try:
|
| 65 |
file_path = os.path.join("storage/", file.filename)
|
| 66 |
user_id = user.get("id")
|
|
@@ -68,8 +97,10 @@ async def create_project(
|
|
| 68 |
file_name = user_email + "/" + get_current_time() + "_" + file.filename
|
| 69 |
with open(file_path, "wb") as buffer:
|
| 70 |
shutil.copyfileobj(file.file, buffer)
|
| 71 |
-
|
| 72 |
-
|
|
|
|
|
|
|
| 73 |
return jsonResponseFmt(None, "Project created successfully", 201)
|
| 74 |
except Exception as e:
|
| 75 |
return jsonResponseFmt(None, str(e), 500)
|
|
|
|
| 1 |
+
from fastapi import (
|
| 2 |
+
APIRouter,
|
| 3 |
+
status,
|
| 4 |
+
Path,
|
| 5 |
+
Depends,
|
| 6 |
+
Query,
|
| 7 |
+
File,
|
| 8 |
+
Form,
|
| 9 |
+
UploadFile,
|
| 10 |
+
HTTPException,
|
| 11 |
+
BackgroundTasks,
|
| 12 |
+
)
|
| 13 |
from fastapi.responses import FileResponse
|
| 14 |
import os
|
| 15 |
import re
|
|
|
|
| 25 |
delete_file_from_storage,
|
| 26 |
download_file_from_storage,
|
| 27 |
list_all_files_in_storage,
|
| 28 |
+
upload_file_to_storage,
|
| 29 |
+
)
|
| 30 |
from ..utils.utils import get_current_time
|
| 31 |
+
from ..configs.thread_config import run_in_thread
|
| 32 |
+
from pydantic import BaseModel, Field
|
| 33 |
+
|
| 34 |
router = APIRouter(prefix="/project", tags=["Projects"])
|
| 35 |
collection = db["project"]
|
| 36 |
|
| 37 |
user_dependency = Annotated[User, Depends(get_current_user)]
|
| 38 |
|
| 39 |
|
| 40 |
+
@router.post("/list", status_code=status.HTTP_200_OK)
|
| 41 |
async def list_projects(user: user_dependency):
|
| 42 |
if user is None:
|
| 43 |
return jsonResponseFmt(None, "Authentication failed", 401)
|
| 44 |
user_id = user.get("id")
|
| 45 |
+
result = await run_in_thread(ProjectSchema.read_all_project_by_user_id, user_id)
|
| 46 |
+
return jsonResponseFmt(result, "List of projects", 200)
|
| 47 |
+
|
| 48 |
|
| 49 |
+
class ProjectDetail(BaseModel):
|
| 50 |
+
project_id: str = Field(description="Project ID", min_length=1)
|
| 51 |
|
| 52 |
+
|
| 53 |
+
@router.post("/get_project", status_code=status.HTTP_200_OK)
|
| 54 |
+
async def get_project(
|
| 55 |
+
background_tasks: BackgroundTasks,
|
| 56 |
+
user: user_dependency,
|
| 57 |
+
body: ProjectDetail,
|
| 58 |
+
):
|
| 59 |
if user is None:
|
| 60 |
return jsonResponseFmt(None, "Authentication failed", 401)
|
| 61 |
user_id = user.get("id")
|
| 62 |
+
project_data = await run_in_thread(
|
| 63 |
+
ProjectSchema.read_project_by_id, body.project_id, user_id
|
| 64 |
+
)
|
| 65 |
if project_data is None:
|
| 66 |
jsonResponseFmt(None, "Project not found", 404)
|
| 67 |
file_name = project_data.get("file")
|
| 68 |
+
sanitized_file_name = re.sub(r'[<>:"/\\|?*]', "_", file_name)
|
| 69 |
destination = "storage/" + sanitized_file_name
|
| 70 |
+
await run_in_thread(download_file_from_storage, file_name, destination)
|
|
|
|
| 71 |
|
| 72 |
def remove_file(path: str):
|
| 73 |
os.remove(path)
|
| 74 |
+
|
| 75 |
background_tasks.add_task(remove_file, destination)
|
| 76 |
+
return FileResponse(
|
| 77 |
+
path=destination, filename="file.pdf", media_type="application/pdf"
|
| 78 |
+
)
|
| 79 |
|
| 80 |
|
| 81 |
+
@router.post("/create", status_code=status.HTTP_201_CREATED)
|
| 82 |
async def create_project(
|
| 83 |
user: user_dependency,
|
| 84 |
title: str = Form(min_length=1, max_length=100),
|
| 85 |
+
file: UploadFile = File(...),
|
| 86 |
):
|
| 87 |
if user is None:
|
| 88 |
return jsonResponseFmt(None, "Authentication failed", 401)
|
| 89 |
if file.content_type != "application/pdf":
|
| 90 |
+
return jsonResponseFmt(
|
| 91 |
+
None, "Invalid file format. Only PDF files are allowed", 400
|
| 92 |
+
)
|
| 93 |
try:
|
| 94 |
file_path = os.path.join("storage/", file.filename)
|
| 95 |
user_id = user.get("id")
|
|
|
|
| 97 |
file_name = user_email + "/" + get_current_time() + "_" + file.filename
|
| 98 |
with open(file_path, "wb") as buffer:
|
| 99 |
shutil.copyfileobj(file.file, buffer)
|
| 100 |
+
|
| 101 |
+
await run_in_thread(upload_file_to_storage, file_path, file_name)
|
| 102 |
+
await run_in_thread(ProjectSchema(title=title, file=file_name).create, user_id)
|
| 103 |
+
# ProjectSchema(title=title, file=file_name).create(user_id)
|
| 104 |
return jsonResponseFmt(None, "Project created successfully", 201)
|
| 105 |
except Exception as e:
|
| 106 |
return jsonResponseFmt(None, str(e), 500)
|
apis/V1/routes/upload.py
CHANGED
|
@@ -8,6 +8,8 @@ from ..middlewares.auth_middleware import get_current_user
|
|
| 8 |
from ..utils.response_fmt import jsonResponseFmt
|
| 9 |
import os
|
| 10 |
from typing import Dict
|
|
|
|
|
|
|
| 11 |
|
| 12 |
router = APIRouter(prefix="/upload", tags=["Upload"])
|
| 13 |
|
|
@@ -16,25 +18,34 @@ user_dependency = Annotated[User, Depends(get_current_user)]
|
|
| 16 |
UserService: Dict[str, AskImage] = {}
|
| 17 |
|
| 18 |
|
| 19 |
-
|
| 20 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
if user is None:
|
| 22 |
return jsonResponseFmt(None, msg="Authentication failed", code=401)
|
| 23 |
try:
|
| 24 |
user_id = user["id"]
|
| 25 |
user_name = user["name"]
|
| 26 |
-
response = requests.get(pdf_url)
|
| 27 |
if response.status_code == 200:
|
| 28 |
os.makedirs("storage", exist_ok=True)
|
| 29 |
with open(f"storage//{user_id}.pdf", "wb") as f:
|
| 30 |
f.write(response.content)
|
| 31 |
UserService[user_id] = AskImage()
|
| 32 |
-
convert_result =
|
| 33 |
-
f"storage//{user_id}.pdf", user_name
|
|
|
|
|
|
|
|
|
|
| 34 |
if convert_result == True:
|
| 35 |
return jsonResponseFmt(None, msg="Success", code=200)
|
| 36 |
else:
|
| 37 |
-
return jsonResponseFmt(
|
|
|
|
|
|
|
| 38 |
except:
|
| 39 |
return jsonResponseFmt(None, msg="Error", code=400)
|
| 40 |
|
|
@@ -51,25 +62,40 @@ async def upload_pdf(pdf_file: UploadFile, user: user_dependency):
|
|
| 51 |
with open(f"storage//{user_id}.pdf", "wb") as f:
|
| 52 |
f.write(contents)
|
| 53 |
UserService[user_id] = AskImage()
|
| 54 |
-
convert_result = UserService[user_id].uploaded(
|
| 55 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 56 |
if convert_result == True:
|
| 57 |
return jsonResponseFmt(None, msg="Success", code=200)
|
| 58 |
else:
|
| 59 |
-
return jsonResponseFmt(
|
|
|
|
|
|
|
| 60 |
except Exception as e:
|
| 61 |
print(e)
|
| 62 |
return jsonResponseFmt(None, msg="e", code=400)
|
| 63 |
|
| 64 |
|
| 65 |
-
|
| 66 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 67 |
if user is None:
|
| 68 |
return jsonResponseFmt(None, msg="Authentication failed", code=401)
|
| 69 |
user_id = user["id"]
|
| 70 |
try:
|
| 71 |
AI = UserService[user_id]
|
| 72 |
-
res = AI.explain_word(word=word, current_page=current_page)
|
|
|
|
| 73 |
return jsonResponseFmt(res, msg="Success", code=200)
|
| 74 |
|
| 75 |
except Exception as e:
|
|
@@ -77,14 +103,25 @@ async def explain_word(user: user_dependency, current_page: int = Query(ge=0), w
|
|
| 77 |
return jsonResponseFmt(None, msg=e, code=400)
|
| 78 |
|
| 79 |
|
| 80 |
-
|
| 81 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 82 |
if user is None:
|
| 83 |
return jsonResponseFmt(None, msg="Authentication failed", code=401)
|
| 84 |
user_id = user["id"]
|
| 85 |
try:
|
| 86 |
AI = UserService[user_id]
|
| 87 |
-
|
|
|
|
|
|
|
| 88 |
return jsonResponseFmt(res, msg="Success", code=200)
|
| 89 |
except Exception as e:
|
| 90 |
print(e)
|
|
|
|
| 8 |
from ..utils.response_fmt import jsonResponseFmt
|
| 9 |
import os
|
| 10 |
from typing import Dict
|
| 11 |
+
from ..configs.thread_config import run_in_thread
|
| 12 |
+
from pydantic import BaseModel, Field
|
| 13 |
|
| 14 |
router = APIRouter(prefix="/upload", tags=["Upload"])
|
| 15 |
|
|
|
|
| 18 |
UserService: Dict[str, AskImage] = {}
|
| 19 |
|
| 20 |
|
| 21 |
+
class UploadPDFURL(BaseModel):
|
| 22 |
+
pdf_url: str = Field(description="An url of a pdf paper", min_length=1)
|
| 23 |
+
|
| 24 |
+
|
| 25 |
+
@router.post("/pdf_url", status_code=status.HTTP_200_OK)
|
| 26 |
+
async def upload_pdf_URL(user: user_dependency, body: UploadPDFURL):
|
| 27 |
if user is None:
|
| 28 |
return jsonResponseFmt(None, msg="Authentication failed", code=401)
|
| 29 |
try:
|
| 30 |
user_id = user["id"]
|
| 31 |
user_name = user["name"]
|
| 32 |
+
response = requests.get(body.pdf_url)
|
| 33 |
if response.status_code == 200:
|
| 34 |
os.makedirs("storage", exist_ok=True)
|
| 35 |
with open(f"storage//{user_id}.pdf", "wb") as f:
|
| 36 |
f.write(response.content)
|
| 37 |
UserService[user_id] = AskImage()
|
| 38 |
+
convert_result = await run_in_thread(
|
| 39 |
+
UserService[user_id].uploaded, f"storage//{user_id}.pdf", user_name
|
| 40 |
+
)
|
| 41 |
+
# convert_result = UserService[user_id].uploaded(
|
| 42 |
+
# f"storage//{user_id}.pdf", user_name)
|
| 43 |
if convert_result == True:
|
| 44 |
return jsonResponseFmt(None, msg="Success", code=200)
|
| 45 |
else:
|
| 46 |
+
return jsonResponseFmt(
|
| 47 |
+
None, msg="Uploaded but unable to extract content", code=409
|
| 48 |
+
)
|
| 49 |
except:
|
| 50 |
return jsonResponseFmt(None, msg="Error", code=400)
|
| 51 |
|
|
|
|
| 62 |
with open(f"storage//{user_id}.pdf", "wb") as f:
|
| 63 |
f.write(contents)
|
| 64 |
UserService[user_id] = AskImage()
|
| 65 |
+
# convert_result = UserService[user_id].uploaded(
|
| 66 |
+
# f"storage//{user_id}.pdf", user_name
|
| 67 |
+
# )
|
| 68 |
+
convert_result = await run_in_thread(
|
| 69 |
+
UserService[user_id].uploaded, f"storage//{user_id}.pdf", user_name
|
| 70 |
+
)
|
| 71 |
if convert_result == True:
|
| 72 |
return jsonResponseFmt(None, msg="Success", code=200)
|
| 73 |
else:
|
| 74 |
+
return jsonResponseFmt(
|
| 75 |
+
None, msg="Uploaded but unable to extract content", code=409
|
| 76 |
+
)
|
| 77 |
except Exception as e:
|
| 78 |
print(e)
|
| 79 |
return jsonResponseFmt(None, msg="e", code=400)
|
| 80 |
|
| 81 |
|
| 82 |
+
class ExplainWord(BaseModel):
|
| 83 |
+
current_page: int = Field(description="Current page is seeing", ge=0)
|
| 84 |
+
word: str = Field("Word or phase need to be explain", min_length=1)
|
| 85 |
+
|
| 86 |
+
|
| 87 |
+
@router.post("/explain_word", status_code=status.HTTP_200_OK)
|
| 88 |
+
async def explain_word(
|
| 89 |
+
user: user_dependency,
|
| 90 |
+
body: ExplainWord,
|
| 91 |
+
):
|
| 92 |
if user is None:
|
| 93 |
return jsonResponseFmt(None, msg="Authentication failed", code=401)
|
| 94 |
user_id = user["id"]
|
| 95 |
try:
|
| 96 |
AI = UserService[user_id]
|
| 97 |
+
# res = AI.explain_word(word=word, current_page=current_page)
|
| 98 |
+
res = await run_in_thread(AI.explain_word, body.word, body.current_page)
|
| 99 |
return jsonResponseFmt(res, msg="Success", code=200)
|
| 100 |
|
| 101 |
except Exception as e:
|
|
|
|
| 103 |
return jsonResponseFmt(None, msg=e, code=400)
|
| 104 |
|
| 105 |
|
| 106 |
+
class AskQuestion(BaseModel):
|
| 107 |
+
current_page: int = Field(..., ge=0)
|
| 108 |
+
question: str = Field(..., min_length=1)
|
| 109 |
+
|
| 110 |
+
|
| 111 |
+
@router.post("/ask", status_code=status.HTTP_200_OK)
|
| 112 |
+
async def ask(
|
| 113 |
+
user: user_dependency,
|
| 114 |
+
body: AskQuestion,
|
| 115 |
+
):
|
| 116 |
+
print("current", body.current_page)
|
| 117 |
if user is None:
|
| 118 |
return jsonResponseFmt(None, msg="Authentication failed", code=401)
|
| 119 |
user_id = user["id"]
|
| 120 |
try:
|
| 121 |
AI = UserService[user_id]
|
| 122 |
+
|
| 123 |
+
# res = AI.ask(question=question, current_page=current_page)
|
| 124 |
+
res = await run_in_thread(AI.ask, body.question, body.current_page)
|
| 125 |
return jsonResponseFmt(res, msg="Success", code=200)
|
| 126 |
except Exception as e:
|
| 127 |
print(e)
|
apis/V1/routes/word.py
CHANGED
|
@@ -11,6 +11,7 @@ from ..controllers.word_controller import (
|
|
| 11 |
update_word_controller,
|
| 12 |
delete_word_controller,
|
| 13 |
)
|
|
|
|
| 14 |
|
| 15 |
router = APIRouter(prefix="/word", tags=["Words"])
|
| 16 |
collection = db["word"]
|
|
@@ -18,11 +19,12 @@ collection = db["word"]
|
|
| 18 |
user_dependency = Annotated[User, Depends(get_current_user)]
|
| 19 |
|
| 20 |
|
| 21 |
-
@router.
|
| 22 |
async def list_words(user: user_dependency):
|
| 23 |
if user is None:
|
| 24 |
return jsonResponseFmt(None, msg="Authentication failed", code=401)
|
| 25 |
-
word = list_word_controlller(user)
|
|
|
|
| 26 |
return jsonResponseFmt(word)
|
| 27 |
|
| 28 |
|
|
@@ -31,7 +33,8 @@ async def add_word(word: Word, user: user_dependency):
|
|
| 31 |
print("word", word)
|
| 32 |
if user is None:
|
| 33 |
return jsonResponseFmt(None, msg="Authentication failed", code=401)
|
| 34 |
-
return add_word_controller(user, word)
|
|
|
|
| 35 |
|
| 36 |
|
| 37 |
@router.put("/updateWord/{word_id}", status_code=status.HTTP_200_OK)
|
|
@@ -40,17 +43,16 @@ async def update_word(
|
|
| 40 |
):
|
| 41 |
if user is None:
|
| 42 |
return jsonResponseFmt(None, msg="Authentication failed", code=401)
|
| 43 |
-
return update_word_controller(user, word_id, word_data)
|
|
|
|
| 44 |
|
| 45 |
|
| 46 |
@router.delete("/deleteWord/{word_id}", status_code=status.HTTP_200_OK)
|
| 47 |
async def delete_word(user: user_dependency, word_id: str = Path(min_length=1)):
|
| 48 |
if user is None:
|
| 49 |
return jsonResponseFmt(None, msg="Authentication failed", code=401)
|
| 50 |
-
return delete_word_controller(user, word_id)
|
|
|
|
|
|
|
| 51 |
|
| 52 |
|
| 53 |
-
@router.post("/test")
|
| 54 |
-
async def test(word: Word):
|
| 55 |
-
user_id = "6661455a0b293703d07f73ba"
|
| 56 |
-
print(WordSchema(**word.dict()).add_word(user_id))
|
|
|
|
| 11 |
update_word_controller,
|
| 12 |
delete_word_controller,
|
| 13 |
)
|
| 14 |
+
from ..configs.thread_config import run_in_thread
|
| 15 |
|
| 16 |
router = APIRouter(prefix="/word", tags=["Words"])
|
| 17 |
collection = db["word"]
|
|
|
|
| 19 |
user_dependency = Annotated[User, Depends(get_current_user)]
|
| 20 |
|
| 21 |
|
| 22 |
+
@router.post("/listWord", status_code=status.HTTP_200_OK)
|
| 23 |
async def list_words(user: user_dependency):
|
| 24 |
if user is None:
|
| 25 |
return jsonResponseFmt(None, msg="Authentication failed", code=401)
|
| 26 |
+
# word = list_word_controlller(user)
|
| 27 |
+
word = await run_in_thread(list_word_controlller, user)
|
| 28 |
return jsonResponseFmt(word)
|
| 29 |
|
| 30 |
|
|
|
|
| 33 |
print("word", word)
|
| 34 |
if user is None:
|
| 35 |
return jsonResponseFmt(None, msg="Authentication failed", code=401)
|
| 36 |
+
# return add_word_controller(user, word)
|
| 37 |
+
return await run_in_thread(add_word_controller, user, word)
|
| 38 |
|
| 39 |
|
| 40 |
@router.put("/updateWord/{word_id}", status_code=status.HTTP_200_OK)
|
|
|
|
| 43 |
):
|
| 44 |
if user is None:
|
| 45 |
return jsonResponseFmt(None, msg="Authentication failed", code=401)
|
| 46 |
+
# return update_word_controller(user, word_id, word_data)
|
| 47 |
+
return await run_in_thread(update_word_controller, user, word_id, word_data)
|
| 48 |
|
| 49 |
|
| 50 |
@router.delete("/deleteWord/{word_id}", status_code=status.HTTP_200_OK)
|
| 51 |
async def delete_word(user: user_dependency, word_id: str = Path(min_length=1)):
|
| 52 |
if user is None:
|
| 53 |
return jsonResponseFmt(None, msg="Authentication failed", code=401)
|
| 54 |
+
# return delete_word_controller(user, word_id)
|
| 55 |
+
return await run_in_thread(delete_word_controller, user, word_id)
|
| 56 |
+
|
| 57 |
|
| 58 |
|
|
|
|
|
|
|
|
|
|
|
|
apis/V1/schemas/__pycache__/user_schema.cpython-311.pyc
ADDED
|
Binary file (872 Bytes). View file
|
|
|
apis/V1/utils/__pycache__/response_fmt.cpython-311.pyc
ADDED
|
Binary file (613 Bytes). View file
|
|
|
apis/V1/utils/__pycache__/utils.cpython-311.pyc
ADDED
|
Binary file (535 Bytes). View file
|
|
|
apis/__init__.py
CHANGED
|
@@ -3,9 +3,11 @@ from .V1.routes.auth import router as auth_router
|
|
| 3 |
from .V1.routes.word import router as word_router
|
| 4 |
from .V1.routes.upload import router as upload_router
|
| 5 |
from .V1.routes.project import router as project_router
|
|
|
|
| 6 |
api_router = APIRouter()
|
| 7 |
|
| 8 |
api_router.include_router(auth_router)
|
| 9 |
api_router.include_router(word_router)
|
| 10 |
api_router.include_router(upload_router)
|
| 11 |
api_router.include_router(project_router)
|
|
|
|
|
|
| 3 |
from .V1.routes.word import router as word_router
|
| 4 |
from .V1.routes.upload import router as upload_router
|
| 5 |
from .V1.routes.project import router as project_router
|
| 6 |
+
from .V1.routes.chat import router as chat_router
|
| 7 |
api_router = APIRouter()
|
| 8 |
|
| 9 |
api_router.include_router(auth_router)
|
| 10 |
api_router.include_router(word_router)
|
| 11 |
api_router.include_router(upload_router)
|
| 12 |
api_router.include_router(project_router)
|
| 13 |
+
api_router.include_router(chat_router)
|
apis/__pycache__/__init__.cpython-310.pyc
CHANGED
|
Binary files a/apis/__pycache__/__init__.cpython-310.pyc and b/apis/__pycache__/__init__.cpython-310.pyc differ
|
|
|
apis/__pycache__/__init__.cpython-311.pyc
ADDED
|
Binary file (900 Bytes). View file
|
|
|
apis/__pycache__/create_app.cpython-311.pyc
ADDED
|
Binary file (2.11 kB). View file
|
|
|
apis/create_app.py
CHANGED
|
@@ -19,11 +19,12 @@ def create_app():
|
|
| 19 |
# CORs handling
|
| 20 |
app.add_middleware(
|
| 21 |
CORSMiddleware,
|
| 22 |
-
allow_origins=["*"],
|
| 23 |
allow_credentials=True,
|
| 24 |
-
allow_methods=["*"],
|
| 25 |
-
allow_headers=["*"],
|
| 26 |
)
|
|
|
|
| 27 |
add_routes(
|
| 28 |
app,
|
| 29 |
chain_summarize,
|
|
|
|
| 19 |
# CORs handling
|
| 20 |
app.add_middleware(
|
| 21 |
CORSMiddleware,
|
| 22 |
+
allow_origins=["*"], # Allow all origins
|
| 23 |
allow_credentials=True,
|
| 24 |
+
allow_methods=["*"], # Allow all methods (GET, POST, etc.)
|
| 25 |
+
allow_headers=["*"], # Allow all headers
|
| 26 |
)
|
| 27 |
+
|
| 28 |
add_routes(
|
| 29 |
app,
|
| 30 |
chain_summarize,
|