Update app.py
Browse files
app.py
CHANGED
@@ -1,19 +1,59 @@
|
|
1 |
import gradio as gr
|
2 |
import utils
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
3 |
|
4 |
-
|
|
|
5 |
|
6 |
-
# Function to ingest a new file into the system
|
7 |
class VectorData():
|
8 |
def __init__(self):
|
9 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
10 |
self.ingested_files = []
|
11 |
-
self.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
12 |
|
13 |
def add_file(self,file):
|
14 |
if file is not None:
|
15 |
self.ingested_files.append(file.name.split('/')[-1])
|
16 |
self.retriever, self.vectorstore = utils.add_doc(file,self.vectorstore)
|
|
|
|
|
|
|
|
|
|
|
|
|
17 |
return [[name] for name in self.ingested_files]
|
18 |
|
19 |
def delete_file_by_name(self,file_name):
|
@@ -26,14 +66,15 @@ class VectorData():
|
|
26 |
self.ingested_files.clear()
|
27 |
self.retriever, self.vectorstore = utils.delete_all_doc(self.vectorstore)
|
28 |
return []
|
|
|
|
|
29 |
|
30 |
# Function to handle question answering
|
31 |
def answer_question(question):
|
32 |
if question.strip():
|
33 |
-
return f
|
34 |
return "Please enter a question."
|
35 |
|
36 |
-
data_obj = VectorData()
|
37 |
|
38 |
# Define the Gradio interface
|
39 |
with gr.Blocks() as rag_interface:
|
|
|
1 |
import gradio as gr
|
2 |
import utils
|
3 |
+
from langchain_mistralai import ChatMistralAI
|
4 |
+
from langchain_core.prompts import ChatPromptTemplate
|
5 |
+
from langchain_core.output_parsers import StrOutputParser
|
6 |
+
from langchain_community.vectorstores import Chroma
|
7 |
+
from langchain_huggingface import HuggingFaceEmbeddings
|
8 |
+
from langchain_core.runnables import RunnablePassthrough
|
9 |
+
import torch
|
10 |
|
11 |
+
import os
|
12 |
+
os.environ['MISTRAL_API_KEY'] = 'XuyOObDE7trMbpAeI7OXYr3dnmoWy3L0'
|
13 |
|
|
|
14 |
class VectorData():
|
15 |
def __init__(self):
|
16 |
+
embedding_model_name = 'nomic-ai/nomic-embed-text-v1.5'
|
17 |
+
|
18 |
+
model_kwargs = {'device':'cuda' if torch.cuda.is_available() else 'cpu',"trust_remote_code": True}
|
19 |
+
|
20 |
+
self.embeddings = HuggingFaceEmbeddings(
|
21 |
+
model_name=embedding_model_name,
|
22 |
+
model_kwargs=model_kwargs
|
23 |
+
)
|
24 |
+
|
25 |
+
self.vectorstore = Chroma(persist_directory="chroma_db", embedding_function=self.embeddings)
|
26 |
+
self.retriever = self.vectorstore.as_retriever()
|
27 |
self.ingested_files = []
|
28 |
+
self.prompt = ChatPromptTemplate.from_messages(
|
29 |
+
[
|
30 |
+
(
|
31 |
+
"system",
|
32 |
+
"""Answer the question based on the given context. Dont give any ans if context is not valid to question. Always give the source of context:
|
33 |
+
{context}
|
34 |
+
""",
|
35 |
+
),
|
36 |
+
("human", "{question}"),
|
37 |
+
]
|
38 |
+
)
|
39 |
+
self.llm = ChatMistralAI(model="mistral-large-latest")
|
40 |
+
self.rag_chain = (
|
41 |
+
{"context": self.retriever, "question": RunnablePassthrough()}
|
42 |
+
| self.prompt
|
43 |
+
| self.llm
|
44 |
+
| StrOutputParser()
|
45 |
+
)
|
46 |
|
47 |
def add_file(self,file):
|
48 |
if file is not None:
|
49 |
self.ingested_files.append(file.name.split('/')[-1])
|
50 |
self.retriever, self.vectorstore = utils.add_doc(file,self.vectorstore)
|
51 |
+
self.rag_chain = (
|
52 |
+
{"context": self.retriever, "question": RunnablePassthrough()}
|
53 |
+
| self.prompt
|
54 |
+
| self.llm
|
55 |
+
| StrOutputParser()
|
56 |
+
)
|
57 |
return [[name] for name in self.ingested_files]
|
58 |
|
59 |
def delete_file_by_name(self,file_name):
|
|
|
66 |
self.ingested_files.clear()
|
67 |
self.retriever, self.vectorstore = utils.delete_all_doc(self.vectorstore)
|
68 |
return []
|
69 |
+
|
70 |
+
data_obj = VectorData()
|
71 |
|
72 |
# Function to handle question answering
|
73 |
def answer_question(question):
|
74 |
if question.strip():
|
75 |
+
return f'{data_obj.rag_chain.invoke(question)}'
|
76 |
return "Please enter a question."
|
77 |
|
|
|
78 |
|
79 |
# Define the Gradio interface
|
80 |
with gr.Blocks() as rag_interface:
|