Spaces:
Sleeping
Sleeping
Update app.py
Browse files
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
|
| 21 |
-
|
| 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 =
|
| 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 =
|
| 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 |
-
|
| 161 |
except Exception as e:
|
| 162 |
print(f"Could not delete previous vector store {self.vector_store_id}: {e}")
|
| 163 |
|
| 164 |
-
vector_store =
|
| 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 =
|
| 173 |
file=file_obj,
|
| 174 |
purpose="assistants"
|
| 175 |
)
|
| 176 |
self.file_ids.append(file_obj.id)
|
| 177 |
-
|
| 178 |
vector_store_id=self.vector_store_id,
|
| 179 |
file_id=file_obj.id
|
| 180 |
)
|
| 181 |
|
| 182 |
-
thread =
|
| 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 |
-
|
| 195 |
thread_id=self.thread_id,
|
| 196 |
role="user",
|
| 197 |
content=question
|
| 198 |
)
|
| 199 |
|
| 200 |
-
run =
|
| 201 |
thread_id=self.thread_id,
|
| 202 |
assistant_id=self.assistant_id
|
| 203 |
)
|
| 204 |
|
| 205 |
waited = 0
|
| 206 |
while True:
|
| 207 |
-
run_status =
|
| 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 =
|
| 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 =
|
| 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 =
|
| 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"
|