anasmkh commited on
Commit
29c811a
·
verified ·
1 Parent(s): af5fe62

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +110 -86
app.py CHANGED
@@ -1,120 +1,144 @@
1
-
2
  import os
 
3
  from getpass import getpass
4
 
5
- openai_api_key = os.getenv('OPENAI_API_KEY')
6
- openai_api_key = openai_api_key
7
-
8
-
9
  from llama_index.llms.openai import OpenAI
10
  from llama_index.embeddings.openai import OpenAIEmbedding
11
- from llama_index.core import Settings
12
-
13
- Settings.llm = OpenAI(model="gpt-3.5-turbo",temperature=0.4)
14
- Settings.embed_model = OpenAIEmbedding(model="text-embedding-ada-002")
15
-
16
- from llama_index.core import SimpleDirectoryReader
17
-
18
- documents = SimpleDirectoryReader("new_file").load_data()
19
-
20
- from llama_index.core import VectorStoreIndex, StorageContext
21
  from llama_index.vector_stores.qdrant import QdrantVectorStore
 
22
  import qdrant_client
23
 
24
- client = qdrant_client.QdrantClient(
25
- location=":memory:",
 
 
 
 
 
 
 
26
  )
27
 
 
 
 
 
 
 
 
 
 
28
  vector_store = QdrantVectorStore(
29
- collection_name = "paper",
30
  client=client,
31
  enable_hybrid=True,
32
  batch_size=20,
33
  )
34
-
35
  storage_context = StorageContext.from_defaults(vector_store=vector_store)
36
 
37
- index = VectorStoreIndex.from_documents(
38
- documents,
39
- storage_context=storage_context,
40
- )
41
-
42
- query_engine = index.as_query_engine(
43
- vector_store_query_mode="hybrid"
44
- )
45
-
46
- from llama_index.core.memory import ChatMemoryBuffer
47
-
48
- memory = ChatMemoryBuffer.from_defaults(token_limit=3000)
49
-
50
  chat_engine = index.as_chat_engine(
51
  chat_mode="context",
52
- memory=memory,
53
- system_prompt=(
54
- """You are an AI assistant who answers the user questions,
55
- use the schema fields to generate appriopriate and valid json queries"""
56
- ),
57
  )
58
 
59
- # def is_greeting(user_input):
60
-
61
- # greetings = ["hello", "hi", "hey", "good morning", "good afternoon", "good evening", "greetings"]
62
- # user_input_lower = user_input.lower().strip()
63
- # return any(greet in user_input_lower for greet in greetings)
64
- # def is_bye(user_input):
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
65
 
66
- # greetings = ["thanks", "thanks you", "thanks a lot", "good answer", "good bye", "bye bye"]
67
- # user_input_lower = user_input.lower().strip()
68
- # return any(greet in user_input_lower for greet in greetings)
69
- import gradio as gr
70
  def chat_with_ai(user_input, chat_history):
71
- # if is_greeting(user_input):
72
- # response = 'hi, how can i help you?'
73
- # chat_history.append((user_input, response))
74
- # return chat_history, ""
75
- # elif is_bye(user_input):
76
- # response = "you're wlocome"
77
- # chat_history.append((user_input, response))
78
- # return chat_history, ""
79
  response = chat_engine.chat(user_input)
 
80
  references = response.source_nodes
81
- ref,pages = [],[]
82
- for i in range(len(references)):
83
- if references[i].metadata['file_name'] not in ref:
84
- ref.append(references[i].metadata['file_name'])
85
- # pages.append(references[i].metadata['page_label'])
86
- complete_response = str(response) + "\n\n"
87
- if ref !=[] or pages!=[]:
88
- chat_history.append((user_input, complete_response))
89
- ref = []
90
- elif ref==[] or pages==[]:
91
- chat_history.append((user_input,str(response)))
92
-
93
  return chat_history, ""
94
 
95
  def clear_history():
96
  return [], ""
97
 
98
  def gradio_chatbot():
 
 
 
 
 
99
  with gr.Blocks() as demo:
100
- gr.Markdown("# Chat Interface for LlamaIndex")
101
-
102
- chatbot = gr.Chatbot(label="LlamaIndex Chatbot")
103
- user_input = gr.Textbox(
104
- placeholder="Ask a question...", label="Enter your question"
105
- )
106
-
107
- submit_button = gr.Button("Send")
108
- btn_clear = gr.Button("Delete Context")
109
-
110
-
111
- chat_history = gr.State([])
112
-
113
- submit_button.click(chat_with_ai, inputs=[user_input, chat_history], outputs=[chatbot, user_input])
114
-
115
- user_input.submit(chat_with_ai, inputs=[user_input, chat_history], outputs=[chatbot, user_input])
116
- btn_clear.click(fn=clear_history, outputs=[chatbot, user_input])
117
-
 
 
 
 
 
118
  return demo
119
 
120
- gradio_chatbot().launch(debug=True)
 
 
 
1
  import os
2
+ import shutil
3
  from getpass import getpass
4
 
5
+ import gradio as gr
 
 
 
6
  from llama_index.llms.openai import OpenAI
7
  from llama_index.embeddings.openai import OpenAIEmbedding
8
+ from llama_index.core import Settings, SimpleDirectoryReader, VectorStoreIndex, StorageContext
 
 
 
 
 
 
 
 
 
9
  from llama_index.vector_stores.qdrant import QdrantVectorStore
10
+ from llama_index.core.memory import ChatMemoryBuffer
11
  import qdrant_client
12
 
13
+ # Set your OpenAI API key from environment variable
14
+ openai_api_key = os.getenv('OPENAI_API_KEY')
15
+ if not openai_api_key:
16
+ raise ValueError("Please set your OPENAI_API_KEY environment variable.")
17
+
18
+ # Define a system prompt as a global constant
19
+ SYSTEM_PROMPT = (
20
+ "You are an AI assistant who answers the user questions, "
21
+ "use the schema fields to generate appropriate and valid json queries"
22
  )
23
 
24
+ # Configure the LLM and embedding models
25
+ Settings.llm = OpenAI(model="gpt-3.5-turbo", temperature=0.4)
26
+ Settings.embed_model = OpenAIEmbedding(model="text-embedding-ada-002")
27
+
28
+ # Load initial documents from a directory called "new_file"
29
+ documents = SimpleDirectoryReader("new_file").load_data()
30
+
31
+ # Set up the Qdrant vector store (using an in-memory collection for simplicity)
32
+ client = qdrant_client.QdrantClient(location=":memory:")
33
  vector_store = QdrantVectorStore(
34
+ collection_name="paper",
35
  client=client,
36
  enable_hybrid=True,
37
  batch_size=20,
38
  )
 
39
  storage_context = StorageContext.from_defaults(vector_store=vector_store)
40
 
41
+ # Build the initial index and query/chat engines
42
+ index = VectorStoreIndex.from_documents(documents, storage_context=storage_context)
43
+ chat_memory = ChatMemoryBuffer.from_defaults(token_limit=3000)
 
 
 
 
 
 
 
 
 
 
44
  chat_engine = index.as_chat_engine(
45
  chat_mode="context",
46
+ memory=chat_memory,
47
+ system_prompt=SYSTEM_PROMPT,
 
 
 
48
  )
49
 
50
+ def process_uploaded_file(uploaded_file):
51
+ """
52
+ Process the uploaded file:
53
+ 1. Save the file to an "uploads" folder.
54
+ 2. Copy it to a temporary folder ("temp_upload") to load using SimpleDirectoryReader.
55
+ 3. Extend the global documents list and rebuild the index and chat engine.
56
+ """
57
+ if uploaded_file is None:
58
+ return "No file uploaded."
59
+
60
+ # 'uploaded_file' is a temporary file path provided by Gradio.
61
+ file_name = os.path.basename(uploaded_file)
62
+ uploads_dir = "uploads"
63
+ os.makedirs(uploads_dir, exist_ok=True)
64
+ dest_path = os.path.join(uploads_dir, file_name)
65
+ shutil.copy(uploaded_file, dest_path)
66
+
67
+ # Prepare a temporary directory to read the file
68
+ temp_dir = "temp_upload"
69
+ os.makedirs(temp_dir, exist_ok=True)
70
+ # Clear previous files in temp_dir (optional, to avoid mixing files)
71
+ for f in os.listdir(temp_dir):
72
+ os.remove(os.path.join(temp_dir, f))
73
+ shutil.copy(dest_path, temp_dir)
74
+
75
+ # Load the new document(s) from the temporary folder
76
+ new_docs = SimpleDirectoryReader(temp_dir).load_data()
77
+
78
+ # Update the global documents list and rebuild the index and chat engine
79
+ global documents, index, chat_engine
80
+ documents.extend(new_docs)
81
+ index = VectorStoreIndex.from_documents(documents, storage_context=storage_context)
82
+ chat_engine = index.as_chat_engine(
83
+ chat_mode="context",
84
+ memory=chat_memory,
85
+ system_prompt=SYSTEM_PROMPT,
86
+ )
87
+
88
+ return f"File '{file_name}' processed and added to index."
89
 
 
 
 
 
90
  def chat_with_ai(user_input, chat_history):
91
+ """
92
+ Send the user input to the chat engine and update the conversation history.
93
+ """
 
 
 
 
 
94
  response = chat_engine.chat(user_input)
95
+ # Collect reference filenames from the response (if available)
96
  references = response.source_nodes
97
+ ref = []
98
+ for node in references:
99
+ if "file_name" in node.metadata and node.metadata["file_name"] not in ref:
100
+ ref.append(node.metadata["file_name"])
101
+ # Create a complete response string with references if present
102
+ complete_response = str(response)
103
+ if ref:
104
+ complete_response += "\n\nReferences: " + ", ".join(ref)
105
+ chat_history.append((user_input, complete_response))
 
 
 
106
  return chat_history, ""
107
 
108
  def clear_history():
109
  return [], ""
110
 
111
  def gradio_chatbot():
112
+ """
113
+ Create a Gradio interface with two tabs:
114
+ - "Chat" for interacting with the chat engine.
115
+ - "Upload" for uploading new files to update the index.
116
+ """
117
  with gr.Blocks() as demo:
118
+ gr.Markdown("# Chat Interface for LlamaIndex with File Upload")
119
+
120
+ with gr.Tab("Chat"):
121
+ chatbot = gr.Chatbot(label="LlamaIndex Chatbot")
122
+ user_input = gr.Textbox(
123
+ placeholder="Ask a question...", label="Enter your question"
124
+ )
125
+ submit_button = gr.Button("Send")
126
+ btn_clear = gr.Button("Delete Context")
127
+ chat_history = gr.State([])
128
+ submit_button.click(chat_with_ai, inputs=[user_input, chat_history],
129
+ outputs=[chatbot, user_input])
130
+ user_input.submit(chat_with_ai, inputs=[user_input, chat_history],
131
+ outputs=[chatbot, user_input])
132
+ btn_clear.click(fn=clear_history, outputs=[chatbot, user_input])
133
+
134
+ with gr.Tab("Upload"):
135
+ gr.Markdown("### Upload a file to add its content to the index")
136
+ file_upload = gr.File(label="Choose a file")
137
+ upload_button = gr.Button("Upload and Process")
138
+ upload_status = gr.Textbox(label="Upload Status")
139
+ upload_button.click(process_uploaded_file, inputs=[file_upload], outputs=[upload_status])
140
+
141
  return demo
142
 
143
+ if __name__ == "__main__":
144
+ gradio_chatbot().launch(debug=True)