Files changed (1) hide show
  1. app.py +19 -12
app.py CHANGED
@@ -1,20 +1,23 @@
1
  import streamlit as st
2
- # from langchain.document_loaders import PyPDFLoader
3
  from PyPDF2 import PdfReader
4
  from langchain.text_splitter import RecursiveCharacterTextSplitter
5
- from langchain_core.prompts import ChatPromptTemplate
6
- from langchain_community.embeddings.spacy_embeddings import SpacyEmbeddings
7
- from langchain_community.vectorstores import FAISS
8
- from langchain.chat_models import ChatOllama
9
- from langchain.embeddings import HuggingFaceEmbeddings
10
- from langchain_community.chat_models import ChatOllama
11
  from langchain.prompts import ChatPromptTemplate
 
 
12
  from langchain.schema.runnable import RunnablePassthrough
13
  from langchain.schema.output_parser import StrOutputParser
 
 
14
  import os
15
 
 
16
  embeddings = HuggingFaceEmbeddings(model_name="sentence-transformers/all-mpnet-base-v2")
17
 
 
 
 
 
 
18
  def pdf_read(pdf_doc):
19
  st.write("Loading PDF")
20
  text = ""
@@ -38,7 +41,6 @@ def vector_store(text_chunks):
38
 
39
  def get_conversational_chain(retriever, user_question):
40
  with st.spinner("Calling LLM"):
41
- llm = ChatOllama(model_name="llama2", temperature=0)
42
  template = """You are an assistant for question-answering tasks for Retrieval Augmented Generation system for the financial reports such as 10Q and 10K.
43
  Use the following pieces of retrieved context to answer the question.
44
  If you don't know the answer, just say that you don't know.
@@ -46,17 +48,22 @@ def get_conversational_chain(retriever, user_question):
46
  Question: {question}
47
  Context: {context}
48
  Answer:
49
- """
50
  prompt = ChatPromptTemplate.from_template(template)
 
51
  # Setup RAG pipeline
52
  conversation_chain = (
53
- {"context": retriever, "question": RunnablePassthrough()}
54
  | prompt
55
- | llm
56
  | StrOutputParser()
57
  )
 
58
  st.write("Creating chain")
59
- answer = conversation_chain.invoke(user_question)
 
 
 
60
  st.write(answer)
61
  st.write("Answer: ", answer)
62
  return answer
 
1
  import streamlit as st
 
2
  from PyPDF2 import PdfReader
3
  from langchain.text_splitter import RecursiveCharacterTextSplitter
 
 
 
 
 
 
4
  from langchain.prompts import ChatPromptTemplate
5
+ from langchain.embeddings import HuggingFaceEmbeddings
6
+ from langchain.vectorstores import FAISS
7
  from langchain.schema.runnable import RunnablePassthrough
8
  from langchain.schema.output_parser import StrOutputParser
9
+ from transformers import AutoModelForCausalLM, AutoTokenizer
10
+ import torch
11
  import os
12
 
13
+ # Initialize embeddings model
14
  embeddings = HuggingFaceEmbeddings(model_name="sentence-transformers/all-mpnet-base-v2")
15
 
16
+ # Load the LLaMA model and tokenizer
17
+ model_name = "meta-llama/Llama-2-7b-chat-hf"
18
+ tokenizer = AutoTokenizer.from_pretrained(model_name)
19
+ model = AutoModelForCausalLM.from_pretrained(model_name)
20
+
21
  def pdf_read(pdf_doc):
22
  st.write("Loading PDF")
23
  text = ""
 
41
 
42
  def get_conversational_chain(retriever, user_question):
43
  with st.spinner("Calling LLM"):
 
44
  template = """You are an assistant for question-answering tasks for Retrieval Augmented Generation system for the financial reports such as 10Q and 10K.
45
  Use the following pieces of retrieved context to answer the question.
46
  If you don't know the answer, just say that you don't know.
 
48
  Question: {question}
49
  Context: {context}
50
  Answer:
51
+ """
52
  prompt = ChatPromptTemplate.from_template(template)
53
+
54
  # Setup RAG pipeline
55
  conversation_chain = (
56
+ {"context": retriever, "question": RunnablePassthrough()}
57
  | prompt
58
+ | model
59
  | StrOutputParser()
60
  )
61
+
62
  st.write("Creating chain")
63
+ inputs = tokenizer(user_question, return_tensors="pt")
64
+ outputs = model.generate(**inputs)
65
+ answer = tokenizer.decode(outputs[0], skip_special_tokens=True)
66
+
67
  st.write(answer)
68
  st.write("Answer: ", answer)
69
  return answer