Spaces:
Sleeping
Sleeping
Commit
·
dc023a7
1
Parent(s):
0d7e65e
Resolved CorsError 2
Browse files
api.py
CHANGED
|
@@ -46,7 +46,7 @@ async def options_upload():
|
|
| 46 |
return JSONResponse(
|
| 47 |
content={"status": "ok"},
|
| 48 |
headers={
|
| 49 |
-
"Access-Control-Allow-Origin": "https://chat-docx-ai-vercel.
|
| 50 |
"Access-Control-Allow-Methods": "POST, OPTIONS",
|
| 51 |
"Access-Control-Allow-Headers": "Content-Type, Authorization",
|
| 52 |
},
|
|
@@ -55,12 +55,36 @@ async def options_upload():
|
|
| 55 |
|
| 56 |
@app.post("/upload")
|
| 57 |
async def upload(files: List[UploadFile] = File(...)):
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 64 |
|
| 65 |
@app.post("/ask")
|
| 66 |
async def ask(
|
|
|
|
| 46 |
return JSONResponse(
|
| 47 |
content={"status": "ok"},
|
| 48 |
headers={
|
| 49 |
+
"Access-Control-Allow-Origin": "https://chat-docx-ai-vercel.app",
|
| 50 |
"Access-Control-Allow-Methods": "POST, OPTIONS",
|
| 51 |
"Access-Control-Allow-Headers": "Content-Type, Authorization",
|
| 52 |
},
|
|
|
|
| 55 |
|
| 56 |
@app.post("/upload")
|
| 57 |
async def upload(files: List[UploadFile] = File(...)):
|
| 58 |
+
headers = {
|
| 59 |
+
"Access-Control-Allow-Origin": "https://chat-docx-ai-vercel.app"
|
| 60 |
+
}
|
| 61 |
+
try:
|
| 62 |
+
if not files:
|
| 63 |
+
return JSONResponse(
|
| 64 |
+
content={"status": "error", "message": "No files uploaded."},
|
| 65 |
+
status_code=400,
|
| 66 |
+
headers=headers
|
| 67 |
+
)
|
| 68 |
+
|
| 69 |
+
print("Starting document processing...")
|
| 70 |
+
raw_docs = load_documents_gradio(files)
|
| 71 |
+
print("Documents loaded. Splitting documents...")
|
| 72 |
+
chunks = split_documents(raw_docs)
|
| 73 |
+
print("Documents split. Building vector store...")
|
| 74 |
+
store["value"] = build_vectorstore(chunks)
|
| 75 |
+
print("Vector store built successfully.")
|
| 76 |
+
|
| 77 |
+
return JSONResponse(
|
| 78 |
+
content={"status": "success", "message": "Document processed successfully! You can now ask questions."},
|
| 79 |
+
headers=headers
|
| 80 |
+
)
|
| 81 |
+
except Exception as e:
|
| 82 |
+
print(f"An error occurred during upload: {e}")
|
| 83 |
+
return JSONResponse(
|
| 84 |
+
content={"status": "error", "message": f"An internal server error occurred: {e}"},
|
| 85 |
+
status_code=500,
|
| 86 |
+
headers=headers
|
| 87 |
+
)
|
| 88 |
|
| 89 |
@app.post("/ask")
|
| 90 |
async def ask(
|