Spaces:
Running
Running
Upload 27 files
Browse files- Dockerfile +8 -0
- app.py +51 -0
- knowledgeassistant/__init__.py +0 -0
- knowledgeassistant/cloud/__init__.py +0 -0
- knowledgeassistant/components/RAG.py +92 -0
- knowledgeassistant/components/__init__.py +0 -0
- knowledgeassistant/components/keywordextraction.py +51 -0
- knowledgeassistant/components/summarization.py +38 -0
- knowledgeassistant/constant/__init__.py +0 -0
- knowledgeassistant/constant/pipeline/__init__.py +19 -0
- knowledgeassistant/entity/__init__.py +0 -0
- knowledgeassistant/entity/artifact_entity.py +0 -0
- knowledgeassistant/entity/config_entity.py +58 -0
- knowledgeassistant/exception/__init__.py +0 -0
- knowledgeassistant/exception/exception.py +15 -0
- knowledgeassistant/logging/__init__.py +0 -0
- knowledgeassistant/logging/logger.py +16 -0
- knowledgeassistant/pipeline/__init__.py +0 -0
- knowledgeassistant/utils/__init__.py +0 -0
- knowledgeassistant/utils/main_utils/__init__.py +0 -0
- knowledgeassistant/utils/main_utils/utils.py +26 -0
- knowledgeassistant/utils/ml_utils/__init__.py +0 -0
- requirements.txt +18 -0
- static/script.js +43 -0
- static/style.css +90 -0
- templates/index.html +44 -0
- templates/results.html +3 -0
Dockerfile
ADDED
@@ -0,0 +1,8 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
FROM python:3.12.9-slim-bookworm
|
2 |
+
WORKDIR /app
|
3 |
+
COPY requirements.txt .
|
4 |
+
RUN pip install --no-cache-dir -r requirements.txt
|
5 |
+
RUN mkdir -p /app/logs && chmod 777 /app/logs
|
6 |
+
COPY . .
|
7 |
+
EXPOSE 8080
|
8 |
+
CMD ["python", "app.py"]
|
app.py
ADDED
@@ -0,0 +1,51 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from flask import Flask, render_template, request
|
2 |
+
from knowledgeassistant.components.summarization import DataSummarization
|
3 |
+
from knowledgeassistant.components.keywordextraction import KeywordExtraction
|
4 |
+
from knowledgeassistant.components.RAG import RAG
|
5 |
+
from knowledgeassistant.entity.config_entity import DataSummarizationConfig, PipelineConfig, DataInputConfig, KeywordExtractionConfig, RAGConfig
|
6 |
+
from knowledgeassistant.utils.main_utils.utils import write_txt_file, read_txt_file
|
7 |
+
|
8 |
+
app = Flask(__name__)
|
9 |
+
|
10 |
+
@app.route('/', methods=['GET', 'POST'])
|
11 |
+
def home():
|
12 |
+
if request.method == 'POST':
|
13 |
+
pipeline_config = PipelineConfig()
|
14 |
+
data_input_config = DataInputConfig(pipeline_config)
|
15 |
+
|
16 |
+
user_text = request.form['user_text']
|
17 |
+
user_option = request.form['tasks']
|
18 |
+
result_text = ""
|
19 |
+
|
20 |
+
if user_option == "Q&A":
|
21 |
+
user_question = request.form['user_question']
|
22 |
+
else:
|
23 |
+
user_number = int(request.form['user_number'])
|
24 |
+
|
25 |
+
input_text_path = data_input_config.input_text_file_path
|
26 |
+
write_txt_file(input_text_path, user_text, replace=True)
|
27 |
+
|
28 |
+
if user_option == "summarization":
|
29 |
+
data_summarization_config = DataSummarizationConfig(pipeline_config)
|
30 |
+
summarizer = DataSummarization(data_summarization_config)
|
31 |
+
file_path = data_summarization_config.summarized_text_file_path
|
32 |
+
summarizer.initiate_data_summarization(input_text_path=input_text_path, min_length=int(user_number))
|
33 |
+
elif user_option == "keywords":
|
34 |
+
keyword_extraction_config = KeywordExtractionConfig(pipeline_config)
|
35 |
+
extractor = KeywordExtraction(keyword_extraction_config)
|
36 |
+
file_path = keyword_extraction_config.extracted_keywords_file_path
|
37 |
+
extractor.initiate_keyword_extraction(input_text_path=input_text_path, keywords_count=int(user_number))
|
38 |
+
elif user_option == "Q&A":
|
39 |
+
rag_config = RAGConfig(pipeline_config)
|
40 |
+
rag = RAG(rag_config)
|
41 |
+
file_path = rag_config.rag_generated_text_path
|
42 |
+
rag.initiate_rag(input_text_path=input_text_path, query=user_question)
|
43 |
+
|
44 |
+
result_text = read_txt_file(file_path=file_path)
|
45 |
+
|
46 |
+
return render_template("index.html", task=user_option, text=result_text, user_text=user_text)
|
47 |
+
|
48 |
+
return render_template("index.html", task=None, text=None, user_text=None)
|
49 |
+
|
50 |
+
if __name__ == '__main__':
|
51 |
+
app.run(host='0.0.0.0', port=8080)
|
knowledgeassistant/__init__.py
ADDED
File without changes
|
knowledgeassistant/cloud/__init__.py
ADDED
File without changes
|
knowledgeassistant/components/RAG.py
ADDED
@@ -0,0 +1,92 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from knowledgeassistant.logging.logger import logging
|
2 |
+
from knowledgeassistant.exception.exception import KnowledgeAssistantException
|
3 |
+
|
4 |
+
from knowledgeassistant.entity.config_entity import RAGConfig
|
5 |
+
from knowledgeassistant.utils.main_utils.utils import read_txt_file, write_txt_file
|
6 |
+
|
7 |
+
import os
|
8 |
+
import sys
|
9 |
+
from langchain.text_splitter import RecursiveCharacterTextSplitter
|
10 |
+
from langchain_core.documents import Document
|
11 |
+
from langchain_ollama import OllamaEmbeddings
|
12 |
+
from langchain_community.vectorstores import FAISS
|
13 |
+
from together import Together
|
14 |
+
from langchain.chains import RetrievalQA
|
15 |
+
from langchain_core.language_models import LLM
|
16 |
+
|
17 |
+
from dotenv import load_dotenv
|
18 |
+
import typing
|
19 |
+
|
20 |
+
load_dotenv()
|
21 |
+
os.environ["TOGETHER_API_KEY"] = os.getenv("TOGETHER_API_KEY")
|
22 |
+
|
23 |
+
class RAG:
|
24 |
+
def __init__(self, rag_config: RAGConfig):
|
25 |
+
try:
|
26 |
+
self.rag_config = rag_config
|
27 |
+
except Exception as e:
|
28 |
+
raise KnowledgeAssistantException(e, sys)
|
29 |
+
|
30 |
+
def split_text(self, input_text_path: str):
|
31 |
+
try:
|
32 |
+
text_splitter = RecursiveCharacterTextSplitter(chunk_size = 1000, chunk_overlap = 200)
|
33 |
+
raw_documents = text_splitter.split_text(text = read_txt_file(file_path = input_text_path))
|
34 |
+
documents = [Document(page_content=text) for text in raw_documents]
|
35 |
+
return documents
|
36 |
+
except Exception as e:
|
37 |
+
raise KnowledgeAssistantException(e, sys)
|
38 |
+
|
39 |
+
def create_and_store_embeddings(self, documents: list):
|
40 |
+
try:
|
41 |
+
db = FAISS.from_documents(documents, OllamaEmbeddings(model="nomic-embed-text"))
|
42 |
+
return db
|
43 |
+
except Exception as e:
|
44 |
+
raise KnowledgeAssistantException(e, sys)
|
45 |
+
|
46 |
+
class TogetherLLM(LLM):
|
47 |
+
model_name: str = "meta-llama/Llama-3-8b-chat-hf"
|
48 |
+
|
49 |
+
@property
|
50 |
+
def _llm_type(self) -> str:
|
51 |
+
return "together_ai"
|
52 |
+
|
53 |
+
def _call(self, prompt: str, stop: typing.Optional[typing.List[str]] = None) -> str:
|
54 |
+
client = Together()
|
55 |
+
response = client.chat.completions.create(
|
56 |
+
model=self.model_name,
|
57 |
+
messages=[{"role": "user", "content": prompt}],
|
58 |
+
)
|
59 |
+
return response.choices[0].message.content
|
60 |
+
|
61 |
+
def retrieval(self, llm, db, query):
|
62 |
+
try:
|
63 |
+
chain = RetrievalQA.from_chain_type(
|
64 |
+
llm=llm,
|
65 |
+
retriever=db.as_retriever()
|
66 |
+
)
|
67 |
+
result = chain.invoke(query)
|
68 |
+
return result
|
69 |
+
except Exception as e:
|
70 |
+
raise KnowledgeAssistantException(e, sys)
|
71 |
+
|
72 |
+
def initiate_rag(self, input_text_path: str, query: str):
|
73 |
+
try:
|
74 |
+
docs = self.split_text(input_text_path = input_text_path)
|
75 |
+
logging.info("Splitted Text into Chunks Successfully")
|
76 |
+
store = self.create_and_store_embeddings(documents = docs)
|
77 |
+
logging.info("Successfully stored vector embeddings")
|
78 |
+
llm = self.TogetherLLM()
|
79 |
+
logging.info("Successfully loaded the llm")
|
80 |
+
result = self.retrieval(
|
81 |
+
llm = llm,
|
82 |
+
db = store,
|
83 |
+
query = query
|
84 |
+
)
|
85 |
+
logging.info("Successfully Generated Results")
|
86 |
+
write_txt_file(
|
87 |
+
file_path = self.rag_config.rag_generated_text_path,
|
88 |
+
content = result['result']
|
89 |
+
)
|
90 |
+
logging.info("Successfully wrote results in txt file")
|
91 |
+
except Exception as e:
|
92 |
+
raise KnowledgeAssistantException(e, sys)
|
knowledgeassistant/components/__init__.py
ADDED
File without changes
|
knowledgeassistant/components/keywordextraction.py
ADDED
@@ -0,0 +1,51 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from knowledgeassistant.exception.exception import KnowledgeAssistantException
|
2 |
+
from knowledgeassistant.logging.logger import logging
|
3 |
+
|
4 |
+
from knowledgeassistant.entity.config_entity import KeywordExtractionConfig
|
5 |
+
from knowledgeassistant.utils.main_utils.utils import read_txt_file, write_txt_file
|
6 |
+
|
7 |
+
import sys
|
8 |
+
import numpy as np
|
9 |
+
from sklearn.feature_extraction.text import CountVectorizer, TfidfTransformer
|
10 |
+
|
11 |
+
class KeywordExtraction:
|
12 |
+
def __init__(self, keyword_extraction_config: KeywordExtractionConfig):
|
13 |
+
try:
|
14 |
+
self.keyword_extraction_config = keyword_extraction_config
|
15 |
+
except Exception as e:
|
16 |
+
raise KnowledgeAssistantException(e, sys)
|
17 |
+
|
18 |
+
def extract_keywords(self, input_text_path: str, keywords_count: str):
|
19 |
+
try:
|
20 |
+
vectorizer = CountVectorizer(stop_words='english', token_pattern=r'(?u)\b[a-zA-Z]+\b')
|
21 |
+
transformer = TfidfTransformer()
|
22 |
+
logging.info("Vectorizer and Transformer successfully setup")
|
23 |
+
|
24 |
+
text = read_txt_file(file_path=input_text_path)
|
25 |
+
word_count_matrix = vectorizer.fit_transform([text])
|
26 |
+
tfidf_matrix = transformer.fit_transform(word_count_matrix)
|
27 |
+
logging.info("Successfully calculated word count and their tfidf scores")
|
28 |
+
|
29 |
+
feature_array = np.array([word for word in vectorizer.get_feature_names_out() if word.isalpha()])
|
30 |
+
tfidf_sorting = np.argsort(tfidf_matrix.toarray()).flatten()[::-1]
|
31 |
+
logging.info("Successfully extracted keywords and sorted in descending order of their tfidf scores")
|
32 |
+
|
33 |
+
top_keywords = feature_array[tfidf_sorting][:keywords_count]
|
34 |
+
content = "\n".join(top_keywords)
|
35 |
+
write_txt_file(
|
36 |
+
self.keyword_extraction_config.extracted_keywords_file_path,
|
37 |
+
content,
|
38 |
+
True
|
39 |
+
)
|
40 |
+
logging.info(f"Successfully extracted and wrote top {keywords_count} keywords from text")
|
41 |
+
except Exception as e:
|
42 |
+
raise KnowledgeAssistantException(e, sys)
|
43 |
+
|
44 |
+
def initiate_keyword_extraction(self, input_text_path: str, keywords_count: str):
|
45 |
+
try:
|
46 |
+
self.extract_keywords(
|
47 |
+
input_text_path = input_text_path,
|
48 |
+
keywords_count = keywords_count
|
49 |
+
)
|
50 |
+
except Exception as e:
|
51 |
+
raise KnowledgeAssistantException(e, sys)
|
knowledgeassistant/components/summarization.py
ADDED
@@ -0,0 +1,38 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from knowledgeassistant.exception.exception import KnowledgeAssistantException
|
2 |
+
from knowledgeassistant.logging.logger import logging
|
3 |
+
|
4 |
+
from knowledgeassistant.entity.config_entity import DataSummarizationConfig
|
5 |
+
from knowledgeassistant.utils.main_utils.utils import write_txt_file, read_txt_file
|
6 |
+
|
7 |
+
import sys
|
8 |
+
from transformers import pipeline
|
9 |
+
|
10 |
+
class DataSummarization:
|
11 |
+
def __init__(self, data_summarization_config: DataSummarizationConfig):
|
12 |
+
try:
|
13 |
+
self.data_summarization_config = data_summarization_config
|
14 |
+
except Exception as e:
|
15 |
+
raise KnowledgeAssistantException(e, sys)
|
16 |
+
|
17 |
+
def summarize(self, input_text_path: str, min_length: int):
|
18 |
+
try:
|
19 |
+
pipe = pipeline("summarization", model="facebook/bart-large-cnn")
|
20 |
+
logging.info("Summarization Pipeline Successfully Setup")
|
21 |
+
|
22 |
+
text = read_txt_file(input_text_path)
|
23 |
+
summary = pipe(text, min_length = min_length, do_sample = False)
|
24 |
+
logging.info("Text successfully summarized")
|
25 |
+
|
26 |
+
write_txt_file(self.data_summarization_config.summarized_text_file_path, summary[0].get("summary_text"))
|
27 |
+
logging.info("Successfully wrote summarized text")
|
28 |
+
except Exception as e:
|
29 |
+
raise KnowledgeAssistantException(e, sys)
|
30 |
+
|
31 |
+
def initiate_data_summarization(self, input_text_path: str, min_length: int):
|
32 |
+
try:
|
33 |
+
self.summarize(
|
34 |
+
input_text_path = input_text_path,
|
35 |
+
min_length = min_length
|
36 |
+
)
|
37 |
+
except Exception as e:
|
38 |
+
raise KnowledgeAssistantException(e, sys)
|
knowledgeassistant/constant/__init__.py
ADDED
File without changes
|
knowledgeassistant/constant/pipeline/__init__.py
ADDED
@@ -0,0 +1,19 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
PIPELINE_NAME: str = "KnowledgeAssistant"
|
2 |
+
ARTIFACT_DIR: str = "Artifacts"
|
3 |
+
|
4 |
+
|
5 |
+
INPUT_TEXT_DIR: str = "input_text"
|
6 |
+
INPUT_TEXT_FILE_NAME: str = "input_text.txt"
|
7 |
+
|
8 |
+
|
9 |
+
DATA_SUMMARIZATION_DIR_NAME: str = "data_summarization"
|
10 |
+
SUMMARIZED_TEXT_DIR: str = "summarized_text"
|
11 |
+
SUMMARIZED_TEXT_FILE_NAME: str = "summarized_text.txt"
|
12 |
+
|
13 |
+
KEYWORD_EXTRACTION_DIR_NAME: str = "keyword_extraction"
|
14 |
+
EXTRACTED_KEYWORDS_DIR: str = "keywords"
|
15 |
+
EXTRACTED_KEYWORDS_FILE_NAME: str = "keywords.txt"
|
16 |
+
|
17 |
+
RAG_DIR_NAME: str = "RAG"
|
18 |
+
RAG_DIR: str = "generation"
|
19 |
+
RAG_FILE_NAME: str = "generated_text.txt"
|
knowledgeassistant/entity/__init__.py
ADDED
File without changes
|
knowledgeassistant/entity/artifact_entity.py
ADDED
File without changes
|
knowledgeassistant/entity/config_entity.py
ADDED
@@ -0,0 +1,58 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
from datetime import datetime
|
3 |
+
from knowledgeassistant.constant import pipeline
|
4 |
+
|
5 |
+
class PipelineConfig:
|
6 |
+
def __init__(self, timestamp = datetime.now()):
|
7 |
+
timestamp = timestamp.strftime("%d_%m_%Y_%H_%M_%S")
|
8 |
+
self.pipeline_name = pipeline.PIPELINE_NAME
|
9 |
+
self.artifact_name = pipeline.ARTIFACT_DIR
|
10 |
+
self.artifact_dir = os.path.join(
|
11 |
+
self.artifact_name,
|
12 |
+
timestamp
|
13 |
+
)
|
14 |
+
self.timestamp: str = timestamp
|
15 |
+
|
16 |
+
class DataInputConfig:
|
17 |
+
def __init__(self, pipeline_config: PipelineConfig):
|
18 |
+
self.input_text_file_path: str = os.path.join(
|
19 |
+
pipeline_config.artifact_dir,
|
20 |
+
pipeline.INPUT_TEXT_DIR,
|
21 |
+
pipeline.INPUT_TEXT_FILE_NAME
|
22 |
+
)
|
23 |
+
|
24 |
+
class DataSummarizationConfig:
|
25 |
+
def __init__(self, pipeline_config: PipelineConfig):
|
26 |
+
self.data_summarization_dir: str = os.path.join(
|
27 |
+
pipeline_config.artifact_dir,
|
28 |
+
pipeline.DATA_SUMMARIZATION_DIR_NAME
|
29 |
+
)
|
30 |
+
self.summarized_text_file_path: str = os.path.join(
|
31 |
+
self.data_summarization_dir,
|
32 |
+
pipeline.SUMMARIZED_TEXT_DIR,
|
33 |
+
pipeline.SUMMARIZED_TEXT_FILE_NAME
|
34 |
+
)
|
35 |
+
|
36 |
+
class KeywordExtractionConfig:
|
37 |
+
def __init__(self, pipeline_config: PipelineConfig):
|
38 |
+
self.keyword_extraction_dir: str = os.path.join(
|
39 |
+
pipeline_config.artifact_dir,
|
40 |
+
pipeline.KEYWORD_EXTRACTION_DIR_NAME
|
41 |
+
)
|
42 |
+
self.extracted_keywords_file_path: str = os.path.join(
|
43 |
+
self.keyword_extraction_dir,
|
44 |
+
pipeline.EXTRACTED_KEYWORDS_DIR,
|
45 |
+
pipeline.EXTRACTED_KEYWORDS_FILE_NAME
|
46 |
+
)
|
47 |
+
|
48 |
+
class RAGConfig:
|
49 |
+
def __init__(self, pipeline_config: PipelineConfig):
|
50 |
+
self.RAG_dir: str = os.path.join(
|
51 |
+
pipeline_config.artifact_dir,
|
52 |
+
pipeline.RAG_DIR_NAME
|
53 |
+
)
|
54 |
+
self.rag_generated_text_path: str = os.path.join(
|
55 |
+
self.RAG_dir,
|
56 |
+
pipeline.RAG_DIR,
|
57 |
+
pipeline.RAG_FILE_NAME
|
58 |
+
)
|
knowledgeassistant/exception/__init__.py
ADDED
File without changes
|
knowledgeassistant/exception/exception.py
ADDED
@@ -0,0 +1,15 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import sys
|
2 |
+
from knowledgeassistant.logging import logger
|
3 |
+
|
4 |
+
class KnowledgeAssistantException(Exception):
|
5 |
+
def __init__(self,error_message,error_details:sys):
|
6 |
+
self.error_message = error_message
|
7 |
+
_,_,exc_tb = error_details.exc_info()
|
8 |
+
|
9 |
+
self.lineno=exc_tb.tb_lineno
|
10 |
+
self.file_name=exc_tb.tb_frame.f_code.co_filename
|
11 |
+
|
12 |
+
def __str__(self):
|
13 |
+
return "Error occured in python script name [{0}] line number [{1}] error message [{2}]".format(
|
14 |
+
self.file_name, self.lineno, str(self.error_message)
|
15 |
+
)
|
knowledgeassistant/logging/__init__.py
ADDED
File without changes
|
knowledgeassistant/logging/logger.py
ADDED
@@ -0,0 +1,16 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import logging
|
3 |
+
from datetime import datetime
|
4 |
+
|
5 |
+
LOG_FILE = f"{datetime.now().strftime('%d_%m_%Y_%H_%M_%S')}.log"
|
6 |
+
|
7 |
+
logs_path = os.path.join(os.getcwd(),"logs",LOG_FILE)
|
8 |
+
os.makedirs(logs_path, exist_ok=True)
|
9 |
+
|
10 |
+
LOG_FILE_PATH = os.path.join(logs_path, LOG_FILE)
|
11 |
+
|
12 |
+
logging.basicConfig(
|
13 |
+
filename = LOG_FILE_PATH,
|
14 |
+
format = "[ %(asctime)s ] : %(lineno)d : %(name)s : %(levelname)s : %(module)s : %(message)s",
|
15 |
+
level = logging.INFO,
|
16 |
+
)
|
knowledgeassistant/pipeline/__init__.py
ADDED
File without changes
|
knowledgeassistant/utils/__init__.py
ADDED
File without changes
|
knowledgeassistant/utils/main_utils/__init__.py
ADDED
File without changes
|
knowledgeassistant/utils/main_utils/utils.py
ADDED
@@ -0,0 +1,26 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from knowledgeassistant.exception.exception import KnowledgeAssistantException
|
2 |
+
from knowledgeassistant.logging.logger import logging
|
3 |
+
import os
|
4 |
+
import sys
|
5 |
+
|
6 |
+
def write_txt_file(file_path: str, content: str, replace: bool = False):
|
7 |
+
try:
|
8 |
+
if replace and os.path.exists(file_path):
|
9 |
+
os.remove(file_path)
|
10 |
+
logging.info(f"Successfully removed the file: {file_path}")
|
11 |
+
dir_path = os.path.dirname(file_path)
|
12 |
+
logging.info(f"Successfully create the file: {file_path}")
|
13 |
+
os.makedirs(dir_path, exist_ok = True)
|
14 |
+
with open(file_path, 'w', encoding='utf-8') as txt_file:
|
15 |
+
txt_file.write(content)
|
16 |
+
except Exception as e:
|
17 |
+
raise KnowledgeAssistantException(e, sys)
|
18 |
+
|
19 |
+
def read_txt_file(file_path: str) -> str:
|
20 |
+
try:
|
21 |
+
with open(file_path, 'r', encoding='utf-8') as txt_file:
|
22 |
+
text = txt_file.read()
|
23 |
+
logging.info("Successfully read the file")
|
24 |
+
return text
|
25 |
+
except Exception as e:
|
26 |
+
raise KnowledgeAssistantException(e, sys)
|
knowledgeassistant/utils/ml_utils/__init__.py
ADDED
File without changes
|
requirements.txt
ADDED
@@ -0,0 +1,18 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
ipykernel
|
2 |
+
pandas
|
3 |
+
numpy
|
4 |
+
flask
|
5 |
+
python-dotenv
|
6 |
+
transformers
|
7 |
+
torch
|
8 |
+
torchvision
|
9 |
+
torchaudio
|
10 |
+
--extra-index-url https://download.pytorch.org/whl/cu126
|
11 |
+
scikit-learn
|
12 |
+
langchain
|
13 |
+
langchain-community
|
14 |
+
langchain-core
|
15 |
+
ollama
|
16 |
+
chromadb
|
17 |
+
faiss-cpu
|
18 |
+
together
|
static/script.js
ADDED
@@ -0,0 +1,43 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
document.addEventListener("DOMContentLoaded", function () {
|
2 |
+
function toggleElements() {
|
3 |
+
let taskDropdown = document.querySelector("select[name='tasks']");
|
4 |
+
let numberInput = document.querySelector("input[name='user_number']");
|
5 |
+
let numberLabel = document.getElementById("number_label");
|
6 |
+
let questionInput = document.querySelector("input[name='user_question']");
|
7 |
+
let questionLabel = document.getElementById("question_label");
|
8 |
+
let submitButton = document.getElementById("submit_btn");
|
9 |
+
|
10 |
+
if (taskDropdown.value === "summarization") {
|
11 |
+
numberInput.style.display = "inline";
|
12 |
+
numberLabel.style.display = "inline";
|
13 |
+
numberInput.disabled = false;
|
14 |
+
numberLabel.textContent = "Enter minimum length: ";
|
15 |
+
questionInput.style.display = "none";
|
16 |
+
questionLabel.style.display = "none";
|
17 |
+
submitButton.disabled = false;
|
18 |
+
} else if (taskDropdown.value === "keywords") {
|
19 |
+
numberInput.style.display = "inline";
|
20 |
+
numberLabel.style.display = "inline";
|
21 |
+
numberInput.disabled = false;
|
22 |
+
numberLabel.textContent = "Count: ";
|
23 |
+
questionInput.style.display = "none";
|
24 |
+
questionLabel.style.display = "none";
|
25 |
+
submitButton.disabled = false;
|
26 |
+
} else if (taskDropdown.value === "Q&A") {
|
27 |
+
numberInput.style.display = "none";
|
28 |
+
numberLabel.style.display = "none";
|
29 |
+
questionInput.style.display = "inline";
|
30 |
+
questionLabel.style.display = "inline";
|
31 |
+
submitButton.disabled = false;
|
32 |
+
} else {
|
33 |
+
numberInput.style.display = "none";
|
34 |
+
numberLabel.style.display = "none";
|
35 |
+
questionInput.style.display = "none";
|
36 |
+
questionLabel.style.display = "none";
|
37 |
+
submitButton.disabled = true;
|
38 |
+
}
|
39 |
+
}
|
40 |
+
|
41 |
+
document.querySelector("select[name='tasks']").addEventListener("change", toggleElements);
|
42 |
+
toggleElements();
|
43 |
+
});
|
static/style.css
ADDED
@@ -0,0 +1,90 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
* {
|
2 |
+
margin: 0;
|
3 |
+
padding: 0;
|
4 |
+
box-sizing: border-box;
|
5 |
+
}
|
6 |
+
|
7 |
+
body {
|
8 |
+
font-family: Arial, sans-serif;
|
9 |
+
background-color: #f4f4f4;
|
10 |
+
display: flex;
|
11 |
+
justify-content: center;
|
12 |
+
align-items: center;
|
13 |
+
height: 100vh;
|
14 |
+
}
|
15 |
+
|
16 |
+
.container {
|
17 |
+
width: 50%;
|
18 |
+
background: white;
|
19 |
+
padding: 30px;
|
20 |
+
box-shadow: 0 4px 10px rgba(0, 0, 0, 0.1);
|
21 |
+
border-radius: 8px;
|
22 |
+
text-align: center;
|
23 |
+
}
|
24 |
+
|
25 |
+
h1 {
|
26 |
+
color: #333;
|
27 |
+
margin-bottom: 20px;
|
28 |
+
}
|
29 |
+
|
30 |
+
label {
|
31 |
+
font-size: 16px;
|
32 |
+
font-weight: bold;
|
33 |
+
display: block;
|
34 |
+
margin-top: 15px;
|
35 |
+
color: #555;
|
36 |
+
text-align: left;
|
37 |
+
}
|
38 |
+
|
39 |
+
textarea, select, input {
|
40 |
+
width: 100%;
|
41 |
+
padding: 10px;
|
42 |
+
margin-top: 5px;
|
43 |
+
border: 1px solid #ccc;
|
44 |
+
border-radius: 5px;
|
45 |
+
font-size: 16px;
|
46 |
+
}
|
47 |
+
|
48 |
+
select {
|
49 |
+
cursor: pointer;
|
50 |
+
}
|
51 |
+
|
52 |
+
button {
|
53 |
+
background: #4CAF50;
|
54 |
+
color: white;
|
55 |
+
padding: 12px 20px;
|
56 |
+
margin-top: 20px;
|
57 |
+
border: none;
|
58 |
+
cursor: pointer;
|
59 |
+
border-radius: 5px;
|
60 |
+
font-size: 16px;
|
61 |
+
width: 100%;
|
62 |
+
transition: background 0.3s ease;
|
63 |
+
}
|
64 |
+
|
65 |
+
button:disabled {
|
66 |
+
background: #ccc;
|
67 |
+
cursor: not-allowed;
|
68 |
+
}
|
69 |
+
|
70 |
+
button:hover:not(:disabled) {
|
71 |
+
background: #45a049;
|
72 |
+
}
|
73 |
+
|
74 |
+
#number_label {
|
75 |
+
display: none;
|
76 |
+
}
|
77 |
+
|
78 |
+
#number {
|
79 |
+
display: none;
|
80 |
+
}
|
81 |
+
|
82 |
+
.result-section {
|
83 |
+
margin-top: 20px;
|
84 |
+
padding: 15px;
|
85 |
+
background-color: #eef;
|
86 |
+
border-left: 5px solid #4CAF50;
|
87 |
+
border-radius: 5px;
|
88 |
+
font-size: 16px;
|
89 |
+
text-align: left;
|
90 |
+
}
|
templates/index.html
ADDED
@@ -0,0 +1,44 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
<!DOCTYPE html>
|
2 |
+
<html lang="en">
|
3 |
+
<head>
|
4 |
+
<meta charset="UTF-8">
|
5 |
+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
6 |
+
<title>Knowledge Assistant</title>
|
7 |
+
<link rel="stylesheet" href="static/style.css">
|
8 |
+
<script src="static/script.js" defer></script>
|
9 |
+
</head>
|
10 |
+
<body>
|
11 |
+
<div class="container">
|
12 |
+
<h1>Enter Details</h1>
|
13 |
+
<form action="/" method="POST">
|
14 |
+
<label for="user_text">Enter Your Text: </label>
|
15 |
+
<textarea name="user_text" rows="4" cols="50" required>{{ user_text if user_text else '' }}</textarea>
|
16 |
+
<br><br>
|
17 |
+
|
18 |
+
<label for="tasks">Choose a Task:</label>
|
19 |
+
<select name="tasks" required onchange="toggleElements()">
|
20 |
+
<option value="none">--Select--</option>
|
21 |
+
<option value="summarization" {% if task == 'summarization' %}selected{% endif %}>Summarize Text</option>
|
22 |
+
<option value="keywords" {% if task == 'keywords' %}selected{% endif %}>Get Keywords</option>
|
23 |
+
<option value="Q&A" {% if task == 'Q&A' %}selected{% endif %}>Ask Questions</option>
|
24 |
+
</select>
|
25 |
+
<br><br>
|
26 |
+
|
27 |
+
<label id="number_label" for="user_number" style="display: none;"></label>
|
28 |
+
<input type="number" id="number" name="user_number" style="display: none;">
|
29 |
+
<label id="question_label" for="user_question" style="display: none;">Enter Your Question:</label>
|
30 |
+
<input type="text" id="question" name="user_question" style="display: none;">
|
31 |
+
<br><br>
|
32 |
+
|
33 |
+
<button type="submit" id="submit_btn" disabled>Submit</button>
|
34 |
+
</form>
|
35 |
+
|
36 |
+
{% if text %}
|
37 |
+
<div class="result-section">
|
38 |
+
<h2>Result for {{ task }}:</h2>
|
39 |
+
<p>{{ text }}</p>
|
40 |
+
</div>
|
41 |
+
{% endif %}
|
42 |
+
</div>
|
43 |
+
</body>
|
44 |
+
</html>
|
templates/results.html
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
<h1> {{ task }}: </h1>
|
2 |
+
<br>
|
3 |
+
{{ text }}
|