Spaces:
Paused
Paused
Shreyas094
commited on
Update app.py
Browse files
app.py
CHANGED
@@ -15,6 +15,8 @@ from langchain_core.runnables import RunnableParallel, RunnablePassthrough
|
|
15 |
from langchain_core.documents import Document
|
16 |
huggingface_token = os.environ.get("HUGGINGFACE_TOKEN")
|
17 |
|
|
|
|
|
18 |
def load_and_split_document_basic(file):
|
19 |
"""Loads and splits the document into pages."""
|
20 |
loader = PyPDFLoader(file.name)
|
@@ -59,11 +61,11 @@ def get_model(temperature, top_p, repetition_penalty):
|
|
59 |
"temperature": temperature,
|
60 |
"top_p": top_p,
|
61 |
"repetition_penalty": repetition_penalty,
|
62 |
-
"max_length":
|
63 |
},
|
64 |
huggingfacehub_api_token=huggingface_token
|
65 |
)
|
66 |
-
def generate_chunked_response(model, prompt, max_tokens=
|
67 |
full_response = ""
|
68 |
for i in range(max_chunks):
|
69 |
chunk = model(prompt + full_response, max_new_tokens=max_tokens)
|
@@ -95,10 +97,17 @@ def update_vectors(files, use_recursive_splitter):
|
|
95 |
def ask_question(question, temperature, top_p, repetition_penalty):
|
96 |
if not question:
|
97 |
return "Please enter a question."
|
|
|
|
|
|
|
98 |
embed = get_embeddings()
|
99 |
database = FAISS.load_local("faiss_database", embed, allow_dangerous_deserialization=True)
|
100 |
model = get_model(temperature, top_p, repetition_penalty)
|
101 |
-
|
|
|
|
|
|
|
|
|
102 |
def extract_db_to_excel():
|
103 |
embed = get_embeddings()
|
104 |
database = FAISS.load_local("faiss_database", embed, allow_dangerous_deserialization=True)
|
|
|
15 |
from langchain_core.documents import Document
|
16 |
huggingface_token = os.environ.get("HUGGINGFACE_TOKEN")
|
17 |
|
18 |
+
# Memory database to store question-answer pairs
|
19 |
+
memory_database = {}
|
20 |
def load_and_split_document_basic(file):
|
21 |
"""Loads and splits the document into pages."""
|
22 |
loader = PyPDFLoader(file.name)
|
|
|
61 |
"temperature": temperature,
|
62 |
"top_p": top_p,
|
63 |
"repetition_penalty": repetition_penalty,
|
64 |
+
"max_length": 512
|
65 |
},
|
66 |
huggingfacehub_api_token=huggingface_token
|
67 |
)
|
68 |
+
def generate_chunked_response(model, prompt, max_tokens=500, max_chunks=5):
|
69 |
full_response = ""
|
70 |
for i in range(max_chunks):
|
71 |
chunk = model(prompt + full_response, max_new_tokens=max_tokens)
|
|
|
97 |
def ask_question(question, temperature, top_p, repetition_penalty):
|
98 |
if not question:
|
99 |
return "Please enter a question."
|
100 |
+
# Check if the question exists in the memory database
|
101 |
+
if question in memory_database:
|
102 |
+
return memory_database[question]
|
103 |
embed = get_embeddings()
|
104 |
database = FAISS.load_local("faiss_database", embed, allow_dangerous_deserialization=True)
|
105 |
model = get_model(temperature, top_p, repetition_penalty)
|
106 |
+
# Generate response from document database
|
107 |
+
answer = response(database, model, question)
|
108 |
+
# Store the question and answer in the memory database
|
109 |
+
memory_database[question] = answer
|
110 |
+
return answer
|
111 |
def extract_db_to_excel():
|
112 |
embed = get_embeddings()
|
113 |
database = FAISS.load_local("faiss_database", embed, allow_dangerous_deserialization=True)
|