DreamStream-1 commited on
Commit
9c66c5f
·
verified ·
1 Parent(s): 110fe14

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +15 -15
app.py CHANGED
@@ -17,8 +17,8 @@ api_key = os.getenv("OPENAI_API_KEY")
17
  if not api_key:
18
  raise ValueError("OPENAI_API_KEY environment variable is not set")
19
 
20
- # Initialize OpenAI client with minimal configuration
21
- client = openai.OpenAI(api_key=api_key)
22
 
23
  # Custom audio recorder component
24
  def create_audio_recorder():
@@ -136,7 +136,7 @@ class AdvancedRAG:
136
  self.assistant_id: Optional[str] = os.getenv("ASSISTANT_ID")
137
 
138
  def create_thread(self) -> str:
139
- thread = client.beta.threads.create(
140
  tool_resources={
141
  "file_search": {
142
  "vector_store_ids": [self.vector_store_id]
@@ -148,7 +148,7 @@ class AdvancedRAG:
148
 
149
  def create_vector_store(self, name: str = "My Vector Store") -> str:
150
  try:
151
- vector_store = client.vector_stores.create(name=name)
152
  self.vector_store_id = vector_store.id
153
  return self.vector_store_id
154
  except Exception as e:
@@ -157,11 +157,11 @@ class AdvancedRAG:
157
  def upload_document(self, file) -> str:
158
  if self.vector_store_id:
159
  try:
160
- client.vector_stores.delete(self.vector_store_id)
161
  except Exception as e:
162
  print(f"Could not delete previous vector store {self.vector_store_id}: {e}")
163
 
164
- vector_store = client.vector_stores.create(name="knowledge_base")
165
  self.vector_store_id = vector_store.id
166
  self.file_ids = []
167
 
@@ -169,17 +169,17 @@ class AdvancedRAG:
169
  tmp.write(file.read())
170
  tmp.flush()
171
  with open(tmp.name, "rb") as file_obj:
172
- file_obj = client.files.create(
173
  file=file_obj,
174
  purpose="assistants"
175
  )
176
  self.file_ids.append(file_obj.id)
177
- client.vector_stores.files.create(
178
  vector_store_id=self.vector_store_id,
179
  file_id=file_obj.id
180
  )
181
 
182
- thread = client.beta.threads.create(
183
  tool_resources={
184
  "file_search": {
185
  "vector_store_ids": [self.vector_store_id]
@@ -191,20 +191,20 @@ class AdvancedRAG:
191
 
192
  def ask_question(self, question: str) -> str:
193
  try:
194
- client.beta.threads.messages.create(
195
  thread_id=self.thread_id,
196
  role="user",
197
  content=question
198
  )
199
 
200
- run = client.beta.threads.runs.create(
201
  thread_id=self.thread_id,
202
  assistant_id=self.assistant_id
203
  )
204
 
205
  waited = 0
206
  while True:
207
- run_status = client.beta.threads.runs.retrieve(
208
  thread_id=self.thread_id,
209
  run_id=run.id
210
  )
@@ -217,7 +217,7 @@ class AdvancedRAG:
217
  if waited > 60:
218
  raise Exception("Run timed out after 60 seconds.")
219
 
220
- messages = client.beta.threads.messages.list(
221
  thread_id=self.thread_id,
222
  order='desc',
223
  limit=1
@@ -235,7 +235,7 @@ class AdvancedRAG:
235
  tmp.flush()
236
  tmp_path = tmp.name
237
  with open(tmp_path, "rb") as audio:
238
- transcript = client.audio.transcriptions.create(
239
  model="whisper-1",
240
  file=audio,
241
  language="en"
@@ -293,7 +293,7 @@ def process_audio_base64(audio_base64, history):
293
 
294
  # Transcribe audio
295
  with open(tmp_path, "rb") as audio_file:
296
- transcript = client.audio.transcriptions.create(
297
  model="whisper-1",
298
  file=audio_file,
299
  language="en"
 
17
  if not api_key:
18
  raise ValueError("OPENAI_API_KEY environment variable is not set")
19
 
20
+ # Initialize OpenAI client with older API syntax
21
+ openai.api_key = api_key
22
 
23
  # Custom audio recorder component
24
  def create_audio_recorder():
 
136
  self.assistant_id: Optional[str] = os.getenv("ASSISTANT_ID")
137
 
138
  def create_thread(self) -> str:
139
+ thread = openai.Thread.create(
140
  tool_resources={
141
  "file_search": {
142
  "vector_store_ids": [self.vector_store_id]
 
148
 
149
  def create_vector_store(self, name: str = "My Vector Store") -> str:
150
  try:
151
+ vector_store = openai.VectorStore.create(name=name)
152
  self.vector_store_id = vector_store.id
153
  return self.vector_store_id
154
  except Exception as e:
 
157
  def upload_document(self, file) -> str:
158
  if self.vector_store_id:
159
  try:
160
+ openai.VectorStore.delete(self.vector_store_id)
161
  except Exception as e:
162
  print(f"Could not delete previous vector store {self.vector_store_id}: {e}")
163
 
164
+ vector_store = openai.VectorStore.create(name="knowledge_base")
165
  self.vector_store_id = vector_store.id
166
  self.file_ids = []
167
 
 
169
  tmp.write(file.read())
170
  tmp.flush()
171
  with open(tmp.name, "rb") as file_obj:
172
+ file_obj = openai.File.create(
173
  file=file_obj,
174
  purpose="assistants"
175
  )
176
  self.file_ids.append(file_obj.id)
177
+ openai.VectorStore.files.create(
178
  vector_store_id=self.vector_store_id,
179
  file_id=file_obj.id
180
  )
181
 
182
+ thread = openai.Thread.create(
183
  tool_resources={
184
  "file_search": {
185
  "vector_store_ids": [self.vector_store_id]
 
191
 
192
  def ask_question(self, question: str) -> str:
193
  try:
194
+ openai.Thread.messages.create(
195
  thread_id=self.thread_id,
196
  role="user",
197
  content=question
198
  )
199
 
200
+ run = openai.Thread.runs.create(
201
  thread_id=self.thread_id,
202
  assistant_id=self.assistant_id
203
  )
204
 
205
  waited = 0
206
  while True:
207
+ run_status = openai.Thread.runs.retrieve(
208
  thread_id=self.thread_id,
209
  run_id=run.id
210
  )
 
217
  if waited > 60:
218
  raise Exception("Run timed out after 60 seconds.")
219
 
220
+ messages = openai.Thread.messages.list(
221
  thread_id=self.thread_id,
222
  order='desc',
223
  limit=1
 
235
  tmp.flush()
236
  tmp_path = tmp.name
237
  with open(tmp_path, "rb") as audio:
238
+ transcript = openai.Audio.transcriptions.create(
239
  model="whisper-1",
240
  file=audio,
241
  language="en"
 
293
 
294
  # Transcribe audio
295
  with open(tmp_path, "rb") as audio_file:
296
+ transcript = openai.Audio.transcriptions.create(
297
  model="whisper-1",
298
  file=audio_file,
299
  language="en"