anasmkh commited on
Commit
1e22681
·
verified ·
1 Parent(s): d29b8ab

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +8 -32
app.py CHANGED
@@ -7,9 +7,7 @@ from getpass import getpass
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 +15,12 @@ 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
- # Global variables to hold the index and chat engine.
28
  chat_engine = None
29
  index = None
30
  query_engine = None
@@ -33,32 +29,22 @@ 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
 
@@ -88,12 +74,9 @@ def process_upload(files):
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
 
@@ -101,7 +84,6 @@ def chat_with_ai(user_input, chat_history):
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:
@@ -114,23 +96,17 @@ def chat_with_ai(user_input, chat_history):
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",
@@ -158,5 +134,5 @@ def gradio_interface():
158
 
159
  return demo
160
 
161
- # Launch the Gradio app.
162
  gradio_interface().launch(debug=True)
 
7
 
8
  openai_api_key = os.getenv('OPENAI_API_KEY')
9
 
10
+
 
 
11
  from llama_index.llms.openai import OpenAI
12
  from llama_index.embeddings.openai import OpenAIEmbedding
13
  from llama_index.core import Settings
 
15
  Settings.llm = OpenAI(model="gpt-3.5-turbo", temperature=0.4)
16
  Settings.embed_model = OpenAIEmbedding(model="text-embedding-ada-002")
17
 
18
+
 
 
19
  from llama_index.core import SimpleDirectoryReader, VectorStoreIndex, StorageContext
20
  from llama_index.vector_stores.qdrant import QdrantVectorStore
21
  from llama_index.core.memory import ChatMemoryBuffer
22
 
23
+
24
  chat_engine = None
25
  index = None
26
  query_engine = None
 
29
  vector_store = None
30
  storage_context = None
31
 
32
+
 
 
33
  def process_upload(files):
 
 
 
 
34
  upload_dir = "uploaded_files"
35
  if not os.path.exists(upload_dir):
36
  os.makedirs(upload_dir)
37
  else:
 
38
  for f in os.listdir(upload_dir):
39
  os.remove(os.path.join(upload_dir, f))
40
 
 
41
  for file_path in files:
42
  file_name = os.path.basename(file_path)
43
  dest = os.path.join(upload_dir, file_name)
44
  shutil.copy(file_path, dest)
45
 
 
46
  documents = SimpleDirectoryReader(upload_dir).load_data()
47
 
 
48
  global client, vector_store, storage_context, index, query_engine, memory, chat_engine
49
  client = qdrant_client.QdrantClient(location=":memory:")
50
 
 
74
 
75
  return "Documents uploaded and index built successfully!"
76
 
77
+
 
 
78
  def chat_with_ai(user_input, chat_history):
79
  global chat_engine
 
80
  if chat_engine is None:
81
  return chat_history, "Please upload documents first."
82
 
 
84
  references = response.source_nodes
85
  ref, pages = [], []
86
 
 
87
  for node in references:
88
  file_name = node.metadata.get('file_name')
89
  if file_name and file_name not in ref:
 
96
  chat_history.append((user_input, str(response)))
97
  return chat_history, ""
98
 
99
+
 
 
100
  def clear_history():
101
  return [], ""
102
 
103
+
 
 
104
  def gradio_interface():
105
  with gr.Blocks() as demo:
106
  gr.Markdown("# Chat Interface for LlamaIndex with File Upload")
107
 
 
108
  with gr.Tab("Upload Documents"):
109
  gr.Markdown("Upload PDF, Excel, CSV, DOC/DOCX, or TXT files below:")
 
110
  file_upload = gr.File(
111
  label="Upload Files",
112
  file_count="multiple",
 
134
 
135
  return demo
136
 
137
+
138
  gradio_interface().launch(debug=True)