Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -110,9 +110,9 @@
|
|
| 110 |
# with st.chat_message("Human"):
|
| 111 |
# st.write(message.content)
|
| 112 |
|
| 113 |
-
|
| 114 |
import os
|
| 115 |
import streamlit as st
|
|
|
|
| 116 |
from langchain_core.messages import AIMessage, HumanMessage
|
| 117 |
from langchain_community.document_loaders import WebBaseLoader
|
| 118 |
from langchain.text_splitter import RecursiveCharacterTextSplitter
|
|
@@ -122,10 +122,10 @@ from langchain_community.llms import HuggingFaceHub
|
|
| 122 |
from langchain_core.prompts import ChatPromptTemplate, MessagesPlaceholder
|
| 123 |
from langchain.chains import create_history_aware_retriever, create_retrieval_chain
|
| 124 |
from langchain.chains.combine_documents import create_stuff_documents_chain
|
| 125 |
-
from dotenv import load_dotenv
|
| 126 |
|
| 127 |
-
# Load Hugging Face
|
| 128 |
load_dotenv()
|
|
|
|
| 129 |
|
| 130 |
# Function to get vectorstore from a website
|
| 131 |
def get_vectorstore_from_url(url):
|
|
@@ -148,6 +148,7 @@ def get_vectorstore_from_url(url):
|
|
| 148 |
def get_context_retriever_chain(vector_store):
|
| 149 |
llm = HuggingFaceHub(
|
| 150 |
repo_id="google/flan-t5-base",
|
|
|
|
| 151 |
model_kwargs={"temperature": 0.5, "max_length": 512}
|
| 152 |
)
|
| 153 |
|
|
@@ -165,6 +166,7 @@ def get_context_retriever_chain(vector_store):
|
|
| 165 |
def get_conversational_rag_chain(retriever_chain):
|
| 166 |
llm = HuggingFaceHub(
|
| 167 |
repo_id="google/flan-t5-base",
|
|
|
|
| 168 |
model_kwargs={"temperature": 0.5, "max_length": 512}
|
| 169 |
)
|
| 170 |
|
|
@@ -198,7 +200,6 @@ with st.sidebar:
|
|
| 198 |
st.header("🔧 Settings")
|
| 199 |
website_url = st.text_input("🌐 Website URL")
|
| 200 |
|
| 201 |
-
# Handle input and chatbot logic
|
| 202 |
if not website_url:
|
| 203 |
st.info("Please enter a website URL in the sidebar.")
|
| 204 |
else:
|
|
@@ -214,7 +215,7 @@ else:
|
|
| 214 |
st.session_state.chat_history.append(HumanMessage(content=user_input))
|
| 215 |
st.session_state.chat_history.append(AIMessage(content=response))
|
| 216 |
|
| 217 |
-
# Display messages
|
| 218 |
for msg in st.session_state.chat_history:
|
| 219 |
with st.chat_message("AI" if isinstance(msg, AIMessage) else "Human"):
|
| 220 |
st.write(msg.content)
|
|
|
|
| 110 |
# with st.chat_message("Human"):
|
| 111 |
# st.write(message.content)
|
| 112 |
|
|
|
|
| 113 |
import os
|
| 114 |
import streamlit as st
|
| 115 |
+
from dotenv import load_dotenv
|
| 116 |
from langchain_core.messages import AIMessage, HumanMessage
|
| 117 |
from langchain_community.document_loaders import WebBaseLoader
|
| 118 |
from langchain.text_splitter import RecursiveCharacterTextSplitter
|
|
|
|
| 122 |
from langchain_core.prompts import ChatPromptTemplate, MessagesPlaceholder
|
| 123 |
from langchain.chains import create_history_aware_retriever, create_retrieval_chain
|
| 124 |
from langchain.chains.combine_documents import create_stuff_documents_chain
|
|
|
|
| 125 |
|
| 126 |
+
# Load environment variable (works locally and on Hugging Face Spaces)
|
| 127 |
load_dotenv()
|
| 128 |
+
HF_TOKEN = os.getenv("HUGGINGFACEHUB_API_TOKEN", "").strip()
|
| 129 |
|
| 130 |
# Function to get vectorstore from a website
|
| 131 |
def get_vectorstore_from_url(url):
|
|
|
|
| 148 |
def get_context_retriever_chain(vector_store):
|
| 149 |
llm = HuggingFaceHub(
|
| 150 |
repo_id="google/flan-t5-base",
|
| 151 |
+
huggingfacehub_api_token=HF_TOKEN,
|
| 152 |
model_kwargs={"temperature": 0.5, "max_length": 512}
|
| 153 |
)
|
| 154 |
|
|
|
|
| 166 |
def get_conversational_rag_chain(retriever_chain):
|
| 167 |
llm = HuggingFaceHub(
|
| 168 |
repo_id="google/flan-t5-base",
|
| 169 |
+
huggingfacehub_api_token=HF_TOKEN,
|
| 170 |
model_kwargs={"temperature": 0.5, "max_length": 512}
|
| 171 |
)
|
| 172 |
|
|
|
|
| 200 |
st.header("🔧 Settings")
|
| 201 |
website_url = st.text_input("🌐 Website URL")
|
| 202 |
|
|
|
|
| 203 |
if not website_url:
|
| 204 |
st.info("Please enter a website URL in the sidebar.")
|
| 205 |
else:
|
|
|
|
| 215 |
st.session_state.chat_history.append(HumanMessage(content=user_input))
|
| 216 |
st.session_state.chat_history.append(AIMessage(content=response))
|
| 217 |
|
| 218 |
+
# Display chat messages
|
| 219 |
for msg in st.session_state.chat_history:
|
| 220 |
with st.chat_message("AI" if isinstance(msg, AIMessage) else "Human"):
|
| 221 |
st.write(msg.content)
|