Deepakkori45 commited on
Commit
a0090b7
·
verified ·
1 Parent(s): 9e4f3f8

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +281 -120
app.py CHANGED
@@ -1,168 +1,329 @@
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
- # OpenAI keys
10
- OPENAI_API_KEY = st.secrets["OPENAI_API_KEY"]
11
- # VECTOR_STORE_ID = st.secrets["VECTOR_STORE_ID"]
12
- # VECTOR_STORE_ID = os.getenv("VECTOR_STORE_ID")
 
 
 
 
 
 
 
 
 
 
13
 
14
  client = OpenAI(api_key=OPENAI_API_KEY)
15
- # vector_store_id =VECTOR_STORE_ID # Vector Store ID to use
16
- vector_store_id = "vs_sHLBqA88ng66RDZYMgaBmDuX"
17
- st.write(vector_store_id)
18
- # all_files = list(client.beta.vector_stores.files.list(vector_store_id))
19
- # for file in all_files:
20
- # # print(file)
21
- # file_id = file.id
22
- # st.write(file_id)
23
-
24
- # Set the assistant ID
25
- assistant_id = "asst_UeIHpkpVejAgrIz3RnisxSLm" # Replace with your own assistant ID
26
-
27
- def ensure_single_thread_id():
28
- if "thread_id" not in st.session_state:
29
  thread = client.beta.threads.create()
30
- st.session_state.thread_id = thread.id
31
- return st.session_state.thread_id
 
32
 
33
  def safe_message_send(prompt, thread_id):
34
  try:
35
- message = client.beta.threads.messages.create(
36
  thread_id=thread_id,
37
  role="user",
38
  content=prompt
39
  )
40
- return message
41
  except Exception as e:
42
  if "active" in str(e):
43
- print("Waiting for the current run to finish...")
44
- time.sleep(1) # wait a bit before retrying
45
- return safe_message_send(prompt, thread_id) # retry sending the message
46
  else:
47
  raise e
48
 
 
49
  def stream_generator(prompt, thread_id):
50
- # print(f'First time thread in the function {thread_id}')
51
- message = safe_message_send(prompt, thread_id) # use the new safe send function
52
-
53
- with st.spinner("Wait... Generating response..."):
54
- try:
55
- stream = client.beta.threads.runs.create(
56
- thread_id=thread_id,
57
- assistant_id=assistant_id,
58
- stream=True
59
- )
60
-
61
- for event in stream:
62
- if event.data.object == "thread.message.delta":
63
- for content in event.data.delta.content:
64
- if content.type == 'text':
65
- yield content.text.value
66
- time.sleep(0.01)
67
- elif event.data.object == "thread.run.stop":
68
- break # Break if the run stops
69
- except Exception as e:
70
- print(f"Error during streaming: {str(e)}")
71
 
72
  def upload_and_add_to_vector_store(uploaded_file):
73
- """Upload a file to OpenAI and add it to the specified vector store."""
74
  try:
75
- # Convert the uploaded file to a BytesIO stream for uploading
76
  file_stream = io.BytesIO(uploaded_file.getvalue())
77
- file_stream.name = uploaded_file.name # Preserve the file name
78
- # Upload the file to the vector store
79
- file_batch = client.beta.vector_stores.file_batches.upload_and_poll(
80
- vector_store_id=vector_store_id,
81
  files=[file_stream]
82
  )
83
-
84
-
85
- st.success(f"File '{uploaded_file.name}' processed and added to vector store. Status: {file_batch.status}")
86
  except Exception as e:
87
- st.error(f"Failed to process file: {str(e)}")
88
-
89
 
90
  def list_all_files_in_vector_store():
91
- """List all files in the specified vector store."""
92
  try:
93
- all_files = list(client.beta.vector_stores.files.list(vector_store_id=vector_store_id))
94
- # st.write(all_files)
95
- for file in all_files:
96
- file_id = file.id
97
- st.write(file_id)
98
  except Exception as e:
99
- st.error(f"Failed to list files: {str(e)}")
100
- return {}
101
 
102
- def delete_file_from_vector_store(vector_store_id, file_id):
103
- """Delete a file from the specified vector store."""
104
  try:
105
- client.beta.vector_stores.files.delete(
106
- vector_store_id=vector_store_id,
107
  file_id=file_id
108
  )
109
- st.success(f"File with ID '{file_id}' deleted from vector store '{vector_store_id}'.")
110
  except Exception as e:
111
- st.error(f"Failed to delete file. File id is not Found.")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
112
 
113
- # Interface to delete files from vector store
114
- st.sidebar.subheader("Delete File from Vector Store")
115
- file_id_to_delete = st.sidebar.text_input("Enter File ID to Delete", "")
116
- if st.sidebar.button("Delete File"):
117
- delete_file_from_vector_store(vector_store_id, file_id_to_delete)
 
 
 
118
 
119
- # Streamlit interface setup
120
- st.title("💬Chatbot")
121
- st.caption("🚀 A Streamlit Custom Chatbot")
122
 
 
 
 
 
123
 
124
- with st.sidebar:
125
- st.write("Upload PDF File")
126
- uploaded_file = st.file_uploader("Choose a file", type=['pdf', 'docx'], key='file_uploader')
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
127
 
128
- if st.button('Upload File', key='process_file'):
129
- if uploaded_file is not None:
130
- upload_and_add_to_vector_store(uploaded_file)
131
- st.success("File successfully uploaded and processed.")
132
  else:
133
- st.error("Please upload a file to process.")
 
 
 
 
 
134
 
135
- # List all uploaded files
136
- st.write("### Uploaded Files")
137
- if 'uploaded_files' in st.session_state and st.session_state.uploaded_files:
138
- for file_name, file_id in st.session_state.uploaded_files.items():
139
- st.write(f"{file_name}: {file_id}")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
140
 
141
- # List all files in the vector store
142
- st.write("## All Files in Vector Store")
143
- all_files = list_all_files_in_vector_store()
144
-
145
- # Initialize session state for chat
146
- st.session_state.start_chat = True
147
- if 'start_chat' not in st.session_state:
148
- st.session_state.start_chat = False
149
-
150
- # Main chat interface
151
- if st.session_state.start_chat:
152
- if "messages" not in st.session_state:
153
- st.session_state.messages = []
154
-
155
- for message in st.session_state.messages:
156
- with st.chat_message(message["role"]):
157
- st.markdown(message["content"])
158
-
159
- prompt = st.chat_input("Enter your message")
160
- if prompt:
161
- thread_id = ensure_single_thread_id()
162
- with st.chat_message("user"):
163
- st.markdown(prompt)
164
- st.session_state.messages.append({"role": "user", "content": prompt})
165
 
166
- with st.chat_message("assistant"):
167
- response = st.write_stream(stream_generator(prompt, thread_id))
168
- st.session_state.messages.append({"role": "assistant", "content": response})
 
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})