Spaces:
Runtime error
Runtime error
MEOW commited on
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,119 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# SPDX-FileCopyrightText: Copyright (c) 2023-2024 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
|
| 2 |
+
# SPDX-License-Identifier: Apache-2.0
|
| 3 |
+
#
|
| 4 |
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
| 5 |
+
# you may not use this file except in compliance with the License.
|
| 6 |
+
# You may obtain a copy of the License at
|
| 7 |
+
#
|
| 8 |
+
# http://www.apache.org/licenses/LICENSE-2.0
|
| 9 |
+
#
|
| 10 |
+
# Unless required by applicable law or agreed to in writing, software
|
| 11 |
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
| 12 |
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
| 13 |
+
# See the License for the specific language governing permissions and
|
| 14 |
+
# limitations under the License.
|
| 15 |
+
|
| 16 |
+
import os
|
| 17 |
+
import streamlit as st
|
| 18 |
+
from llama_index.core import Settings
|
| 19 |
+
from llama_index.core import VectorStoreIndex, StorageContext
|
| 20 |
+
from llama_index.core.node_parser import SentenceSplitter
|
| 21 |
+
from llama_index.vector_stores.milvus import MilvusVectorStore
|
| 22 |
+
from llama_index.embeddings.nvidia import NVIDIAEmbedding
|
| 23 |
+
from llama_index.llms.nvidia import NVIDIA
|
| 24 |
+
|
| 25 |
+
from document_processors import load_multimodal_data, load_data_from_directory
|
| 26 |
+
from utils import set_environment_variables
|
| 27 |
+
|
| 28 |
+
# Set up the page configuration
|
| 29 |
+
st.set_page_config(layout="wide")
|
| 30 |
+
|
| 31 |
+
# Initialize settings
|
| 32 |
+
def initialize_settings():
|
| 33 |
+
Settings.embed_model = NVIDIAEmbedding(model="nvidia/nv-embedqa-e5-v5", truncate="END")
|
| 34 |
+
Settings.llm = NVIDIA(model="meta/llama-3.1-8b-instruct
|
| 35 |
+
")
|
| 36 |
+
Settings.text_splitter = SentenceSplitter(chunk_size=400)
|
| 37 |
+
|
| 38 |
+
# Create index from documents
|
| 39 |
+
def create_index(documents):
|
| 40 |
+
vector_store = MilvusVectorStore(
|
| 41 |
+
host = "127.0.0.1",
|
| 42 |
+
port = 19530,
|
| 43 |
+
dim = 1024
|
| 44 |
+
)
|
| 45 |
+
# vector_store = MilvusVectorStore(uri="./milvus_demo.db", dim=1024, overwrite=True) #For CPU only vector store
|
| 46 |
+
storage_context = StorageContext.from_defaults(vector_store=vector_store)
|
| 47 |
+
return VectorStoreIndex.from_documents(documents, storage_context=storage_context)
|
| 48 |
+
|
| 49 |
+
# Main function to run the Streamlit app
|
| 50 |
+
def main():
|
| 51 |
+
set_environment_variables()
|
| 52 |
+
initialize_settings()
|
| 53 |
+
|
| 54 |
+
col1, col2 = st.columns([1, 2])
|
| 55 |
+
|
| 56 |
+
with col1:
|
| 57 |
+
st.title("Multimodal RAG")
|
| 58 |
+
|
| 59 |
+
input_method = st.radio("Choose input method:", ("Upload Files", "Enter Directory Path"))
|
| 60 |
+
|
| 61 |
+
if input_method == "Upload Files":
|
| 62 |
+
uploaded_files = st.file_uploader("Drag and drop files here", accept_multiple_files=True)
|
| 63 |
+
if uploaded_files and st.button("Process Files"):
|
| 64 |
+
with st.spinner("Processing files..."):
|
| 65 |
+
documents = load_multimodal_data(uploaded_files)
|
| 66 |
+
st.session_state['index'] = create_index(documents)
|
| 67 |
+
st.session_state['history'] = []
|
| 68 |
+
st.success("Files processed and index created!")
|
| 69 |
+
else:
|
| 70 |
+
directory_path = st.text_input("Enter directory path:")
|
| 71 |
+
if directory_path and st.button("Process Directory"):
|
| 72 |
+
if os.path.isdir(directory_path):
|
| 73 |
+
with st.spinner("Processing directory..."):
|
| 74 |
+
documents = load_data_from_directory(directory_path)
|
| 75 |
+
st.session_state['index'] = create_index(documents)
|
| 76 |
+
st.session_state['history'] = []
|
| 77 |
+
st.success("Directory processed and index created!")
|
| 78 |
+
else:
|
| 79 |
+
st.error("Invalid directory path. Please enter a valid path.")
|
| 80 |
+
|
| 81 |
+
with col2:
|
| 82 |
+
if 'index' in st.session_state:
|
| 83 |
+
st.title("Chat")
|
| 84 |
+
if 'history' not in st.session_state:
|
| 85 |
+
st.session_state['history'] = []
|
| 86 |
+
|
| 87 |
+
query_engine = st.session_state['index'].as_query_engine(similarity_top_k=20, streaming=True)
|
| 88 |
+
|
| 89 |
+
user_input = st.chat_input("Enter your query:")
|
| 90 |
+
|
| 91 |
+
# Display chat messages
|
| 92 |
+
chat_container = st.container()
|
| 93 |
+
with chat_container:
|
| 94 |
+
for message in st.session_state['history']:
|
| 95 |
+
with st.chat_message(message["role"]):
|
| 96 |
+
st.markdown(message["content"])
|
| 97 |
+
|
| 98 |
+
if user_input:
|
| 99 |
+
with st.chat_message("user"):
|
| 100 |
+
st.markdown(user_input)
|
| 101 |
+
st.session_state['history'].append({"role": "user", "content": user_input})
|
| 102 |
+
|
| 103 |
+
with st.chat_message("assistant"):
|
| 104 |
+
message_placeholder = st.empty()
|
| 105 |
+
full_response = ""
|
| 106 |
+
response = query_engine.query(user_input)
|
| 107 |
+
for token in response.response_gen:
|
| 108 |
+
full_response += token
|
| 109 |
+
message_placeholder.markdown(full_response + "▌")
|
| 110 |
+
message_placeholder.markdown(full_response)
|
| 111 |
+
st.session_state['history'].append({"role": "assistant", "content": full_response})
|
| 112 |
+
|
| 113 |
+
# Add a clear button
|
| 114 |
+
if st.button("Clear Chat"):
|
| 115 |
+
st.session_state['history'] = []
|
| 116 |
+
st.rerun()
|
| 117 |
+
|
| 118 |
+
if __name__ == "__main__":
|
| 119 |
+
main()
|