Deepakkori45 commited on
Commit
990d5e1
·
verified ·
1 Parent(s): a0090b7

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +118 -277
app.py CHANGED
@@ -1,329 +1,170 @@
1
  import openai
 
2
  from openai import OpenAI
3
  import io
4
  import time
5
  import os
6
  from dotenv import load_dotenv
7
 
8
- # Load environment variables
 
9
  load_dotenv()
10
 
11
- # Securely fetch environment variables for Hugging Face Spaces
12
- def get_env_variable(name: str) -> str:
13
- value = os.environ.get(name)
14
- if value is None:
15
- raise ValueError(f"Environment variable '{name}' is not set.")
16
- return value
17
-
18
- # Initialize OpenAI client
19
- OPENAI_API_KEY = get_env_variable("OPENAI_API_KEY")
20
- VECTOR_STORE_ID = get_env_variable("VECTOR_STORE_ID")
21
- ASSISTANT_ID = get_env_variable("ASSISTANT_ID")
22
 
23
  client = OpenAI(api_key=OPENAI_API_KEY)
 
24
 
25
- # Utility Functions
26
- def ensure_single_thread_id(session_state):
27
- if "thread_id" not in session_state:
28
- thread = client.beta.threads.create()
29
- session_state.thread_id = thread.id
30
- return session_state.thread_id
 
 
31
 
 
 
 
 
 
32
 
33
  def safe_message_send(prompt, thread_id):
34
  try:
35
- return client.beta.threads.messages.create(
36
  thread_id=thread_id,
37
  role="user",
38
  content=prompt
39
  )
 
40
  except Exception as e:
41
  if "active" in str(e):
42
- time.sleep(1)
43
- return safe_message_send(prompt, thread_id)
 
44
  else:
45
  raise e
46
 
47
-
48
  def stream_generator(prompt, thread_id):
49
- safe_message_send(prompt, thread_id)
50
- try:
51
- stream = client.beta.threads.runs.create(
52
- thread_id=thread_id,
53
- assistant_id=ASSISTANT_ID,
54
- stream=True
55
- )
56
- for event in stream:
57
- if event.data.object == "thread.message.delta":
58
- for content in event.data.delta.content:
59
- if content.type == 'text':
60
- yield content.text.value
61
- time.sleep(0.01)
62
- elif event.data.object == "thread.run.stop":
63
- break
64
- except Exception as e:
65
- print(f"Streaming error: {str(e)}")
66
-
 
 
 
67
 
68
  def upload_and_add_to_vector_store(uploaded_file):
 
69
  try:
 
70
  file_stream = io.BytesIO(uploaded_file.getvalue())
71
- file_stream.name = uploaded_file.name
72
- client.vector_stores.file_batches.upload_and_poll(
73
- vector_store_id=VECTOR_STORE_ID,
 
74
  files=[file_stream]
75
  )
76
- print(f"File '{uploaded_file.name}' uploaded successfully.")
 
 
77
  except Exception as e:
78
- print(f"Upload failed: {str(e)}")
 
79
 
80
 
81
  def list_all_files_in_vector_store():
 
82
  try:
83
- all_files = client.vector_stores.files.list(vector_store_id=VECTOR_STORE_ID)
84
- for file in all_files.data:
85
- print(f"File ID: {file.id}")
 
 
86
  except Exception as e:
87
- print(f"Failed to list files: {str(e)}")
88
-
89
 
90
- def delete_file_from_vector_store(file_id):
 
91
  try:
92
- client.vector_stores.files.delete(
93
- vector_store_id=VECTOR_STORE_ID,
94
  file_id=file_id
95
  )
96
- print(f"File '{file_id}' deleted successfully.")
97
  except Exception as e:
98
- print(f"Delete failed: {str(e)}")
99
-
100
- # Simple CLI-based Interface for Testing
101
- def main():
102
- import sys
103
-
104
- session_state = {}
105
-
106
- print("Welcome to the CLI Chatbot!")
107
- while True:
108
- print("\nOptions:")
109
- print("1. Upload file")
110
- print("2. List files")
111
- print("3. Delete file")
112
- print("4. Chat")
113
- print("5. Exit")
114
- choice = input("Choose an option: ")
115
 
116
- if choice == "1":
117
- file_path = input("Enter path to PDF or DOCX file: ")
118
- try:
119
- with open(file_path, "rb") as f:
120
- class UploadedFile:
121
- def __init__(self, name, content):
122
- self.name = name
123
- self.content = content
124
 
125
- def getvalue(self):
126
- return self.content
 
127
 
128
- uploaded_file = UploadedFile(os.path.basename(file_path), f.read())
129
- upload_and_add_to_vector_store(uploaded_file)
130
- except Exception as e:
131
- print(f"File upload error: {str(e)}")
132
 
133
- elif choice == "2":
134
- list_all_files_in_vector_store()
135
-
136
- elif choice == "3":
137
- file_id = input("Enter File ID to delete: ")
138
- delete_file_from_vector_store(file_id)
139
-
140
- elif choice == "4":
141
- if "messages" not in session_state:
142
- session_state["messages"] = []
143
- user_prompt = input("You: ")
144
- session_state["messages"].append({"role": "user", "content": user_prompt})
145
- thread_id = ensure_single_thread_id(session_state)
146
- print("Assistant:", end=" ")
147
- for part in stream_generator(user_prompt, thread_id):
148
- print(part, end="", flush=True)
149
- print()
150
-
151
- elif choice == "5":
152
- sys.exit()
153
 
 
 
 
 
154
  else:
155
- print("Invalid choice, please try again.")
156
-
157
-
158
- if __name__ == "__main__":
159
- main()
160
-
161
 
162
- # import openai
163
- # import streamlit as st
164
- # from openai import OpenAI
165
- # import io
166
- # import time
167
- # import os
168
- # from dotenv import load_dotenv
169
-
170
- # # OpenAI keys
171
- # OPENAI_API_KEY = st.secrets["OPENAI_API_KEY"]
172
- # # VECTOR_STORE_ID = st.secrets["VECTOR_STORE_ID"]
173
- # # VECTOR_STORE_ID = os.getenv("VECTOR_STORE_ID")
174
-
175
- # client = OpenAI(api_key=OPENAI_API_KEY)
176
- # # vector_store_id =VECTOR_STORE_ID # Vector Store ID to use
177
- # vector_store_id = "vs_sHLBqA88ng66RDZYMgaBmDuX"
178
- # st.write(vector_store_id)
179
- # # all_files = list(client.beta.vector_stores.files.list(vector_store_id))
180
- # # for file in all_files:
181
- # # # print(file)
182
- # # file_id = file.id
183
- # # st.write(file_id)
184
-
185
- # # Set the assistant ID
186
- # assistant_id = "asst_UeIHpkpVejAgrIz3RnisxSLm" # Replace with your own assistant ID
187
-
188
- # def ensure_single_thread_id():
189
- # if "thread_id" not in st.session_state:
190
- # thread = client.beta.threads.create()
191
- # st.session_state.thread_id = thread.id
192
- # return st.session_state.thread_id
193
-
194
- # def safe_message_send(prompt, thread_id):
195
- # try:
196
- # message = client.beta.threads.messages.create(
197
- # thread_id=thread_id,
198
- # role="user",
199
- # content=prompt
200
- # )
201
- # return message
202
- # except Exception as e:
203
- # if "active" in str(e):
204
- # print("Waiting for the current run to finish...")
205
- # time.sleep(1) # wait a bit before retrying
206
- # return safe_message_send(prompt, thread_id) # retry sending the message
207
- # else:
208
- # raise e
209
-
210
- # def stream_generator(prompt, thread_id):
211
- # # print(f'First time thread in the function {thread_id}')
212
- # message = safe_message_send(prompt, thread_id) # use the new safe send function
213
-
214
- # with st.spinner("Wait... Generating response..."):
215
- # try:
216
- # stream = client.beta.threads.runs.create(
217
- # thread_id=thread_id,
218
- # assistant_id=assistant_id,
219
- # stream=True
220
- # )
221
-
222
- # for event in stream:
223
- # if event.data.object == "thread.message.delta":
224
- # for content in event.data.delta.content:
225
- # if content.type == 'text':
226
- # yield content.text.value
227
- # time.sleep(0.01)
228
- # elif event.data.object == "thread.run.stop":
229
- # break # Break if the run stops
230
- # except Exception as e:
231
- # print(f"Error during streaming: {str(e)}")
232
-
233
- # def upload_and_add_to_vector_store(uploaded_file):
234
- # """Upload a file to OpenAI and add it to the specified vector store."""
235
- # try:
236
- # # Convert the uploaded file to a BytesIO stream for uploading
237
- # file_stream = io.BytesIO(uploaded_file.getvalue())
238
- # file_stream.name = uploaded_file.name # Preserve the file name
239
- # # Upload the file to the vector store
240
- # file_batch = client.beta.vector_stores.file_batches.upload_and_poll(
241
- # vector_store_id=vector_store_id,
242
- # files=[file_stream]
243
- # )
244
-
245
-
246
- # st.success(f"File '{uploaded_file.name}' processed and added to vector store. Status: {file_batch.status}")
247
- # except Exception as e:
248
- # st.error(f"Failed to process file: {str(e)}")
249
-
250
-
251
- # def list_all_files_in_vector_store():
252
- # """List all files in the specified vector store."""
253
- # try:
254
- # all_files = list(client.beta.vector_stores.files.list(vector_store_id=vector_store_id))
255
- # # st.write(all_files)
256
- # for file in all_files:
257
- # file_id = file.id
258
- # st.write(file_id)
259
- # except Exception as e:
260
- # st.error(f"Failed to list files: {str(e)}")
261
- # return {}
262
-
263
- # def delete_file_from_vector_store(vector_store_id, file_id):
264
- # """Delete a file from the specified vector store."""
265
- # try:
266
- # client.beta.vector_stores.files.delete(
267
- # vector_store_id=vector_store_id,
268
- # file_id=file_id
269
- # )
270
- # st.success(f"File with ID '{file_id}' deleted from vector store '{vector_store_id}'.")
271
- # except Exception as e:
272
- # st.error(f"Failed to delete file. File id is not Found.")
273
-
274
- # # Interface to delete files from vector store
275
- # st.sidebar.subheader("Delete File from Vector Store")
276
- # file_id_to_delete = st.sidebar.text_input("Enter File ID to Delete", "")
277
- # if st.sidebar.button("Delete File"):
278
- # delete_file_from_vector_store(vector_store_id, file_id_to_delete)
279
-
280
- # # Streamlit interface setup
281
- # st.title("💬Chatbot")
282
- # st.caption("🚀 A Streamlit Custom Chatbot")
283
-
284
-
285
- # with st.sidebar:
286
- # st.write("Upload PDF File")
287
- # uploaded_file = st.file_uploader("Choose a file", type=['pdf', 'docx'], key='file_uploader')
288
-
289
- # if st.button('Upload File', key='process_file'):
290
- # if uploaded_file is not None:
291
- # upload_and_add_to_vector_store(uploaded_file)
292
- # st.success("File successfully uploaded and processed.")
293
- # else:
294
- # st.error("Please upload a file to process.")
295
-
296
- # # List all uploaded files
297
- # st.write("### Uploaded Files")
298
- # if 'uploaded_files' in st.session_state and st.session_state.uploaded_files:
299
- # for file_name, file_id in st.session_state.uploaded_files.items():
300
- # st.write(f"{file_name}: {file_id}")
301
 
302
- # # List all files in the vector store
303
- # st.write("## All Files in Vector Store")
304
- # all_files = list_all_files_in_vector_store()
305
-
306
- # # Initialize session state for chat
307
- # st.session_state.start_chat = True
308
- # if 'start_chat' not in st.session_state:
309
- # st.session_state.start_chat = False
310
-
311
- # # Main chat interface
312
- # if st.session_state.start_chat:
313
- # if "messages" not in st.session_state:
314
- # st.session_state.messages = []
315
-
316
- # for message in st.session_state.messages:
317
- # with st.chat_message(message["role"]):
318
- # st.markdown(message["content"])
319
-
320
- # prompt = st.chat_input("Enter your message")
321
- # if prompt:
322
- # thread_id = ensure_single_thread_id()
323
- # with st.chat_message("user"):
324
- # st.markdown(prompt)
325
- # st.session_state.messages.append({"role": "user", "content": prompt})
326
 
327
- # with st.chat_message("assistant"):
328
- # response = st.write_stream(stream_generator(prompt, thread_id))
329
- # st.session_state.messages.append({"role": "assistant", "content": response})
 
1
  import openai
2
+ import streamlit as st
3
  from openai import OpenAI
4
  import io
5
  import time
6
  import os
7
  from dotenv import load_dotenv
8
 
9
+ # Initialize the OpenAI client with your API key
10
+ # Load environment variables from the .env file
11
  load_dotenv()
12
 
13
+ # Get the OpenAI API key
14
+ OPENAI_API_KEY = os.getenv("OPENAI_API_KEY")
 
 
 
 
 
 
 
 
 
15
 
16
  client = OpenAI(api_key=OPENAI_API_KEY)
17
+ vector_store_id = "vs_sHLBqA88ng66RDZYMgaBmDuX" # Vector Store ID to use
18
 
19
+ # all_files = list(client.beta.vector_stores.files.list(vector_store_id))
20
+ # for file in all_files:
21
+ # # print(file)
22
+ # file_id = file.id
23
+ # st.write(file_id)
24
+
25
+ # Set the assistant ID
26
+ assistant_id = "asst_UeIHpkpVejAgrIz3RnisxSLm" # Replace with your own assistant ID
27
 
28
+ def ensure_single_thread_id():
29
+ if "thread_id" not in st.session_state:
30
+ thread = client.beta.threads.create()
31
+ st.session_state.thread_id = thread.id
32
+ return st.session_state.thread_id
33
 
34
  def safe_message_send(prompt, thread_id):
35
  try:
36
+ message = client.beta.threads.messages.create(
37
  thread_id=thread_id,
38
  role="user",
39
  content=prompt
40
  )
41
+ return message
42
  except Exception as e:
43
  if "active" in str(e):
44
+ print("Waiting for the current run to finish...")
45
+ time.sleep(1) # wait a bit before retrying
46
+ return safe_message_send(prompt, thread_id) # retry sending the message
47
  else:
48
  raise e
49
 
 
50
  def stream_generator(prompt, thread_id):
51
+ # print(f'First time thread in the function {thread_id}')
52
+ message = safe_message_send(prompt, thread_id) # use the new safe send function
53
+
54
+ with st.spinner("Wait... Generating response..."):
55
+ try:
56
+ stream = client.beta.threads.runs.create(
57
+ thread_id=thread_id,
58
+ assistant_id=assistant_id,
59
+ stream=True
60
+ )
61
+
62
+ for event in stream:
63
+ if event.data.object == "thread.message.delta":
64
+ for content in event.data.delta.content:
65
+ if content.type == 'text':
66
+ yield content.text.value
67
+ time.sleep(0.01)
68
+ elif event.data.object == "thread.run.stop":
69
+ break # Break if the run stops
70
+ except Exception as e:
71
+ print(f"Error during streaming: {str(e)}")
72
 
73
  def upload_and_add_to_vector_store(uploaded_file):
74
+ """Upload a file to OpenAI and add it to the specified vector store."""
75
  try:
76
+ # Convert the uploaded file to a BytesIO stream for uploading
77
  file_stream = io.BytesIO(uploaded_file.getvalue())
78
+ file_stream.name = uploaded_file.name # Preserve the file name
79
+ # Upload the file to the vector store
80
+ file_batch = client.beta.vector_stores.file_batches.upload_and_poll(
81
+ vector_store_id=vector_store_id,
82
  files=[file_stream]
83
  )
84
+
85
+
86
+ st.success(f"File '{uploaded_file.name}' processed and added to vector store. Status: {file_batch.status}")
87
  except Exception as e:
88
+ st.error(f"Failed to process file: {str(e)}")
89
+
90
 
91
 
92
  def list_all_files_in_vector_store():
93
+ """List all files in the specified vector store."""
94
  try:
95
+ all_files = list(client.beta.vector_stores.files.list(vector_store_id=vector_store_id))
96
+ # st.write(all_files)
97
+ for file in all_files:
98
+ file_id = file.id
99
+ st.write(file_id)
100
  except Exception as e:
101
+ st.error(f"Failed to list files: {str(e)}")
102
+ return {}
103
 
104
+ def delete_file_from_vector_store(vector_store_id, file_id):
105
+ """Delete a file from the specified vector store."""
106
  try:
107
+ client.beta.vector_stores.files.delete(
108
+ vector_store_id=vector_store_id,
109
  file_id=file_id
110
  )
111
+ st.success(f"File with ID '{file_id}' deleted from vector store '{vector_store_id}'.")
112
  except Exception as e:
113
+ st.error(f"Failed to delete file. File id is not Found.")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
114
 
115
+ # Interface to delete files from vector store
116
+ st.sidebar.subheader("Delete File from Vector Store")
117
+ file_id_to_delete = st.sidebar.text_input("Enter File ID to Delete", "")
118
+ if st.sidebar.button("Delete File"):
119
+ delete_file_from_vector_store(vector_store_id, file_id_to_delete)
 
 
 
120
 
121
+ # Streamlit interface setup
122
+ st.title("💬Chatbot")
123
+ st.caption("🚀 A Streamlit Custom Chatbot")
124
 
 
 
 
 
125
 
126
+ with st.sidebar:
127
+ st.write("Upload PDF File")
128
+ uploaded_file = st.file_uploader("Choose a file", type=['pdf', 'docx'], key='file_uploader')
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
129
 
130
+ if st.button('Upload File', key='process_file'):
131
+ if uploaded_file is not None:
132
+ upload_and_add_to_vector_store(uploaded_file)
133
+ st.success("File successfully uploaded and processed.")
134
  else:
135
+ st.error("Please upload a file to process.")
 
 
 
 
 
136
 
137
+ # List all uploaded files
138
+ st.write("### Uploaded Files")
139
+ if 'uploaded_files' in st.session_state and st.session_state.uploaded_files:
140
+ for file_name, file_id in st.session_state.uploaded_files.items():
141
+ st.write(f"{file_name}: {file_id}")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
142
 
143
+ # List all files in the vector store
144
+ st.write("## All Files in Vector Store")
145
+ all_files = list_all_files_in_vector_store()
146
+
147
+ # Initialize session state for chat
148
+ st.session_state.start_chat = True
149
+ if 'start_chat' not in st.session_state:
150
+ st.session_state.start_chat = False
151
+
152
+ # Main chat interface
153
+ if st.session_state.start_chat:
154
+ if "messages" not in st.session_state:
155
+ st.session_state.messages = []
156
+
157
+ for message in st.session_state.messages:
158
+ with st.chat_message(message["role"]):
159
+ st.markdown(message["content"])
160
+
161
+ prompt = st.chat_input("Enter your message")
162
+ if prompt:
163
+ thread_id = ensure_single_thread_id()
164
+ with st.chat_message("user"):
165
+ st.markdown(prompt)
166
+ st.session_state.messages.append({"role": "user", "content": prompt})
167
 
168
+ with st.chat_message("assistant"):
169
+ response = st.write_stream(stream_generator(prompt, thread_id))
170
+ st.session_state.messages.append({"role": "assistant", "content": response})