Spaces:
Sleeping
Sleeping
main logic
Browse files- app.py +60 -0
- requirements.txt +8 -0
app.py
ADDED
@@ -0,0 +1,60 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import getpass
|
3 |
+
from langchain_community.document_loaders import ConfluenceLoader
|
4 |
+
from langchain_google_genai import ChatGoogleGenerativeAI, GoogleGenerativeAIEmbeddings
|
5 |
+
from langchain.text_splitter import RecursiveCharacterTextSplitter
|
6 |
+
from langchain_community.vectorstores.faiss import FAISS
|
7 |
+
import google.generativeai as genai
|
8 |
+
from langchain.prompts import PromptTemplate
|
9 |
+
from langchain.chains.question_answering import load_qa_chain
|
10 |
+
import streamlit as st
|
11 |
+
|
12 |
+
confluence_api_key = os.environ["CONFLUENCE_API_KEY"]
|
13 |
+
|
14 |
+
if "GOOGLE_API_KEY" not in os.environ:
|
15 |
+
os.environ["GOOGLE_API_KEY"] = getpass.getpass("Please provide Google API Key")
|
16 |
+
|
17 |
+
google_api_key = os.environ['GOOGLE_API_KEY']
|
18 |
+
genai.configure(api_key=google_api_key)
|
19 |
+
|
20 |
+
|
21 |
+
loader = ConfluenceLoader(
|
22 |
+
url=os.environ["CONFLUENCE_URL"], space_key=os.environ['SPACE_KEY'], username=os.environ['USERNAME'], api_key=confluence_api_key
|
23 |
+
)
|
24 |
+
|
25 |
+
conf_docs = loader.load(page_id=os.environ["PAGE_ID"])
|
26 |
+
|
27 |
+
|
28 |
+
text_splitter = RecursiveCharacterTextSplitter(chunk_size=10000, chunk_overlap=1000)
|
29 |
+
chunks = text_splitter.split_text(conf_docs[-1].page_content)
|
30 |
+
|
31 |
+
embeddings = GoogleGenerativeAIEmbeddings(model='models/embedding-001')
|
32 |
+
llm = ChatGoogleGenerativeAI(model="gemini-1.5-flash-latest")
|
33 |
+
|
34 |
+
vector_store = FAISS.from_texts(chunks, embedding=embeddings)
|
35 |
+
vector_store.save_local("faiss_index")
|
36 |
+
|
37 |
+
def get_response(query):
|
38 |
+
prompt_template = """
|
39 |
+
Answer the question as detailed as possible from the provided context, make sure to provide all the details, if the answer is not in
|
40 |
+
provided context just say, "answer is not available in the context", don't provide the wrong answer\n\n
|
41 |
+
Context:\n {context}?\n
|
42 |
+
Question: \n{question}\n
|
43 |
+
|
44 |
+
Answer:
|
45 |
+
"""
|
46 |
+
prompt = PromptTemplate(template=prompt_template, input_variables=["context", "question"])
|
47 |
+
chain = load_qa_chain(llm, chain_type="stuff", prompt=prompt)
|
48 |
+
db = FAISS.load_local("faiss_index", embeddings, allow_dangerous_deserialization=True)
|
49 |
+
docs = db.similarity_search(query)
|
50 |
+
response = chain({"input_documents" : docs, "question": query}, return_only_outputs = True)
|
51 |
+
return response["output_text"]
|
52 |
+
|
53 |
+
|
54 |
+
if __name__ == '__main__':
|
55 |
+
st.set_page_config("Chat with Confluence Page")
|
56 |
+
st.header("Chat with Confluence Page using AI")
|
57 |
+
|
58 |
+
question = st.text_input("Ask questions related to login and registration")
|
59 |
+
answer = get_response(question)
|
60 |
+
st.write("Reply: ", answer)
|
requirements.txt
ADDED
@@ -0,0 +1,8 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
lxml
|
2 |
+
google-generativeai
|
3 |
+
langchain
|
4 |
+
langchain_community
|
5 |
+
langchain_google_genai
|
6 |
+
faiss-cpu
|
7 |
+
atlassian-python-api
|
8 |
+
streamlit
|