MEOW commited on
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,99 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import gradio as gr
|
| 3 |
+
|
| 4 |
+
from llama_index.core import Settings, SimpleDirectoryReader, VectorStoreIndex, StorageContext
|
| 5 |
+
from llama_index.llms.nvidia import NVIDIA
|
| 6 |
+
Settings.llm = NVIDIA(model="meta/llama-3.1-8b-instruct")
|
| 7 |
+
|
| 8 |
+
from llama_index.embeddings.nvidia import NVIDIAEmbedding
|
| 9 |
+
Settings.embed_model = NVIDIAEmbedding(model="NV-Embed-QA", truncate="END")
|
| 10 |
+
|
| 11 |
+
from llama_index.vector_stores.milvus import MilvusVectorStore
|
| 12 |
+
from llama_index.core.node_parser import SentenceSplitter
|
| 13 |
+
Settings.text_splitter = SentenceSplitter(chunk_size=400)
|
| 14 |
+
|
| 15 |
+
index = None
|
| 16 |
+
query_engine = None
|
| 17 |
+
|
| 18 |
+
# Function to get file names from file objects
|
| 19 |
+
def get_files_from_input(file_objs):
|
| 20 |
+
if not file_objs:
|
| 21 |
+
return []
|
| 22 |
+
return [file_obj.name for file_obj in file_objs]
|
| 23 |
+
|
| 24 |
+
# Function to load documents and create the index
|
| 25 |
+
def load_documents(file_objs):
|
| 26 |
+
global index, query_engine
|
| 27 |
+
try:
|
| 28 |
+
if not file_objs:
|
| 29 |
+
return "Error: No files selected."
|
| 30 |
+
|
| 31 |
+
file_paths = get_files_from_input(file_objs)
|
| 32 |
+
documents = []
|
| 33 |
+
for file_path in file_paths:
|
| 34 |
+
directory = os.path.dirname(file_path)
|
| 35 |
+
documents.extend(SimpleDirectoryReader(input_files=[file_path]).load_data())
|
| 36 |
+
|
| 37 |
+
if not documents:
|
| 38 |
+
return f"No documents found in the selected files."
|
| 39 |
+
|
| 40 |
+
# Create a Milvus vector store and storage context
|
| 41 |
+
vector_store = MilvusVectorStore(uri="./milvus_demo.db", dim=1024, overwrite=True,output_fields=[])
|
| 42 |
+
storage_context = StorageContext.from_defaults(vector_store=vector_store)
|
| 43 |
+
|
| 44 |
+
# Create the index from the documents
|
| 45 |
+
index = VectorStoreIndex.from_documents(documents, storage_context=storage_context) # Create index inside the function after documents are loaded
|
| 46 |
+
|
| 47 |
+
# Create the query engine after the index is created
|
| 48 |
+
query_engine = index.as_query_engine(similarity_top_k=20, streaming=True)
|
| 49 |
+
return f"Successfully loaded {len(documents)} documents from {len(file_paths)} files."
|
| 50 |
+
except Exception as e:
|
| 51 |
+
return f"Error loading documents: {str(e)}"
|
| 52 |
+
|
| 53 |
+
# Function to handle chat interactions
|
| 54 |
+
def chat(message,history):
|
| 55 |
+
global query_engine
|
| 56 |
+
if query_engine is None:
|
| 57 |
+
return history + [("Please upload a file first.",None)]
|
| 58 |
+
try:
|
| 59 |
+
#modification for nemo guardrails ( next three rows)
|
| 60 |
+
user_message = {"role":"user","content":message}
|
| 61 |
+
response = rails.generate(messages=[user_message])
|
| 62 |
+
return history + [(message,response['content'])]
|
| 63 |
+
except Exception as e:
|
| 64 |
+
return history + [(message,f"Error processing query: {str(e)}")]
|
| 65 |
+
|
| 66 |
+
# Function to stream responses
|
| 67 |
+
def stream_response(message,history):
|
| 68 |
+
global query_engine
|
| 69 |
+
if query_engine is None:
|
| 70 |
+
return history + [("Please upload a file first.",None)]
|
| 71 |
+
|
| 72 |
+
try:
|
| 73 |
+
response = query_engine.query(message)
|
| 74 |
+
return history + [(message, response)]
|
| 75 |
+
except Exception as e:
|
| 76 |
+
yield history + [(message, f"Error processing query: {str(e)}")]
|
| 77 |
+
|
| 78 |
+
# Create the Gradio interface
|
| 79 |
+
with gr.Blocks() as demo:
|
| 80 |
+
gr.Markdown("# RAG Chatbot for PDF Files")
|
| 81 |
+
|
| 82 |
+
with gr.Row():
|
| 83 |
+
file_input = gr.File(label="Select files to upload", file_count="multiple")
|
| 84 |
+
load_btn = gr.Button("Load PDF Documents only")
|
| 85 |
+
|
| 86 |
+
load_output = gr.Textbox(label="Load Status")
|
| 87 |
+
|
| 88 |
+
chatbot = gr.Chatbot()
|
| 89 |
+
msg = gr.Textbox(label="Enter your question",interactive=True)
|
| 90 |
+
clear = gr.Button("Clear")
|
| 91 |
+
|
| 92 |
+
# Set up event handler (Event handlers should be defined within the 'with gr.Blocks() as demo:' block)
|
| 93 |
+
load_btn.click(load_documents, inputs=[file_input], outputs=[load_output])
|
| 94 |
+
msg.submit(stream_response, inputs=[msg, chatbot], outputs=[chatbot]) # Use submit button instead of msg
|
| 95 |
+
clear.click(lambda: None, None, chatbot, queue=False)
|
| 96 |
+
|
| 97 |
+
# Launch the Gradio interface
|
| 98 |
+
if __name__ == "__main__":
|
| 99 |
+
demo.queue().launch(share=True,debug=True)
|