Update app.py
Browse files
app.py
CHANGED
|
@@ -4,12 +4,9 @@ import gradio as gr
|
|
| 4 |
import qdrant_client
|
| 5 |
from getpass import getpass
|
| 6 |
|
| 7 |
-
|
| 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
|
|
@@ -17,14 +14,11 @@ from llama_index.core import Settings
|
|
| 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 |
-
|
| 28 |
chat_engine = None
|
| 29 |
index = None
|
| 30 |
query_engine = None
|
|
@@ -33,31 +27,21 @@ 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 |
-
This version accumulates files, so if you upload more files later,
|
| 44 |
-
they are added to the existing document set.
|
| 45 |
-
"""
|
| 46 |
upload_dir = "uploaded_files"
|
| 47 |
if not os.path.exists(upload_dir):
|
| 48 |
os.makedirs(upload_dir)
|
| 49 |
|
| 50 |
-
# Copy new files into the folder without clearing existing ones.
|
| 51 |
for file_path in files:
|
| 52 |
file_name = os.path.basename(file_path)
|
| 53 |
dest = os.path.join(upload_dir, file_name)
|
| 54 |
if not os.path.exists(dest):
|
| 55 |
shutil.copy(file_path, dest)
|
| 56 |
|
| 57 |
-
# Load documents from the saved folder.
|
| 58 |
documents = SimpleDirectoryReader(upload_dir).load_data()
|
| 59 |
|
| 60 |
-
# Build the index and chat engine using Qdrant as the vector store.
|
| 61 |
global client, vector_store, storage_context, index, query_engine, memory, chat_engine
|
| 62 |
client = qdrant_client.QdrantClient(location=":memory:")
|
| 63 |
|
|
@@ -80,19 +64,15 @@ def process_upload(files):
|
|
| 80 |
chat_mode="context",
|
| 81 |
memory=memory,
|
| 82 |
system_prompt=(
|
| 83 |
-
"You are an AI assistant who answers the user questions
|
| 84 |
-
"use the schema fields to generate appropriate and valid json queries"
|
| 85 |
),
|
| 86 |
)
|
| 87 |
|
| 88 |
return "Documents uploaded and index built successfully!"
|
| 89 |
|
| 90 |
-
|
| 91 |
-
# Chat function that uses the built chat engine.
|
| 92 |
-
# -------------------------------------------------------
|
| 93 |
def chat_with_ai(user_input, chat_history):
|
| 94 |
global chat_engine
|
| 95 |
-
# Check if the chat engine is initialized.
|
| 96 |
if chat_engine is None:
|
| 97 |
return chat_history, "Please upload documents first."
|
| 98 |
|
|
@@ -100,7 +80,6 @@ def chat_with_ai(user_input, chat_history):
|
|
| 100 |
references = response.source_nodes
|
| 101 |
ref, pages = [], []
|
| 102 |
|
| 103 |
-
# Extract file names from the source nodes (if available)
|
| 104 |
for node in references:
|
| 105 |
file_name = node.metadata.get('file_name')
|
| 106 |
if file_name and file_name not in ref:
|
|
@@ -113,20 +92,15 @@ def chat_with_ai(user_input, chat_history):
|
|
| 113 |
chat_history.append((user_input, str(response)))
|
| 114 |
return chat_history, ""
|
| 115 |
|
| 116 |
-
|
| 117 |
-
# Function to clear the chat history.
|
| 118 |
-
# -------------------------------------------------------
|
| 119 |
def clear_history():
|
| 120 |
return [], ""
|
| 121 |
|
| 122 |
-
|
| 123 |
-
# Build the Gradio interface.
|
| 124 |
-
# -------------------------------------------------------
|
| 125 |
def gradio_interface():
|
| 126 |
with gr.Blocks() as demo:
|
| 127 |
-
gr.Markdown("#
|
| 128 |
|
| 129 |
-
# Use Tabs to separate the file upload and chat interfaces.
|
| 130 |
with gr.Tab("Upload Documents"):
|
| 131 |
gr.Markdown("Upload PDF, Excel, CSV, DOC/DOCX, or TXT files below:")
|
| 132 |
# The file upload widget: we specify allowed file types.
|
|
@@ -142,12 +116,12 @@ def gradio_interface():
|
|
| 142 |
upload_button.click(process_upload, inputs=file_upload, outputs=upload_status)
|
| 143 |
|
| 144 |
with gr.Tab("Chat"):
|
| 145 |
-
chatbot = gr.Chatbot(label="
|
| 146 |
user_input = gr.Textbox(
|
| 147 |
placeholder="Ask a question...", label="Enter your question"
|
| 148 |
)
|
| 149 |
submit_button = gr.Button("Send")
|
| 150 |
-
btn_clear = gr.Button("
|
| 151 |
|
| 152 |
# A State to hold the chat history.
|
| 153 |
chat_history = gr.State([])
|
|
|
|
| 4 |
import qdrant_client
|
| 5 |
from getpass import getpass
|
| 6 |
|
| 7 |
+
|
| 8 |
openai_api_key = os.getenv('OPENAI_API_KEY')
|
| 9 |
|
|
|
|
|
|
|
|
|
|
| 10 |
from llama_index.llms.openai import OpenAI
|
| 11 |
from llama_index.embeddings.openai import OpenAIEmbedding
|
| 12 |
from llama_index.core import Settings
|
|
|
|
| 14 |
Settings.llm = OpenAI(model="gpt-3.5-turbo", temperature=0.4)
|
| 15 |
Settings.embed_model = OpenAIEmbedding(model="text-embedding-ada-002")
|
| 16 |
|
|
|
|
|
|
|
|
|
|
| 17 |
from llama_index.core import SimpleDirectoryReader, VectorStoreIndex, StorageContext
|
| 18 |
from llama_index.vector_stores.qdrant import QdrantVectorStore
|
| 19 |
from llama_index.core.memory import ChatMemoryBuffer
|
| 20 |
|
| 21 |
+
|
| 22 |
chat_engine = None
|
| 23 |
index = None
|
| 24 |
query_engine = None
|
|
|
|
| 27 |
vector_store = None
|
| 28 |
storage_context = None
|
| 29 |
|
| 30 |
+
|
|
|
|
|
|
|
| 31 |
def process_upload(files):
|
| 32 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
upload_dir = "uploaded_files"
|
| 34 |
if not os.path.exists(upload_dir):
|
| 35 |
os.makedirs(upload_dir)
|
| 36 |
|
|
|
|
| 37 |
for file_path in files:
|
| 38 |
file_name = os.path.basename(file_path)
|
| 39 |
dest = os.path.join(upload_dir, file_name)
|
| 40 |
if not os.path.exists(dest):
|
| 41 |
shutil.copy(file_path, dest)
|
| 42 |
|
|
|
|
| 43 |
documents = SimpleDirectoryReader(upload_dir).load_data()
|
| 44 |
|
|
|
|
| 45 |
global client, vector_store, storage_context, index, query_engine, memory, chat_engine
|
| 46 |
client = qdrant_client.QdrantClient(location=":memory:")
|
| 47 |
|
|
|
|
| 64 |
chat_mode="context",
|
| 65 |
memory=memory,
|
| 66 |
system_prompt=(
|
| 67 |
+
"You are an AI assistant who answers the user questions"
|
|
|
|
| 68 |
),
|
| 69 |
)
|
| 70 |
|
| 71 |
return "Documents uploaded and index built successfully!"
|
| 72 |
|
| 73 |
+
|
|
|
|
|
|
|
| 74 |
def chat_with_ai(user_input, chat_history):
|
| 75 |
global chat_engine
|
|
|
|
| 76 |
if chat_engine is None:
|
| 77 |
return chat_history, "Please upload documents first."
|
| 78 |
|
|
|
|
| 80 |
references = response.source_nodes
|
| 81 |
ref, pages = [], []
|
| 82 |
|
|
|
|
| 83 |
for node in references:
|
| 84 |
file_name = node.metadata.get('file_name')
|
| 85 |
if file_name and file_name not in ref:
|
|
|
|
| 92 |
chat_history.append((user_input, str(response)))
|
| 93 |
return chat_history, ""
|
| 94 |
|
| 95 |
+
|
|
|
|
|
|
|
| 96 |
def clear_history():
|
| 97 |
return [], ""
|
| 98 |
|
| 99 |
+
|
|
|
|
|
|
|
| 100 |
def gradio_interface():
|
| 101 |
with gr.Blocks() as demo:
|
| 102 |
+
gr.Markdown("# AI Assistant")
|
| 103 |
|
|
|
|
| 104 |
with gr.Tab("Upload Documents"):
|
| 105 |
gr.Markdown("Upload PDF, Excel, CSV, DOC/DOCX, or TXT files below:")
|
| 106 |
# The file upload widget: we specify allowed file types.
|
|
|
|
| 116 |
upload_button.click(process_upload, inputs=file_upload, outputs=upload_status)
|
| 117 |
|
| 118 |
with gr.Tab("Chat"):
|
| 119 |
+
chatbot = gr.Chatbot(label="Chatbot Assistant")
|
| 120 |
user_input = gr.Textbox(
|
| 121 |
placeholder="Ask a question...", label="Enter your question"
|
| 122 |
)
|
| 123 |
submit_button = gr.Button("Send")
|
| 124 |
+
btn_clear = gr.Button("Restart")
|
| 125 |
|
| 126 |
# A State to hold the chat history.
|
| 127 |
chat_history = gr.State([])
|