Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -15,20 +15,20 @@ TOKEN=os.getenv('HF_TOKEN')
|
|
| 15 |
subprocess.run(["huggingface-cli", "login", "--token", TOKEN, "--add-to-git-credential"])
|
| 16 |
st.sidebar.title("Welcome to MBAL Chatbot")
|
| 17 |
class PDFChatbot:
|
| 18 |
-
|
| 19 |
self.azure_client = openai.OpenAI()
|
| 20 |
# Store conversation history
|
| 21 |
self.conversation_history = []
|
| 22 |
|
| 23 |
-
|
| 24 |
"""Split text into smaller chunks for better processing."""
|
| 25 |
db = FAISS.load_local("mbal_faiss_db", embeddings=HuggingFaceEmbeddings(model_name='bkai-foundation-models/vietnamese-bi-encoder'), allow_dangerous_deserialization=True)
|
| 26 |
relevant_chunks = db.similarity_search(user_question, k=3)
|
| 27 |
relevant_chunks = [chunk.page_content for chunk in relevant_chunks]
|
| 28 |
return "\n\n".join(relevant_chunks)
|
| 29 |
-
|
| 30 |
"""Generate response using Azure OpenAI based on PDF content and user question."""
|
| 31 |
-
|
| 32 |
# Split PDF content into chunks
|
| 33 |
# Get relevant context for the question
|
| 34 |
relevant_context = self.get_relevant_context(user_question)
|
|
@@ -75,11 +75,11 @@ Please provide a helpful response based on the insurance document content above.
|
|
| 75 |
except Exception as e:
|
| 76 |
return f"Error generating response: {str(e)}"
|
| 77 |
def main():
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
st.session_state.chatbot = PDFChatbot()
|
| 84 |
st.session_state.pdf_processed = False
|
| 85 |
st.session_state.chat_history = []
|
|
@@ -90,10 +90,10 @@ def main():
|
|
| 90 |
st.session_state.chat_history = []
|
| 91 |
st.rerun()
|
| 92 |
# Main chat interface
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
with st.container():
|
| 98 |
st.markdown(f"**You:** {question}")
|
| 99 |
st.markdown(f"**Insurance Assistant:** {answer}")
|
|
@@ -139,4 +139,4 @@ def main():
|
|
| 139 |
""")
|
| 140 |
|
| 141 |
if __name__ == "__main__":
|
| 142 |
-
|
|
|
|
| 15 |
subprocess.run(["huggingface-cli", "login", "--token", TOKEN, "--add-to-git-credential"])
|
| 16 |
st.sidebar.title("Welcome to MBAL Chatbot")
|
| 17 |
class PDFChatbot:
|
| 18 |
+
def __init__(self):
|
| 19 |
self.azure_client = openai.OpenAI()
|
| 20 |
# Store conversation history
|
| 21 |
self.conversation_history = []
|
| 22 |
|
| 23 |
+
def get_relevant_context(self, user_question: str) -> List[str]:
|
| 24 |
"""Split text into smaller chunks for better processing."""
|
| 25 |
db = FAISS.load_local("mbal_faiss_db", embeddings=HuggingFaceEmbeddings(model_name='bkai-foundation-models/vietnamese-bi-encoder'), allow_dangerous_deserialization=True)
|
| 26 |
relevant_chunks = db.similarity_search(user_question, k=3)
|
| 27 |
relevant_chunks = [chunk.page_content for chunk in relevant_chunks]
|
| 28 |
return "\n\n".join(relevant_chunks)
|
| 29 |
+
def chat_with_pdf(self, user_question: str, pdf_content: str) -> str:
|
| 30 |
"""Generate response using Azure OpenAI based on PDF content and user question."""
|
| 31 |
+
try:
|
| 32 |
# Split PDF content into chunks
|
| 33 |
# Get relevant context for the question
|
| 34 |
relevant_context = self.get_relevant_context(user_question)
|
|
|
|
| 75 |
except Exception as e:
|
| 76 |
return f"Error generating response: {str(e)}"
|
| 77 |
def main():
|
| 78 |
+
# st.set_page_config(page_title="Insurance PDF Chatbot", page_icon="🛡️", layout="wide")
|
| 79 |
+
st.title("🛡️ Insurance Policy Assistant")
|
| 80 |
+
st.markdown("Upload your insurance policy PDF and ask questions about your coverage, claims, deductibles, and more!")
|
| 81 |
+
# Initialize chatbot
|
| 82 |
+
if 'chatbot' not in st.session_state:
|
| 83 |
st.session_state.chatbot = PDFChatbot()
|
| 84 |
st.session_state.pdf_processed = False
|
| 85 |
st.session_state.chat_history = []
|
|
|
|
| 90 |
st.session_state.chat_history = []
|
| 91 |
st.rerun()
|
| 92 |
# Main chat interface
|
| 93 |
+
if st.session_state.pdf_processed:
|
| 94 |
+
st.header("💬 Ask About Your Insurance Policy")
|
| 95 |
+
# Display chat history
|
| 96 |
+
for i, (question, answer) in enumerate(st.session_state.chat_history):
|
| 97 |
with st.container():
|
| 98 |
st.markdown(f"**You:** {question}")
|
| 99 |
st.markdown(f"**Insurance Assistant:** {answer}")
|
|
|
|
| 139 |
""")
|
| 140 |
|
| 141 |
if __name__ == "__main__":
|
| 142 |
+
main()
|