Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,34 +1,32 @@
|
|
| 1 |
import os
|
| 2 |
import streamlit as st
|
| 3 |
-
from langchain_community.vectorstores import FAISS
|
| 4 |
-
from langchain_huggingface import HuggingFaceEmbeddings
|
| 5 |
import subprocess
|
| 6 |
import openai
|
|
|
|
|
|
|
| 7 |
from openai import OpenAI
|
| 8 |
from langchain_openai import ChatOpenAI
|
| 9 |
-
from io import BytesIO
|
| 10 |
from typing import List, Dict
|
| 11 |
-
from dotenv import load_dotenv
|
| 12 |
# Load environment variables
|
| 13 |
OPENAI_API_KEY = os.getenv("OPENAI_API")
|
| 14 |
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)
|
|
@@ -62,7 +60,7 @@ Please provide a helpful response based on the insurance document content above.
|
|
| 62 |
messages.append(msg)
|
| 63 |
# Get response from Azure OpenAI
|
| 64 |
response = self.azure_client.chat.completions.create(
|
| 65 |
-
model="gpt-4o-mini
|
| 66 |
messages=messages,
|
| 67 |
max_tokens=1000,
|
| 68 |
temperature=0.7
|
|
@@ -75,25 +73,46 @@ 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 = []
|
| 86 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 87 |
# Clear conversation
|
| 88 |
-
if st.button("Xóa lịch sử
|
| 89 |
st.session_state.chatbot.conversation_history = []
|
| 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 +158,4 @@ def main():
|
|
| 139 |
""")
|
| 140 |
|
| 141 |
if __name__ == "__main__":
|
| 142 |
-
|
|
|
|
| 1 |
import os
|
| 2 |
import streamlit as st
|
|
|
|
|
|
|
| 3 |
import subprocess
|
| 4 |
import openai
|
| 5 |
+
from langchain_community.vectorstores import FAISS
|
| 6 |
+
from langchain.embeddings import HuggingFaceEmbeddings
|
| 7 |
from openai import OpenAI
|
| 8 |
from langchain_openai import ChatOpenAI
|
|
|
|
| 9 |
from typing import List, Dict
|
|
|
|
| 10 |
# Load environment variables
|
| 11 |
OPENAI_API_KEY = os.getenv("OPENAI_API")
|
| 12 |
TOKEN=os.getenv('HF_TOKEN')
|
| 13 |
subprocess.run(["huggingface-cli", "login", "--token", TOKEN, "--add-to-git-credential"])
|
| 14 |
st.sidebar.title("Welcome to MBAL Chatbot")
|
| 15 |
class PDFChatbot:
|
| 16 |
+
def __init__(self):
|
| 17 |
self.azure_client = openai.OpenAI()
|
|
|
|
| 18 |
self.conversation_history = []
|
| 19 |
+
self.pdf_content = ""
|
| 20 |
|
| 21 |
+
def get_relevant_context(self, user_question: str) -> List[str]:
|
| 22 |
"""Split text into smaller chunks for better processing."""
|
| 23 |
db = FAISS.load_local("mbal_faiss_db", embeddings=HuggingFaceEmbeddings(model_name='bkai-foundation-models/vietnamese-bi-encoder'), allow_dangerous_deserialization=True)
|
| 24 |
relevant_chunks = db.similarity_search(user_question, k=3)
|
| 25 |
relevant_chunks = [chunk.page_content for chunk in relevant_chunks]
|
| 26 |
return "\n\n".join(relevant_chunks)
|
| 27 |
+
def chat_with_pdf(self, user_question: str, pdf_content: str) -> str:
|
| 28 |
"""Generate response using Azure OpenAI based on PDF content and user question."""
|
| 29 |
+
try:
|
| 30 |
# Split PDF content into chunks
|
| 31 |
# Get relevant context for the question
|
| 32 |
relevant_context = self.get_relevant_context(user_question)
|
|
|
|
| 60 |
messages.append(msg)
|
| 61 |
# Get response from Azure OpenAI
|
| 62 |
response = self.azure_client.chat.completions.create(
|
| 63 |
+
model="gpt-4o-mini,
|
| 64 |
messages=messages,
|
| 65 |
max_tokens=1000,
|
| 66 |
temperature=0.7
|
|
|
|
| 73 |
except Exception as e:
|
| 74 |
return f"Error generating response: {str(e)}"
|
| 75 |
def main():
|
| 76 |
+
# st.set_page_config(page_title="Insurance PDF Chatbot", page_icon="🛡️", layout="wide")
|
| 77 |
+
st.title("🛡️ Insurance Policy Assistant")
|
| 78 |
+
st.markdown("Upload your insurance policy PDF and ask questions about your coverage, claims, deductibles, and more!")
|
| 79 |
+
# Initialize chatbot
|
| 80 |
+
if 'chatbot' not in st.session_state:
|
| 81 |
st.session_state.chatbot = PDFChatbot()
|
| 82 |
st.session_state.pdf_processed = False
|
| 83 |
st.session_state.chat_history = []
|
| 84 |
+
# Sidebar for PDF upload and settings
|
| 85 |
+
with st.sidebar:
|
| 86 |
+
st.header("📁 Upload Insurance Document")
|
| 87 |
+
uploaded_file = st.file_uploader("Choose a PDF file", type="pdf")
|
| 88 |
+
if uploaded_file is not None:
|
| 89 |
+
if st.button("Process PDF"):
|
| 90 |
+
with st.spinner("Processing your insurance document..."):
|
| 91 |
+
# Extract text from PDF
|
| 92 |
+
text_content = st.session_state.chatbot.extract_text_from_pdf(uploaded_file)
|
| 93 |
+
if text_content:
|
| 94 |
+
st.session_state.chatbot.pdf_content = text_content
|
| 95 |
+
st.session_state.pdf_processed = True
|
| 96 |
+
st.success("Insurance document processed successfully!")
|
| 97 |
+
# Show PDF summary
|
| 98 |
+
st.subheader("Document Preview")
|
| 99 |
+
st.text_area(
|
| 100 |
+
"First 500 characters:",
|
| 101 |
+
text_content[:500] + "..." if len(text_content) > 500 else text_content,
|
| 102 |
+
height=100
|
| 103 |
+
)
|
| 104 |
+
else:
|
| 105 |
+
st.error("Failed to process PDF")
|
| 106 |
# Clear conversation
|
| 107 |
+
if st.button("Xóa lịch sử"):
|
| 108 |
st.session_state.chatbot.conversation_history = []
|
| 109 |
st.session_state.chat_history = []
|
| 110 |
st.rerun()
|
| 111 |
# Main chat interface
|
| 112 |
+
if st.session_state.pdf_processed:
|
| 113 |
+
st.header("💬 Ask About Your Insurance Policy")
|
| 114 |
+
# Display chat history
|
| 115 |
+
for i, (question, answer) in enumerate(st.session_state.chat_history):
|
| 116 |
with st.container():
|
| 117 |
st.markdown(f"**You:** {question}")
|
| 118 |
st.markdown(f"**Insurance Assistant:** {answer}")
|
|
|
|
| 158 |
""")
|
| 159 |
|
| 160 |
if __name__ == "__main__":
|
| 161 |
+
main()
|