Spaces:
Runtime error
Runtime error
Download app.py from denglhs/Multimodal-RAG-Testing: direct link, hf CLI and curl.
- Browser
- Download file 5.34 kB
-
https://huggingface.co/spaces/denglhs/Multimodal-RAG-Testing/resolve/main/app.py
- Command line
-
hf download hf://spaces/denglhs/Multimodal-RAG-Testing/app.py
-
curl -L -o app.py https://huggingface.co/spaces/denglhs/Multimodal-RAG-Testing/resolve/main/app.py
5.34 kB
| # SPDX-FileCopyrightText: Copyright (c) 2023-2024 NVIDIA CORPORATION & AFFILIATES. All rights reserved. | |
| # SPDX-License-Identifier: Apache-2.0 | |
| # | |
| # Licensed under the Apache License, Version 2.0 (the "License"); | |
| # you may not use this file except in compliance with the License. | |
| # You may obtain a copy of the License at | |
| # | |
| # http://www.apache.org/licenses/LICENSE-2.0 | |
| # | |
| # Unless required by applicable law or agreed to in writing, software | |
| # distributed under the License is distributed on an "AS IS" BASIS, | |
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
| # See the License for the specific language governing permissions and | |
| # limitations under the License. | |
| import os | |
| import streamlit as st | |
| import pandas as pd #panda dataframe | |
| import numpy as np #numpy arrays | |
| from openai import OpenAI #addition for openai | |
| from llama_index.core import Settings | |
| from llama_index.core import VectorStoreIndex, StorageContext | |
| from llama_index.core.node_parser import SentenceSplitter | |
| from llama_index.vector_stores.milvus import MilvusVectorStore | |
| from llama_index.embeddings.nvidia import NVIDIAEmbedding | |
| from llama_index.llms.nvidia import NVIDIA | |
| from document_processors import load_multimodal_data, load_data_from_directory | |
| from utils import set_environment_variables | |
| # Set up the page configuration | |
| st.set_page_config(layout="wide") | |
| # Initialize settings | |
| def initialize_settings(): | |
| Settings.embed_model = NVIDIAEmbedding(model="nvidia/nv-embedqa-e5-v5", truncate="END") | |
| Settings.llm = NVIDIA(model="meta/llama-3.1-8b-instruct") | |
| Settings.text_splitter = SentenceSplitter(chunk_size=500) | |
| # Create index from documents | |
| def create_index(documents): | |
| vector_store = MilvusVectorStore( | |
| host="127.0.0.1", | |
| port=19530, | |
| dim=1024, | |
| collection_name="vectorstore", | |
| gpu_id=0, # Specify the GPU ID to use | |
| output_fields=["field1","field2"] | |
| ) | |
| # vector_store = MilvusVectorStore(uri="./milvus_demo.db", dim=1024, overwrite=True) #For CPU only vector store | |
| storage_context = StorageContext.from_defaults(vector_store=vector_store) | |
| return VectorStoreIndex.from_documents(documents, storage_context=storage_context) | |
| # Main function to run the Streamlit app | |
| def main(): | |
| set_environment_variables() | |
| initialize_settings() | |
| col1, col2 = st.columns([1, 2]) | |
| with col1: | |
| st.title("Multimodal RAG") | |
| input_method = st.radio("Choose input method:", ("Upload Files", "Enter Directory Path")) | |
| if input_method == "Upload Files": | |
| uploaded_files = st.file_uploader("Drag and drop files here", accept_multiple_files=True) | |
| if uploaded_files and st.button("Process Files"): | |
| with st.spinner("Processing files..."): | |
| documents = load_multimodal_data(uploaded_files) | |
| st.session_state['index'] = create_index(documents) | |
| st.session_state['history'] = [] | |
| st.success("Files processed and index created!") | |
| else: | |
| directory_path = st.text_input("Enter directory path:") | |
| if directory_path and st.button("Process Directory"): | |
| if os.path.isdir(directory_path): | |
| with st.spinner("Processing directory..."): | |
| documents = load_data_from_directory(directory_path) | |
| st.session_state['index'] = create_index(documents) | |
| st.session_state['history'] = [] | |
| st.success("Directory processed and index created!") | |
| else: | |
| st.error("Invalid directory path. Please enter a valid path.") | |
| with col2: | |
| if 'index' in st.session_state: | |
| st.title("Chat") | |
| if 'history' not in st.session_state: | |
| st.session_state['history'] = [] | |
| query_engine = st.session_state['index'].as_query_engine(similarity_top_k=20, streaming=True) | |
| user_input = st.chat_input("Enter your query:") | |
| # Display chat messages | |
| chat_container = st.container() | |
| with chat_container: | |
| for message in st.session_state['history']: | |
| with st.chat_message(message["role"]): | |
| st.markdown(message["content"]) | |
| if user_input: | |
| with st.chat_message("user"): | |
| st.markdown(user_input) | |
| st.session_state['history'].append({"role": "user", "content": user_input}) | |
| with st.chat_message("assistant"): | |
| message_placeholder = st.empty() | |
| full_response = "" | |
| response = query_engine.query(user_input) | |
| for token in response.response_gen: | |
| full_response += token | |
| message_placeholder.markdown(full_response + "▌") | |
| message_placeholder.markdown(full_response) | |
| st.session_state['history'].append({"role": "assistant", "content": full_response}) | |
| # Add a clear button | |
| if st.button("Clear Chat"): | |
| st.session_state['history'] = [] | |
| st.rerun() | |
| if __name__ == "__main__": | |
| main() | |