Spaces:
Sleeping
Sleeping
add files
Browse files- app.py +109 -0
- requirements.txt +0 -0
app.py
ADDED
|
@@ -0,0 +1,109 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
from langchain.document_loaders import PyPDFLoader
|
| 3 |
+
from langchain.vectorstores import FAISS
|
| 4 |
+
from langchain.embeddings import HuggingFaceEmbeddings
|
| 5 |
+
from langchain.chains import RetrievalQA
|
| 6 |
+
from langchain.llms import OpenAI
|
| 7 |
+
from langchain.llms import HuggingFaceHub
|
| 8 |
+
# from langchain import HuggingFaceHub
|
| 9 |
+
|
| 10 |
+
if "responses" not in st.session_state:
|
| 11 |
+
st.session_state.responses = []
|
| 12 |
+
|
| 13 |
+
if "questions" not in st.session_state:
|
| 14 |
+
st.session_state.questions = []
|
| 15 |
+
|
| 16 |
+
|
| 17 |
+
def app():
|
| 18 |
+
st.set_page_config(
|
| 19 |
+
page_title="Chat with AI",
|
| 20 |
+
page_icon="🤖",
|
| 21 |
+
layout="centered"
|
| 22 |
+
)
|
| 23 |
+
st.title("Chat with AI")
|
| 24 |
+
st.markdown(":violet[Get Huggingface API Read Token or Open AI API Key]")
|
| 25 |
+
st.markdown("#### Select an Option")
|
| 26 |
+
Option = st.selectbox(
|
| 27 |
+
label="Select the model",
|
| 28 |
+
options=(
|
| 29 |
+
"Select the model",
|
| 30 |
+
"HuggingFace(Uses Falcon 4b Model)",
|
| 31 |
+
"OpenAI"
|
| 32 |
+
),
|
| 33 |
+
label_visibility="collapsed"
|
| 34 |
+
)
|
| 35 |
+
if Option != "Select the model":
|
| 36 |
+
st.markdown("#### Enter your " + Option + " API key")
|
| 37 |
+
API = st.text_input(
|
| 38 |
+
"Enter your " + Option + " API key",
|
| 39 |
+
label_visibility="collapsed"
|
| 40 |
+
)
|
| 41 |
+
if API != "":
|
| 42 |
+
st.markdown("#### Upload a document")
|
| 43 |
+
doc = st.file_uploader("Upload a document", type=["pdf"], label_visibility="collapsed")
|
| 44 |
+
if doc is not None:
|
| 45 |
+
with open("doc.pdf", "wb") as f:
|
| 46 |
+
f.write(doc.getbuffer())
|
| 47 |
+
loader = PyPDFLoader("doc.pdf")
|
| 48 |
+
pages = loader.load_and_split()
|
| 49 |
+
embeddings = HuggingFaceEmbeddings(
|
| 50 |
+
model_name="all-MiniLM-L6-v2"
|
| 51 |
+
)
|
| 52 |
+
faiss_index = FAISS.from_documents(pages, embeddings)
|
| 53 |
+
llm = OpenAI(open_api_key=API) if Option == "OpenAI" else (
|
| 54 |
+
HuggingFaceHub(
|
| 55 |
+
repo_id="tiiuae/falcon-7b-instruct",
|
| 56 |
+
model_kwargs={
|
| 57 |
+
"temperature": 0.5,
|
| 58 |
+
"max_new_tokens": 500
|
| 59 |
+
},
|
| 60 |
+
huggingfacehub_api_token=API,
|
| 61 |
+
)
|
| 62 |
+
)
|
| 63 |
+
qa = RetrievalQA.from_chain_type(
|
| 64 |
+
llm=llm,
|
| 65 |
+
chain_type="stuff",
|
| 66 |
+
retriever=faiss_index.as_retriever(
|
| 67 |
+
search_type="mmr",
|
| 68 |
+
search_kwargs={'fetch_k': 10}),
|
| 69 |
+
return_source_documents=True
|
| 70 |
+
)
|
| 71 |
+
|
| 72 |
+
container = st.container()
|
| 73 |
+
st.write("Ask Your Question Here")
|
| 74 |
+
question = st.text_input(
|
| 75 |
+
"Ask your question here",
|
| 76 |
+
label_visibility="collapsed"
|
| 77 |
+
)
|
| 78 |
+
with container:
|
| 79 |
+
with st.chat_message("assistant"):
|
| 80 |
+
st.write("How can I help you?")
|
| 81 |
+
|
| 82 |
+
if question != "":
|
| 83 |
+
response = qa(question)
|
| 84 |
+
st.session_state.responses.insert(0, response)
|
| 85 |
+
st.session_state.questions.insert(0, question)
|
| 86 |
+
|
| 87 |
+
for i in range(len(st.session_state.responses)):
|
| 88 |
+
with st.chat_message("user"):
|
| 89 |
+
st.write(st.session_state.questions[i - 1])
|
| 90 |
+
|
| 91 |
+
with st.chat_message("assistant"):
|
| 92 |
+
with st.expander(
|
| 93 |
+
"Response (Click here to collapse)",
|
| 94 |
+
expanded=True
|
| 95 |
+
):
|
| 96 |
+
result = st.session_state.responses[i]
|
| 97 |
+
st.write(result['result'])
|
| 98 |
+
st.write("Source documents: "
|
| 99 |
+
"(Most relevant are first)")
|
| 100 |
+
for i in result['source_documents']:
|
| 101 |
+
with st.expander(
|
| 102 |
+
"Page: " + str(i.metadata['page'])
|
| 103 |
+
):
|
| 104 |
+
st.write(i.page_content)
|
| 105 |
+
st.divider()
|
| 106 |
+
|
| 107 |
+
|
| 108 |
+
if __name__ == "__main__":
|
| 109 |
+
app()
|
requirements.txt
ADDED
|
Binary file (3.3 kB). View file
|
|
|