Update app.py
Browse files
app.py
CHANGED
|
@@ -1,119 +1,163 @@
|
|
| 1 |
import os
|
|
|
|
|
|
|
|
|
|
| 2 |
from getpass import getpass
|
| 3 |
|
|
|
|
| 4 |
openai_api_key = os.getenv('OPENAI_API_KEY')
|
| 5 |
-
openai_api_key = openai_api_key
|
| 6 |
-
|
| 7 |
|
|
|
|
|
|
|
|
|
|
| 8 |
from llama_index.llms.openai import OpenAI
|
| 9 |
from llama_index.embeddings.openai import OpenAIEmbedding
|
| 10 |
from llama_index.core import Settings
|
| 11 |
|
| 12 |
-
Settings.llm = OpenAI(model="gpt-3.5-turbo",temperature=0.4)
|
| 13 |
Settings.embed_model = OpenAIEmbedding(model="text-embedding-ada-002")
|
| 14 |
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
from llama_index.core import VectorStoreIndex, StorageContext
|
| 20 |
from llama_index.vector_stores.qdrant import QdrantVectorStore
|
| 21 |
-
import qdrant_client
|
| 22 |
-
|
| 23 |
-
client = qdrant_client.QdrantClient(
|
| 24 |
-
location=":memory:",
|
| 25 |
-
)
|
| 26 |
-
|
| 27 |
-
vector_store = QdrantVectorStore(
|
| 28 |
-
collection_name = "paper",
|
| 29 |
-
client=client,
|
| 30 |
-
enable_hybrid=True,
|
| 31 |
-
batch_size=20,
|
| 32 |
-
)
|
| 33 |
-
|
| 34 |
-
storage_context = StorageContext.from_defaults(vector_store=vector_store)
|
| 35 |
-
|
| 36 |
-
index = VectorStoreIndex.from_documents(
|
| 37 |
-
documents,
|
| 38 |
-
storage_context=storage_context,
|
| 39 |
-
)
|
| 40 |
-
|
| 41 |
-
query_engine = index.as_query_engine(
|
| 42 |
-
vector_store_query_mode="hybrid"
|
| 43 |
-
)
|
| 44 |
-
|
| 45 |
from llama_index.core.memory import ChatMemoryBuffer
|
| 46 |
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
#
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 69 |
def chat_with_ai(user_input, chat_history):
|
| 70 |
-
|
| 71 |
-
#
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
# response = "you're wlocome"
|
| 76 |
-
# chat_history.append((user_input, response))
|
| 77 |
-
# return chat_history, ""
|
| 78 |
response = chat_engine.chat(user_input)
|
| 79 |
references = response.source_nodes
|
| 80 |
-
ref,pages = [],[]
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
|
|
|
| 92 |
return chat_history, ""
|
| 93 |
|
|
|
|
|
|
|
|
|
|
| 94 |
def clear_history():
|
| 95 |
return [], ""
|
| 96 |
|
| 97 |
-
|
|
|
|
|
|
|
|
|
|
| 98 |
with gr.Blocks() as demo:
|
| 99 |
-
gr.Markdown("# Chat Interface for LlamaIndex")
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 117 |
return demo
|
| 118 |
|
| 119 |
-
|
|
|
|
|
|
| 1 |
import os
|
| 2 |
+
import shutil
|
| 3 |
+
import gradio as gr
|
| 4 |
+
import qdrant_client
|
| 5 |
from getpass import getpass
|
| 6 |
|
| 7 |
+
# Set your OpenAI API key from environment variables.
|
| 8 |
openai_api_key = os.getenv('OPENAI_API_KEY')
|
|
|
|
|
|
|
| 9 |
|
| 10 |
+
# -------------------------------------------------------
|
| 11 |
+
# Configure LlamaIndex with OpenAI LLM and Embeddings
|
| 12 |
+
# -------------------------------------------------------
|
| 13 |
from llama_index.llms.openai import OpenAI
|
| 14 |
from llama_index.embeddings.openai import OpenAIEmbedding
|
| 15 |
from llama_index.core import Settings
|
| 16 |
|
| 17 |
+
Settings.llm = OpenAI(model="gpt-3.5-turbo", temperature=0.4)
|
| 18 |
Settings.embed_model = OpenAIEmbedding(model="text-embedding-ada-002")
|
| 19 |
|
| 20 |
+
# -------------------------------------------------------
|
| 21 |
+
# Import document readers, index, vector store, memory, etc.
|
| 22 |
+
# -------------------------------------------------------
|
| 23 |
+
from llama_index.core import SimpleDirectoryReader, VectorStoreIndex, StorageContext
|
|
|
|
| 24 |
from llama_index.vector_stores.qdrant import QdrantVectorStore
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
from llama_index.core.memory import ChatMemoryBuffer
|
| 26 |
|
| 27 |
+
# Global variables to hold the index and chat engine.
|
| 28 |
+
chat_engine = None
|
| 29 |
+
index = None
|
| 30 |
+
query_engine = None
|
| 31 |
+
memory = None
|
| 32 |
+
client = None
|
| 33 |
+
vector_store = None
|
| 34 |
+
storage_context = None
|
| 35 |
+
|
| 36 |
+
# -------------------------------------------------------
|
| 37 |
+
# Function to process uploaded files and build the index.
|
| 38 |
+
# -------------------------------------------------------
|
| 39 |
+
def process_upload(files):
|
| 40 |
+
"""
|
| 41 |
+
Accepts a list of uploaded file paths, saves them to a local folder,
|
| 42 |
+
loads them as documents, and builds the vector index and chat engine.
|
| 43 |
+
"""
|
| 44 |
+
upload_dir = "uploaded_files"
|
| 45 |
+
if not os.path.exists(upload_dir):
|
| 46 |
+
os.makedirs(upload_dir)
|
| 47 |
+
else:
|
| 48 |
+
# Clear any existing files in the folder.
|
| 49 |
+
for f in os.listdir(upload_dir):
|
| 50 |
+
os.remove(os.path.join(upload_dir, f))
|
| 51 |
+
|
| 52 |
+
# 'files' is a list of file paths (Gradio's File component with type="file")
|
| 53 |
+
for file_path in files:
|
| 54 |
+
file_name = os.path.basename(file_path)
|
| 55 |
+
dest = os.path.join(upload_dir, file_name)
|
| 56 |
+
shutil.copy(file_path, dest)
|
| 57 |
+
|
| 58 |
+
# Load documents from the saved folder.
|
| 59 |
+
documents = SimpleDirectoryReader(upload_dir).load_data()
|
| 60 |
+
|
| 61 |
+
# Build the index and chat engine using Qdrant as the vector store.
|
| 62 |
+
global client, vector_store, storage_context, index, query_engine, memory, chat_engine
|
| 63 |
+
client = qdrant_client.QdrantClient(location=":memory:")
|
| 64 |
+
|
| 65 |
+
vector_store = QdrantVectorStore(
|
| 66 |
+
collection_name="paper",
|
| 67 |
+
client=client,
|
| 68 |
+
enable_hybrid=True,
|
| 69 |
+
batch_size=20,
|
| 70 |
+
)
|
| 71 |
+
|
| 72 |
+
storage_context = StorageContext.from_defaults(vector_store=vector_store)
|
| 73 |
+
|
| 74 |
+
index = VectorStoreIndex.from_documents(documents, storage_context=storage_context)
|
| 75 |
+
|
| 76 |
+
query_engine = index.as_query_engine(vector_store_query_mode="hybrid")
|
| 77 |
+
|
| 78 |
+
memory = ChatMemoryBuffer.from_defaults(token_limit=3000)
|
| 79 |
+
|
| 80 |
+
chat_engine = index.as_chat_engine(
|
| 81 |
+
chat_mode="context",
|
| 82 |
+
memory=memory,
|
| 83 |
+
system_prompt=(
|
| 84 |
+
"You are an AI assistant who answers the user questions, "
|
| 85 |
+
"use the schema fields to generate appropriate and valid json queries"
|
| 86 |
+
),
|
| 87 |
+
)
|
| 88 |
+
|
| 89 |
+
return "Documents uploaded and index built successfully!"
|
| 90 |
+
|
| 91 |
+
# -------------------------------------------------------
|
| 92 |
+
# Chat function that uses the built chat engine.
|
| 93 |
+
# -------------------------------------------------------
|
| 94 |
def chat_with_ai(user_input, chat_history):
|
| 95 |
+
global chat_engine
|
| 96 |
+
# Check if the chat engine is initialized.
|
| 97 |
+
if chat_engine is None:
|
| 98 |
+
return chat_history, "Please upload documents first."
|
| 99 |
+
|
|
|
|
|
|
|
|
|
|
| 100 |
response = chat_engine.chat(user_input)
|
| 101 |
references = response.source_nodes
|
| 102 |
+
ref, pages = [], []
|
| 103 |
+
|
| 104 |
+
# Extract file names from the source nodes (if available)
|
| 105 |
+
for node in references:
|
| 106 |
+
file_name = node.metadata.get('file_name')
|
| 107 |
+
if file_name and file_name not in ref:
|
| 108 |
+
ref.append(file_name)
|
| 109 |
+
|
| 110 |
+
complete_response = str(response) + "\n\n"
|
| 111 |
+
if ref or pages:
|
| 112 |
+
chat_history.append((user_input, complete_response))
|
| 113 |
+
else:
|
| 114 |
+
chat_history.append((user_input, str(response)))
|
| 115 |
return chat_history, ""
|
| 116 |
|
| 117 |
+
# -------------------------------------------------------
|
| 118 |
+
# Function to clear the chat history.
|
| 119 |
+
# -------------------------------------------------------
|
| 120 |
def clear_history():
|
| 121 |
return [], ""
|
| 122 |
|
| 123 |
+
# -------------------------------------------------------
|
| 124 |
+
# Build the Gradio interface.
|
| 125 |
+
# -------------------------------------------------------
|
| 126 |
+
def gradio_interface():
|
| 127 |
with gr.Blocks() as demo:
|
| 128 |
+
gr.Markdown("# Chat Interface for LlamaIndex with File Upload")
|
| 129 |
+
|
| 130 |
+
# Use Tabs to separate the file upload and chat interfaces.
|
| 131 |
+
with gr.Tab("Upload Documents"):
|
| 132 |
+
gr.Markdown("Upload PDF, Excel, CSV, DOC/DOCX, or TXT files below:")
|
| 133 |
+
# The file upload widget: we specify allowed file types.
|
| 134 |
+
file_upload = gr.File(
|
| 135 |
+
label="Upload Files",
|
| 136 |
+
file_count="multiple",
|
| 137 |
+
file_types=[".pdf", ".csv", ".txt", ".xlsx", ".xls", ".doc", ".docx"],
|
| 138 |
+
type="file" # returns file paths
|
| 139 |
+
)
|
| 140 |
+
upload_status = gr.Textbox(label="Upload Status", interactive=False)
|
| 141 |
+
upload_button = gr.Button("Process Upload")
|
| 142 |
+
|
| 143 |
+
upload_button.click(process_upload, inputs=file_upload, outputs=upload_status)
|
| 144 |
+
|
| 145 |
+
with gr.Tab("Chat"):
|
| 146 |
+
chatbot = gr.Chatbot(label="LlamaIndex Chatbot")
|
| 147 |
+
user_input = gr.Textbox(
|
| 148 |
+
placeholder="Ask a question...", label="Enter your question"
|
| 149 |
+
)
|
| 150 |
+
submit_button = gr.Button("Send")
|
| 151 |
+
btn_clear = gr.Button("Clear History")
|
| 152 |
+
|
| 153 |
+
# A State to hold the chat history.
|
| 154 |
+
chat_history = gr.State([])
|
| 155 |
+
|
| 156 |
+
submit_button.click(chat_with_ai, inputs=[user_input, chat_history], outputs=[chatbot, user_input])
|
| 157 |
+
user_input.submit(chat_with_ai, inputs=[user_input, chat_history], outputs=[chatbot, user_input])
|
| 158 |
+
btn_clear.click(clear_history, outputs=[chatbot, user_input])
|
| 159 |
+
|
| 160 |
return demo
|
| 161 |
|
| 162 |
+
# Launch the Gradio app.
|
| 163 |
+
gradio_interface().launch(debug=True)
|