Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -78,53 +78,33 @@ tavily_search = TavilySearchResults(max_results=4)
|
|
| 78 |
|
| 79 |
@app.post("/uploadpdfs")
|
| 80 |
async def upload_file(file: UploadFile = File(...)):
|
| 81 |
-
#
|
| 82 |
-
contents = await file.read()
|
| 83 |
-
file_stream = BytesIO(contents)
|
| 84 |
|
| 85 |
-
#
|
| 86 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 87 |
parser = SentenceSplitter(chunk_size=300, chunk_overlap=50)
|
| 88 |
nodes = parser.get_nodes_from_documents(documents)
|
| 89 |
|
| 90 |
# Create or update vector index
|
| 91 |
embed_model = HuggingFaceEmbedding(model_name="WhereIsAI/UAE-Large-V1")
|
|
|
|
| 92 |
if not hasattr(app.state, "vector_index") or app.state.vector_index is None:
|
| 93 |
app.state.vector_index = VectorStoreIndex(nodes, embed_model=embed_model)
|
| 94 |
-
message = "New vector index created and file
|
| 95 |
else:
|
| 96 |
app.state.vector_index.insert_nodes(nodes)
|
| 97 |
-
message = "File
|
| 98 |
|
| 99 |
return {"message": message, "filename": file.filename}
|
| 100 |
-
# async def upload_file(file: UploadFile = File(...)):
|
| 101 |
-
# # global vector_index
|
| 102 |
-
|
| 103 |
-
# # Save uploaded file to a temp directory
|
| 104 |
-
# temp_dir = "temp_uploads"
|
| 105 |
-
# os.makedirs(temp_dir, exist_ok=True)
|
| 106 |
-
# file_id = str(uuid.uuid4())
|
| 107 |
-
# file_path = os.path.join(temp_dir, f"{file_id}_{file.filename}")
|
| 108 |
-
|
| 109 |
-
# with open(file_path, "wb") as f:
|
| 110 |
-
# shutil.copyfileobj(file.file, f)
|
| 111 |
-
|
| 112 |
-
# # Load and parse document
|
| 113 |
-
# documents = SimpleDirectoryReader(input_files=[file_path]).load_data()
|
| 114 |
-
# parser = SentenceSplitter(chunk_size=300, chunk_overlap=50)
|
| 115 |
-
# nodes = parser.get_nodes_from_documents(documents)
|
| 116 |
-
|
| 117 |
-
# # Create or update vector index
|
| 118 |
-
# embed_model = HuggingFaceEmbedding(model_name="WhereIsAI/UAE-Large-V1")
|
| 119 |
-
# # if vector_index is None:
|
| 120 |
-
# if not hasattr(app.state, "vector_index") or app.state.vector_index is None:
|
| 121 |
-
# app.state.vector_index = VectorStoreIndex(nodes, embed_model=embed_model)
|
| 122 |
-
# message = "New vector index created and file stored."
|
| 123 |
-
# else:
|
| 124 |
-
# app.state.vector_index.insert_nodes(nodes)
|
| 125 |
-
# message = "File stored and vector index updated."
|
| 126 |
-
|
| 127 |
-
# return {"message": message, "filename": file.filename}
|
| 128 |
|
| 129 |
|
| 130 |
class QueryRequest(BaseModel):
|
|
|
|
| 78 |
|
| 79 |
@app.post("/uploadpdfs")
|
| 80 |
async def upload_file(file: UploadFile = File(...)):
|
| 81 |
+
# global vector_index
|
|
|
|
|
|
|
| 82 |
|
| 83 |
+
# Save uploaded file to a temp directory
|
| 84 |
+
temp_dir = "temp_uploads"
|
| 85 |
+
os.makedirs(temp_dir, exist_ok=True)
|
| 86 |
+
file_id = str(uuid.uuid4())
|
| 87 |
+
file_path = os.path.join(temp_dir, f"{file_id}_{file.filename}")
|
| 88 |
+
|
| 89 |
+
with open(file_path, "wb") as f:
|
| 90 |
+
shutil.copyfileobj(file.file, f)
|
| 91 |
+
|
| 92 |
+
# Load and parse document
|
| 93 |
+
documents = SimpleDirectoryReader(input_files=[file_path]).load_data()
|
| 94 |
parser = SentenceSplitter(chunk_size=300, chunk_overlap=50)
|
| 95 |
nodes = parser.get_nodes_from_documents(documents)
|
| 96 |
|
| 97 |
# Create or update vector index
|
| 98 |
embed_model = HuggingFaceEmbedding(model_name="WhereIsAI/UAE-Large-V1")
|
| 99 |
+
# if vector_index is None:
|
| 100 |
if not hasattr(app.state, "vector_index") or app.state.vector_index is None:
|
| 101 |
app.state.vector_index = VectorStoreIndex(nodes, embed_model=embed_model)
|
| 102 |
+
message = "New vector index created and file stored."
|
| 103 |
else:
|
| 104 |
app.state.vector_index.insert_nodes(nodes)
|
| 105 |
+
message = "File stored and vector index updated."
|
| 106 |
|
| 107 |
return {"message": message, "filename": file.filename}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 108 |
|
| 109 |
|
| 110 |
class QueryRequest(BaseModel):
|