AreesaAshfaq commited on
Commit
b5124d9
·
verified ·
1 Parent(s): cd87b5d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +63 -63
app.py CHANGED
@@ -53,67 +53,67 @@ else:
53
 
54
  # Load, chunk, and index the contents of the blog
55
  def load_data(url):
56
- try:
57
- loader = WebBaseLoader(
58
- web_paths=(url,),
59
- bs_kwargs=dict(
60
- parse_only=bs4.SoupStrainer(
61
- class_=("post-content", "post-title", "post-header")
62
- )
63
- ),
64
- )
65
- docs = loader.load()
66
- if not docs:
67
- st.error("No documents were loaded. Please check the URL and try again.")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
68
  return None
69
-
70
- st.write(f"Loaded {len(docs)} documents.")
71
-
72
- text_splitter = RecursiveCharacterTextSplitter(chunk_size=1000, chunk_overlap=200)
73
- splits = text_splitter.split_documents(docs)
74
- if not splits:
75
- st.error("No document splits were created. Please check the document content.")
76
- return None
77
-
78
- st.write(f"Created {len(splits)} document splits.")
79
-
80
- vectorstore = Chroma.from_documents(documents=splits, embedding=embedding_model)
81
- if vectorstore is None:
82
- st.error("Failed to create the vectorstore.")
83
- return None
84
-
85
- return vectorstore
86
- except Exception as e:
87
- st.error(f"An error occurred while loading the blog: {e}")
88
- return None
89
-
90
-
91
- # Load the data if a URL is provided
92
- if blog_url:
93
- vectorstore = load_data(blog_url)
94
- if vectorstore:
95
- # Streamlit UI for question input
96
- question = st.text_input("Enter your question:")
97
-
98
- if question:
99
- retriever = vectorstore.as_retriever()
100
- prompt = hub.pull("rlm/rag-prompt", api_key=api_key_langchain)
101
-
102
- def format_docs(docs):
103
- return "\n\n".join(doc.page_content for doc in docs)
104
-
105
- rag_chain = (
106
- {"context": retriever | format_docs, "question": RunnablePassthrough()}
107
- | prompt
108
- | llm
109
- | StrOutputParser()
110
- )
111
-
112
- # Example invocation
113
- try:
114
- result = rag_chain.invoke(question)
115
- st.write("Answer:", result)
116
- except Exception as e:
117
- st.error(f"An error occurred while generating the answer: {e}")
118
- else:
119
- st.write("Failed to load the blog content. Please check the URL and try again.")
 
53
 
54
  # Load, chunk, and index the contents of the blog
55
  def load_data(url):
56
+ try:
57
+ loader = WebBaseLoader(
58
+ web_paths=(url,),
59
+ bs_kwargs=dict(
60
+ parse_only=bs4.SoupStrainer(
61
+ class_=("post-content", "post-title", "post-header")
62
+ )
63
+ ),
64
+ )
65
+ docs = loader.load()
66
+ if not docs:
67
+ st.error("No documents were loaded. Please check the URL and try again.")
68
+ return None
69
+
70
+ st.write(f"Loaded {len(docs)} documents.")
71
+
72
+ text_splitter = RecursiveCharacterTextSplitter(chunk_size=1000, chunk_overlap=200)
73
+ splits = text_splitter.split_documents(docs)
74
+ if not splits:
75
+ st.error("No document splits were created. Please check the document content.")
76
+ return None
77
+
78
+ st.write(f"Created {len(splits)} document splits.")
79
+
80
+ vectorstore = Chroma.from_documents(documents=splits, embedding=embedding_model)
81
+ if vectorstore is None:
82
+ st.error("Failed to create the vectorstore.")
83
+ return None
84
+
85
+ return vectorstore
86
+ except Exception as e:
87
+ st.error(f"An error occurred while loading the blog: {e}")
88
  return None
89
+
90
+
91
+ # Load the data if a URL is provided
92
+ if blog_url:
93
+ vectorstore = load_data(blog_url)
94
+ if vectorstore:
95
+ # Streamlit UI for question input
96
+ question = st.text_input("Enter your question:")
97
+
98
+ if question:
99
+ retriever = vectorstore.as_retriever()
100
+ prompt = hub.pull("rlm/rag-prompt", api_key=api_key_langchain)
101
+
102
+ def format_docs(docs):
103
+ return "\n\n".join(doc.page_content for doc in docs)
104
+
105
+ rag_chain = (
106
+ {"context": retriever | format_docs, "question": RunnablePassthrough()}
107
+ | prompt
108
+ | llm
109
+ | StrOutputParser()
110
+ )
111
+
112
+ # Example invocation
113
+ try:
114
+ result = rag_chain.invoke(question)
115
+ st.write("Answer:", result)
116
+ except Exception as e:
117
+ st.error(f"An error occurred while generating the answer: {e}")
118
+ else:
119
+ st.write("Failed to load the blog content. Please check the URL and try again.")