Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -212,10 +212,17 @@ class AdvancedRAG:
|
|
| 212 |
|
| 213 |
def upload_document(self, file) -> str:
|
| 214 |
try:
|
|
|
|
|
|
|
|
|
|
| 215 |
# Create a new file
|
| 216 |
with tempfile.NamedTemporaryFile(delete=False, suffix=os.path.splitext(file.name)[1]) as tmp:
|
| 217 |
-
|
|
|
|
|
|
|
| 218 |
tmp.flush()
|
|
|
|
|
|
|
| 219 |
with open(tmp.name, "rb") as file_obj:
|
| 220 |
file_obj = openai.files.create(
|
| 221 |
file=file_obj,
|
|
@@ -237,6 +244,13 @@ class AdvancedRAG:
|
|
| 237 |
return file_obj.id
|
| 238 |
except Exception as e:
|
| 239 |
raise Exception(f"Error uploading document: {str(e)}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 240 |
|
| 241 |
def ask_question(self, question: str) -> str:
|
| 242 |
try:
|
|
|
|
| 212 |
|
| 213 |
def upload_document(self, file) -> str:
|
| 214 |
try:
|
| 215 |
+
if file is None:
|
| 216 |
+
raise ValueError("No file provided")
|
| 217 |
+
|
| 218 |
# Create a new file
|
| 219 |
with tempfile.NamedTemporaryFile(delete=False, suffix=os.path.splitext(file.name)[1]) as tmp:
|
| 220 |
+
# Write the file content to the temporary file
|
| 221 |
+
with open(file.name, 'rb') as f:
|
| 222 |
+
tmp.write(f.read())
|
| 223 |
tmp.flush()
|
| 224 |
+
|
| 225 |
+
# Upload the file to OpenAI
|
| 226 |
with open(tmp.name, "rb") as file_obj:
|
| 227 |
file_obj = openai.files.create(
|
| 228 |
file=file_obj,
|
|
|
|
| 244 |
return file_obj.id
|
| 245 |
except Exception as e:
|
| 246 |
raise Exception(f"Error uploading document: {str(e)}")
|
| 247 |
+
finally:
|
| 248 |
+
# Clean up temporary file
|
| 249 |
+
if 'tmp' in locals():
|
| 250 |
+
try:
|
| 251 |
+
os.unlink(tmp.name)
|
| 252 |
+
except:
|
| 253 |
+
pass
|
| 254 |
|
| 255 |
def ask_question(self, question: str) -> str:
|
| 256 |
try:
|