sanjeevbora commited on
Commit
00ea8c0
·
verified ·
1 Parent(s): d47f253

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +110 -103
app.py CHANGED
@@ -1,108 +1,115 @@
1
- import streamlit as st
2
- from llama_index.core import StorageContext, load_index_from_storage, VectorStoreIndex, SimpleDirectoryReader, ChatPromptTemplate
3
- from llama_index.llms.huggingface import HuggingFaceInferenceAPI
4
- from dotenv import load_dotenv
5
- from llama_index.embeddings.huggingface import HuggingFaceEmbedding
6
- from llama_index.core import Settings
7
  import os
8
- import base64
9
-
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
10
  # Load environment variables
11
- load_dotenv()
12
-
13
- # Configure the Llama index settings
14
- Settings.llm = HuggingFaceInferenceAPI(
15
- model_name="nltpt/Llama-3.2-3B-Instruct",
16
- tokenizer_name="nltpt/Llama-3.2-3B-Instruct",
17
- # model_name="google/gemma-1.1-7b-it",
18
- # tokenizer_name="google/gemma-1.1-7b-it",
19
- context_window=3000,
20
- token=os.getenv("HF_TOKEN"),
21
- max_new_tokens=512,
22
- generate_kwargs={"temperature": 0.1},
 
23
  )
24
- Settings.embed_model = HuggingFaceEmbedding(
25
- model_name="BAAI/bge-small-en-v1.5"
 
 
 
 
 
 
26
  )
27
-
28
- # Define the directory for persistent storage and data
29
- PERSIST_DIR = "./db"
30
- DATA_DIR = "data"
31
-
32
- # Ensure data directory exists
33
- os.makedirs(DATA_DIR, exist_ok=True)
34
- os.makedirs(PERSIST_DIR, exist_ok=True)
35
-
36
- def displayPDF(file):
37
- with open(file, "rb") as f:
38
- base64_pdf = base64.b64encode(f.read()).decode('utf-8')
39
- pdf_display = f'<iframe src="data:application/pdf;base64,{base64_pdf}" width="100%" height="600" type="application/pdf"></iframe>'
40
- st.markdown(pdf_display, unsafe_allow_html=True)
41
-
42
- def data_ingestion():
43
- documents = SimpleDirectoryReader(DATA_DIR).load_data()
44
- storage_context = StorageContext.from_defaults()
45
- index = VectorStoreIndex.from_documents(documents)
46
- index.storage_context.persist(persist_dir=PERSIST_DIR)
47
-
48
- def handle_query(query):
49
- storage_context = StorageContext.from_defaults(persist_dir=PERSIST_DIR)
50
- index = load_index_from_storage(storage_context)
51
- chat_text_qa_msgs = [
52
- (
53
- "user",
54
- """You are a Q&A assistant. Your main goal is to provide answers as accurately as possible, based on the context of the document provided. If the question does not match the context or is outside the scope of the document, advise the user to ask questions that are relevant to the document.
55
- Context:
56
- {context_str}
57
- Question:
58
- {query_str}
59
- """
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
60
  )
61
- ]
62
-
63
- text_qa_template = ChatPromptTemplate.from_messages(chat_text_qa_msgs)
64
-
65
- query_engine = index.as_query_engine(text_qa_template=text_qa_template)
66
- answer = query_engine.query(query)
67
-
68
- if hasattr(answer, 'response'):
69
- return answer.response
70
- elif isinstance(answer, dict) and 'response' in answer:
71
- return answer['response']
72
- else:
73
- return "Sorry, I couldn't find an answer."
74
-
75
-
76
- # Streamlit app initialization
77
- st.title("(PDF) Information and Inference🗞️")
78
- st.markdown("Retrieval-Augmented Generation")
79
- st.markdown("Start chat ...🚀")
80
-
81
- if 'messages' not in st.session_state:
82
- st.session_state.messages = [{'role': 'assistant', "content": 'Hello! Upload PDF files and ask me anything about their content.'}]
83
-
84
- with st.sidebar:
85
- st.title("Menu:")
86
- uploaded_files = st.file_uploader("Upload your PDF Files and Click on the Submit & Process Button", type="pdf", accept_multiple_files=True)
87
-
88
- if st.button("Submit & Process"):
89
- if uploaded_files:
90
- with st.spinner("Processing..."):
91
- for uploaded_file in uploaded_files:
92
- filepath = os.path.join(DATA_DIR, uploaded_file.name)
93
- with open(filepath, "wb") as f:
94
- f.write(uploaded_file.getbuffer())
95
- data_ingestion() # Process PDFs after they are uploaded
96
- st.success("Done")
97
- else:
98
- st.warning("Please upload at least one PDF file.")
99
-
100
- user_prompt = st.chat_input("Ask me anything about the content of the PDF(s):")
101
- if user_prompt:
102
- st.session_state.messages.append({'role': 'user', "content": user_prompt})
103
- response = handle_query(user_prompt)
104
- st.session_state.messages.append({'role': 'assistant', "content": response})
105
-
106
- for message in st.session_state.messages:
107
- with st.chat_message(message['role']):
108
- st.write(message['content'])
 
1
+ import gradio as gr
 
 
 
 
 
2
  import os
3
+ from langchain_community.embeddings import HuggingFaceEmbeddings
4
+ from langchain_community.vectorstores import Chroma
5
+ from langchain.text_splitter import RecursiveCharacterTextSplitter
6
+ from langchain_community.document_loaders import TextLoader, PyPDFLoader
7
+ from langchain.chains import RetrievalQA
8
+ from langchain_community.llms import HuggingFaceHub
9
+ import tempfile
10
+ import shutil
11
+ from langchain.prompts import PromptTemplate
12
+
13
+ # Define a proper prompt template
14
+ prompt_template = """Use the following pieces of context to answer the question at the end. If you don't know the answer, just say that you don't know, don't try to make up an answer.
15
+ {context}
16
+ Question: {question}
17
+ Answer:"""
18
+ PROMPT = PromptTemplate(
19
+ template=prompt_template, input_variables=["context", "question"]
20
+ )
21
+
22
  # Load environment variables
23
+ TOKEN = os.getenv("HF_TOKEN")
24
+ os.environ["HUGGINGFACEHUB_API_TOKEN"] = TOKEN
25
+
26
+ # Initialize LangChain components
27
+ embeddings = HuggingFaceEmbeddings(model_name="sentence-transformers/all-MiniLM-L6-v2")
28
+
29
+ # Create vector store
30
+ vectorstore = Chroma(persist_directory="./chroma_db", embedding_function=embeddings)
31
+
32
+ # Initialize LLM
33
+ llm = HuggingFaceHub(
34
+ repo_id="meta-llama/Meta-Llama-3.1-405B-Instruct-FP8",
35
+ model_kwargs={"temperature": 0.7, "max_length": 512}
36
  )
37
+
38
+ # Create RetrievalQA chain
39
+ qa_chain = RetrievalQA.from_chain_type(
40
+ llm=llm,
41
+ chain_type="stuff",
42
+ retriever=vectorstore.as_retriever(),
43
+ return_source_documents=True,
44
+ chain_type_kwargs={"prompt": PROMPT}
45
  )
46
+
47
+ def process_uploaded_file(file):
48
+ if file is None:
49
+ return "No file uploaded."
50
+ try:
51
+ # Create a temporary directory
52
+ with tempfile.TemporaryDirectory() as temp_dir:
53
+ # Create a path for the temporary file
54
+ temp_file_path = os.path.join(temp_dir, os.path.basename(file.name))
55
+ # Save the uploaded file to the temporary path
56
+ with open(temp_file_path, 'wb') as temp_file:
57
+ temp_file.write(file.read())
58
+ # Determine file type and load accordingly
59
+ if file.name.endswith('.pdf'):
60
+ loader = PyPDFLoader(temp_file_path)
61
+ else:
62
+ loader = TextLoader(temp_file_path)
63
+
64
+ documents = loader.load()
65
+ text_splitter = RecursiveCharacterTextSplitter(chunk_size=1000, chunk_overlap=200)
66
+ texts = text_splitter.split_documents(documents)
67
+
68
+ # Update the vector store with new documents
69
+ vectorstore.add_documents(texts)
70
+ vectorstore.persist()
71
+
72
+ return f"File processed and added to the knowledge base. {len(texts)} chunks created."
73
+ except Exception as e:
74
+ return f"An error occurred while processing the file: {str(e)}"
75
+
76
+ def respond(message, history, system_message, max_tokens, temperature, top_p):
77
+ full_prompt = f"{system_message}\n\nHuman: {message}"
78
+ # Use the RetrievalQA chain to get the answer
79
+ result = qa_chain({"query": full_prompt})
80
+ answer = result['result']
81
+ # Return only the answer
82
+ yield answer
83
+
84
+ with gr.Blocks() as demo:
85
+ gr.Markdown("# RAG Chatbot with Content Upload")
86
+ with gr.Row():
87
+ with gr.Column(scale=3):
88
+ chatbot = gr.Chatbot()
89
+ msg = gr.Textbox()
90
+ clear = gr.Button("Clear")
91
+ with gr.Column(scale=1):
92
+ file_upload = gr.File(label="Upload Content for RAG (TXT or PDF)")
93
+ upload_button = gr.Button("Process Uploaded File")
94
+ system_message = gr.Textbox(value="You are a friendly Chatbot.", label="System message")
95
+ max_tokens = gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens")
96
+ temperature = gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature")
97
+ top_p = gr.Slider(minimum=0.1, maximum=1.0, value=0.95, step=0.05, label="Top-p (nucleus sampling)")
98
+
99
+ def user(user_message, history):
100
+ return "", history + [[user_message, None]]
101
+
102
+ def bot(history, system_message, max_tokens, temperature, top_p):
103
+ user_message = history[-1][0]
104
+ bot_message = next(respond(user_message, history[:-1], system_message, max_tokens, temperature, top_p))
105
+ history[-1][1] = bot_message
106
+ return history
107
+
108
+ msg.submit(user, [msg, chatbot], [msg, chatbot], queue=False).then(
109
+ bot, [chatbot, system_message, max_tokens, temperature, top_p], chatbot
110
  )
111
+ clear.click(lambda: None, None, chatbot, queue=False)
112
+ upload_button.click(process_uploaded_file, inputs=[file_upload], outputs=[gr.Textbox()])
113
+
114
+ if __name__ == "__main__":
115
+ demo.launch()