Update app.py
Browse files
app.py
CHANGED
@@ -12,7 +12,6 @@ from langchain_core.output_parsers import StrOutputParser
|
|
12 |
from langchain_core.runnables import RunnablePassthrough
|
13 |
|
14 |
|
15 |
-
|
16 |
# Initialize the FAISS vector store
|
17 |
vector_store = None
|
18 |
|
@@ -39,47 +38,87 @@ Question: {question}
|
|
39 |
Answer:
|
40 |
"""
|
41 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
42 |
|
43 |
-
|
44 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
45 |
|
|
|
|
|
46 |
|
|
|
|
|
47 |
|
48 |
-
|
49 |
-
|
|
|
|
|
|
|
50 |
|
51 |
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
|
56 |
-
|
57 |
-
|
58 |
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
|
72 |
-
|
|
|
73 |
|
74 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
75 |
|
76 |
-
# additional_inputs = [
|
77 |
-
# gr.Dropdown(choices=["llama-3.1-70b-versatile", "llama-3.1-8b-instant", "llama3-70b-8192", "llama3-8b-8192", "mixtral-8x7b-32768", "gemma2-9b-it", "gemma-7b-it"], value="llama-3.1-70b-versatile", label="Model"),
|
78 |
-
# gr.Slider(minimum=0.0, maximum=1.0, step=0.01, value=0.5, label="Temperature", info="Controls diversity of the generated text. Lower is more deterministic, higher is more creative."),
|
79 |
-
# gr.Slider(minimum=1, maximum=8000, step=1, value=8000, label="Max Tokens", info="The maximum number of tokens that the model can process in a single response.<br>Maximums: 8k for gemma 7b it, gemma2 9b it, llama 7b & 70b, 32k for mixtral 8x7b, 132k for llama 3.1."),
|
80 |
-
# gr.Slider(minimum=0.0, maximum=1.0, step=0.01, value=0.5, label="Top P", info="A method of text generation where a model will only consider the most probable next tokens that make up the probability p."),
|
81 |
-
# gr.Number(precision=0, value=0, label="Seed", info="A starting point to initiate generation, use 0 for random")
|
82 |
-
# ]
|
83 |
|
84 |
with gr.Blocks(theme="Nymbo/Alyx_Theme") as demo:
|
85 |
# Set the title and description for the app.
|
|
|
12 |
from langchain_core.runnables import RunnablePassthrough
|
13 |
|
14 |
|
|
|
15 |
# Initialize the FAISS vector store
|
16 |
vector_store = None
|
17 |
|
|
|
38 |
Answer:
|
39 |
"""
|
40 |
|
41 |
+
# Function to handle PDF upload and indexing
|
42 |
+
def index_pdf(pdf):
|
43 |
+
global vector_store
|
44 |
+
|
45 |
+
# Load the PDF
|
46 |
+
loader = PyPDFLoader(pdf.name)
|
47 |
+
documents = loader.load()
|
48 |
|
49 |
+
# Split the documents into chunks
|
50 |
+
text_splitter = RecursiveCharacterTextSplitter(chunk_size=1000, chunk_overlap=200)
|
51 |
+
texts = text_splitter.split_documents(documents)
|
52 |
+
|
53 |
+
# Embed the chunks
|
54 |
+
embeddings = HuggingFaceEmbeddings(model_name="bert-base-uncased", encode_kwargs={"normalize_embeddings": True})
|
55 |
+
|
56 |
+
# Store the embeddings in the vector store
|
57 |
+
vector_store = FAISS.from_documents(texts, embeddings)
|
58 |
+
|
59 |
+
return "PDF indexed successfully!"
|
60 |
+
|
61 |
+
def load_sample_pdf():
|
62 |
+
global vector_store
|
63 |
+
|
64 |
+
# Load the PDF
|
65 |
+
loader = PyPDFLoader(sample_filename)
|
66 |
+
documents = loader.load()
|
67 |
+
|
68 |
+
# Split the documents into chunks
|
69 |
+
text_splitter = RecursiveCharacterTextSplitter(chunk_size=1000, chunk_overlap=200)
|
70 |
+
texts = text_splitter.split_documents(documents)
|
71 |
|
72 |
+
# Embed the chunks
|
73 |
+
embeddings = HuggingFaceEmbeddings(model_name="bert-base-uncased", encode_kwargs={"normalize_embeddings": True})
|
74 |
|
75 |
+
# Store the embeddings in the vector store
|
76 |
+
vector_store = FAISS.from_documents(texts, embeddings)
|
77 |
|
78 |
+
return "Sample PDF indexed successfully!"
|
79 |
+
|
80 |
+
|
81 |
+
def format_docs(docs):
|
82 |
+
return "\n\n".join(doc.page_content for doc in docs)
|
83 |
|
84 |
|
85 |
+
def generate_response(query, history, model, temperature, max_tokens, top_p, seed):
|
86 |
+
if vector_store is None:
|
87 |
+
return "Please upload and index a PDF at the Indexing tab."
|
88 |
|
89 |
+
if seed == 0:
|
90 |
+
seed = random.randint(1, 100000)
|
91 |
|
92 |
+
retriever = vector_store.as_retriever(search_type="similarity", search_kwargs={"k": 16})
|
93 |
+
llm = ChatGroq(groq_api_key=os.environ.get("GROQ_API_KEY"), model=model)
|
94 |
+
custom_rag_prompt = PromptTemplate.from_template(template)
|
95 |
|
96 |
+
rag_chain = (
|
97 |
+
{"context": retriever | format_docs, "question": RunnablePassthrough()}
|
98 |
+
| custom_rag_prompt
|
99 |
+
| llm
|
100 |
+
| StrOutputParser()
|
101 |
+
)
|
102 |
+
|
103 |
+
response = rag_chain.invoke(query)
|
104 |
|
105 |
+
return response
|
106 |
+
|
107 |
|
108 |
|
109 |
+
additional_inputs = [
|
110 |
+
gr.Dropdown(choices=["llama-3.1-70b-versatile", "llama-3.1-8b-instant", "llama3-70b-8192", "llama3-8b-8192", "mixtral-8x7b-32768", "gemma2-9b-it", "gemma-7b-it"], value="llama-3.1-70b-versatile", label="Model"),
|
111 |
+
gr.Slider(minimum=0.0, maximum=1.0, step=0.01, value=0.5, label="Temperature", info="Controls diversity of the generated text. Lower is more deterministic, higher is more creative."),
|
112 |
+
gr.Slider(minimum=1, maximum=8000, step=1, value=8000, label="Max Tokens", info="The maximum number of tokens that the model can process in a single response.<br>Maximums: 8k for gemma 7b it, gemma2 9b it, llama 7b & 70b, 32k for mixtral 8x7b, 132k for llama 3.1."),
|
113 |
+
gr.Slider(minimum=0.0, maximum=1.0, step=0.01, value=0.5, label="Top P", info="A method of text generation where a model will only consider the most probable next tokens that make up the probability p."),
|
114 |
+
gr.Number(precision=0, value=0, label="Seed", info="A starting point to initiate generation, use 0 for random")
|
115 |
+
]
|
116 |
+
|
117 |
+
|
118 |
+
def greet(name):
|
119 |
+
return f"Hello, {name}!"
|
120 |
+
|
121 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
122 |
|
123 |
with gr.Blocks(theme="Nymbo/Alyx_Theme") as demo:
|
124 |
# Set the title and description for the app.
|