MEOW commited on
Update app.py
Browse files
app.py
CHANGED
|
@@ -22,6 +22,7 @@ from llama_index.readers.file import (
|
|
| 22 |
ImageReader,
|
| 23 |
CSVReader,
|
| 24 |
)
|
|
|
|
| 25 |
|
| 26 |
# Initialize LLM, embedding model, and text splitter
|
| 27 |
Settings.llm = NVIDIA(model="meta/llama-3.1-8b-instruct")
|
|
@@ -81,8 +82,7 @@ def load_documents(file_objs, url=None, service_context=None):
|
|
| 81 |
return f"No documents found in the selected files."
|
| 82 |
|
| 83 |
# Create index from documents
|
| 84 |
-
|
| 85 |
-
vector_store = MilvusVectorStore(
|
| 86 |
host="127.0.0.1",
|
| 87 |
port=19530,
|
| 88 |
dim=1024,
|
|
@@ -92,7 +92,6 @@ def load_documents(file_objs, url=None, service_context=None):
|
|
| 92 |
)
|
| 93 |
# vector_store = MilvusVectorStore(uri="./milvus_demo.db", dim=1024, overwrite=True) #For CPU only vector store
|
| 94 |
storage_context = StorageContext.from_defaults(vector_store=vector_store)
|
| 95 |
-
return VectorStoreIndex.from_documents(documents, storage_context=storage_context)
|
| 96 |
|
| 97 |
# Create index and query engine
|
| 98 |
index = VectorStoreIndex.from_documents(
|
|
@@ -100,6 +99,7 @@ def load_documents(file_objs, url=None, service_context=None):
|
|
| 100 |
)
|
| 101 |
query_engine = index.as_query_engine(similarity_top_k=20, streaming=True)
|
| 102 |
return "Documents loaded successfully!"
|
|
|
|
| 103 |
except Exception as e:
|
| 104 |
return f"Error loading documents: {str(e)}"
|
| 105 |
|
|
@@ -116,30 +116,12 @@ def chat(message,history):
|
|
| 116 |
except Exception as e:
|
| 117 |
return history + [(message,f"Error processing query: {str(e)}")]
|
| 118 |
|
| 119 |
-
# Function to stream responses
|
| 120 |
-
def stream_response(message,history):
|
| 121 |
-
global query_engine
|
| 122 |
-
if query_engine is None:
|
| 123 |
-
yield history + [("Please upload a file first.",None)]
|
| 124 |
-
return
|
| 125 |
-
|
| 126 |
-
try:
|
| 127 |
-
response = query_engine.query(message)
|
| 128 |
-
partial_response = ""
|
| 129 |
-
for text in response.response_gen:
|
| 130 |
-
partial_response += text
|
| 131 |
-
yield history + [(message,partial_response)]
|
| 132 |
-
except Exception as e:
|
| 133 |
-
yield history + [(message, f"Error processing query: {str(e)}")]
|
| 134 |
-
|
| 135 |
-
|
| 136 |
with gr.Blocks() as iface:
|
| 137 |
gr.Markdown("# Multi-document RAG Chatbot 🦀 ") # Title using Markdown
|
| 138 |
gr.Markdown("Upload various documents or HTML URL for Q&A")
|
| 139 |
with gr.Row():
|
| 140 |
with gr.Column():
|
| 141 |
-
file_input = gr.File(
|
| 142 |
-
file_count="multiple", label="Upload documents")
|
| 143 |
with gr.Column():
|
| 144 |
url_input = gr.Textbox(label="Enter URL")
|
| 145 |
with gr.Row():
|
|
@@ -147,18 +129,17 @@ with gr.Blocks() as iface:
|
|
| 147 |
status_output = gr.Textbox(label="Status")
|
| 148 |
|
| 149 |
load_button.click(
|
| 150 |
-
fn=load_documents,
|
| 151 |
-
inputs=[file_input, url_input],
|
| 152 |
-
outputs=status_output,
|
| 153 |
)
|
| 154 |
|
| 155 |
with gr.Row():
|
| 156 |
chatbot = gr.Chatbot()
|
| 157 |
msg = gr.Textbox(label="Enter your question")
|
| 158 |
-
|
|
|
|
| 159 |
clear_button = gr.Button("Clear")
|
| 160 |
clear_button.click(lambda: None, None, chatbot, queue=False)
|
| 161 |
|
| 162 |
# Launch the Gradio interface
|
| 163 |
if __name__ == "__main__":
|
| 164 |
-
|
|
|
|
| 22 |
ImageReader,
|
| 23 |
CSVReader,
|
| 24 |
)
|
| 25 |
+
from llama_index.text_splitter import SentenceSplitter # Import SentenceSplitter
|
| 26 |
|
| 27 |
# Initialize LLM, embedding model, and text splitter
|
| 28 |
Settings.llm = NVIDIA(model="meta/llama-3.1-8b-instruct")
|
|
|
|
| 82 |
return f"No documents found in the selected files."
|
| 83 |
|
| 84 |
# Create index from documents
|
| 85 |
+
vector_store = MilvusVectorStore(
|
|
|
|
| 86 |
host="127.0.0.1",
|
| 87 |
port=19530,
|
| 88 |
dim=1024,
|
|
|
|
| 92 |
)
|
| 93 |
# vector_store = MilvusVectorStore(uri="./milvus_demo.db", dim=1024, overwrite=True) #For CPU only vector store
|
| 94 |
storage_context = StorageContext.from_defaults(vector_store=vector_store)
|
|
|
|
| 95 |
|
| 96 |
# Create index and query engine
|
| 97 |
index = VectorStoreIndex.from_documents(
|
|
|
|
| 99 |
)
|
| 100 |
query_engine = index.as_query_engine(similarity_top_k=20, streaming=True)
|
| 101 |
return "Documents loaded successfully!"
|
| 102 |
+
|
| 103 |
except Exception as e:
|
| 104 |
return f"Error loading documents: {str(e)}"
|
| 105 |
|
|
|
|
| 116 |
except Exception as e:
|
| 117 |
return history + [(message,f"Error processing query: {str(e)}")]
|
| 118 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 119 |
with gr.Blocks() as iface:
|
| 120 |
gr.Markdown("# Multi-document RAG Chatbot 🦀 ") # Title using Markdown
|
| 121 |
gr.Markdown("Upload various documents or HTML URL for Q&A")
|
| 122 |
with gr.Row():
|
| 123 |
with gr.Column():
|
| 124 |
+
file_input = gr.File(file_count="multiple", label="Upload documents")
|
|
|
|
| 125 |
with gr.Column():
|
| 126 |
url_input = gr.Textbox(label="Enter URL")
|
| 127 |
with gr.Row():
|
|
|
|
| 129 |
status_output = gr.Textbox(label="Status")
|
| 130 |
|
| 131 |
load_button.click(
|
| 132 |
+
fn=load_documents, inputs=[file_input, url_input, outputs=status_output,
|
|
|
|
|
|
|
| 133 |
)
|
| 134 |
|
| 135 |
with gr.Row():
|
| 136 |
chatbot = gr.Chatbot()
|
| 137 |
msg = gr.Textbox(label="Enter your question")
|
| 138 |
+
# Use the chat function with Gurardrails
|
| 139 |
+
msg.submit(chat, inputs=[msg, chatbot], outputs=[chatbot])
|
| 140 |
clear_button = gr.Button("Clear")
|
| 141 |
clear_button.click(lambda: None, None, chatbot, queue=False)
|
| 142 |
|
| 143 |
# Launch the Gradio interface
|
| 144 |
if __name__ == "__main__":
|
| 145 |
+
iface.queue().launch(share=True,debug=True)
|