rag_karim / app.py
karimbkh's picture
Create app.py
bb7b36a verified
Raw
History Blame Contribute Delete
2.74 kB
import os
import streamlit as st
from model import ChatModel
import rag_util
st.title("RAG By Karim Ben Khaled")
@st.cache_resource
def load_model():
model = ChatModel(model_id="microsoft/Phi-3-mini-4k-instruct", device="cpu")
return model
@st.cache_resource
def load_encoder():
encoder = rag_util.Encoder(
model_name="sentence-transformers/all-MiniLM-L12-v2", device="cpu"
)
return encoder
model = load_model() # load our models once and then cache it
encoder = load_encoder()
def save_file(uploaded_file):
"""helper function to save documents to disk"""
file_path = uploaded_file.name
with open(file_path, "wb") as f:
f.write(uploaded_file.getbuffer())
return file_path
with st.sidebar:
max_new_tokens = st.number_input("max_new_tokens", 128, 4096, 512)
k = st.number_input("k", 1, 10, 3)
uploaded_files = st.file_uploader(
"Upload PDFs, CSVs, or JSONs for context",
type=["PDF", "pdf", "csv", "json"],
accept_multiple_files=True
)
file_paths = []
for uploaded_file in uploaded_files:
file_paths.append(save_file(uploaded_file))
if uploaded_files:
docs = []
for file_path in file_paths:
if file_path.endswith(".pdf"):
docs.extend(rag_util.load_and_split_pdfs([file_path]))
elif file_path.endswith(".csv"):
docs.extend(rag_util.load_and_split_csv(file_path))
elif file_path.endswith(".json"):
docs.extend(rag_util.load_and_split_json(file_path))
DB = rag_util.FaissDb(docs=docs, embedding_function=encoder.embedding_function)
# Initialize chat history
if "messages" not in st.session_state:
st.session_state.messages = []
# Display chat messages from history on app rerun
for message in st.session_state.messages:
with st.chat_message(message["role"]):
st.markdown(message["content"])
# Accept user input
if prompt := st.chat_input("Ask me anything!"):
# Add user message to chat history
st.session_state.messages.append({"role": "user", "content": prompt})
# Display user message in chat message container
with st.chat_message("user"):
st.markdown(prompt)
# Display assistant response in chat message container
with st.chat_message("assistant"):
user_prompt = st.session_state.messages[-1]["content"]
context = (
None if not uploaded_files else DB.similarity_search(user_prompt, k=k)
)
answer = model.generate(
user_prompt, context=context, max_new_tokens=max_new_tokens
)
response = st.write(answer)
st.session_state.messages.append({"role": "assistant", "content": answer})