Spaces:
Build error
Build error
Creating app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,65 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import tempfile
|
| 3 |
+
import os
|
| 4 |
+
import shutil
|
| 5 |
+
from langchain.embeddings.openai import OpenAIEmbeddings
|
| 6 |
+
from langchain.text_splitter import CharacterTextSplitter
|
| 7 |
+
from langchain.vectorstores import FAISS
|
| 8 |
+
from langchain_community.document_loaders import WebBaseLoader
|
| 9 |
+
from langchain.chains.question_answering import load_qa_chain
|
| 10 |
+
from langchain.llms import OpenAI
|
| 11 |
+
|
| 12 |
+
# Streamlit UI
|
| 13 |
+
st.title("🔍 Chat with Any Website")
|
| 14 |
+
|
| 15 |
+
# User inputs
|
| 16 |
+
openai_api_key = st.text_input("Enter OpenAI API Key", type="password")
|
| 17 |
+
website_url = st.text_input("Enter Website URL")
|
| 18 |
+
|
| 19 |
+
# Temporary directory to store FAISS index
|
| 20 |
+
temp_dir = tempfile.gettempdir()
|
| 21 |
+
faiss_db_path = os.path.join(temp_dir, "faiss_index_dir")
|
| 22 |
+
|
| 23 |
+
# Ensure FAISS directory exists
|
| 24 |
+
if not os.path.exists(faiss_db_path):
|
| 25 |
+
os.makedirs(faiss_db_path)
|
| 26 |
+
|
| 27 |
+
# Load embeddings if already created
|
| 28 |
+
if os.path.exists(os.path.join(faiss_db_path, "index.faiss")):
|
| 29 |
+
docsearch = FAISS.load_local(faiss_db_path, OpenAIEmbeddings(), allow_dangerous_deserialization=True)
|
| 30 |
+
else:
|
| 31 |
+
docsearch = None
|
| 32 |
+
|
| 33 |
+
if st.button("Build Embeddings") and openai_api_key and website_url:
|
| 34 |
+
st.info("Fetching website data...")
|
| 35 |
+
os.environ['OPENAI_API_KEY'] = openai_api_key
|
| 36 |
+
|
| 37 |
+
# Load website data
|
| 38 |
+
loader = WebBaseLoader(website_url)
|
| 39 |
+
raw_text = loader.load()
|
| 40 |
+
|
| 41 |
+
# Chunking the fetched text
|
| 42 |
+
text_splitter = CharacterTextSplitter(separator='\n', chunk_size=500, chunk_overlap=50)
|
| 43 |
+
docs = text_splitter.split_documents(raw_text)
|
| 44 |
+
|
| 45 |
+
# Creating embeddings
|
| 46 |
+
embeddings = OpenAIEmbeddings()
|
| 47 |
+
docsearch = FAISS.from_documents(docs, embeddings)
|
| 48 |
+
|
| 49 |
+
# Save FAISS index
|
| 50 |
+
if os.path.exists(faiss_db_path):
|
| 51 |
+
shutil.rmtree(faiss_db_path)
|
| 52 |
+
os.makedirs(faiss_db_path)
|
| 53 |
+
docsearch.save_local(faiss_db_path)
|
| 54 |
+
|
| 55 |
+
st.success("Embeddings built and saved successfully!")
|
| 56 |
+
|
| 57 |
+
# Chat section
|
| 58 |
+
if docsearch:
|
| 59 |
+
st.subheader("💬 Chat with Website")
|
| 60 |
+
user_query = st.text_input("Enter your question")
|
| 61 |
+
if st.button("Get Answer") and user_query:
|
| 62 |
+
chain = load_qa_chain(OpenAI(), chain_type="stuff")
|
| 63 |
+
docs = docsearch.similarity_search(user_query)
|
| 64 |
+
response = chain.run(input_documents=docs, question=user_query)
|
| 65 |
+
st.write("**Response:**", response)
|