45 lines
1.4 KiB
Python
45 lines
1.4 KiB
Python
import requests
|
|
import json
|
|
import os
|
|
import sys
|
|
|
|
def get_token():
|
|
resp = requests.get("http://localhost:3015/auth-status")
|
|
if resp.status_code != 200:
|
|
raise Exception(f"Failed to get token: {resp.status_code} {resp.text}")
|
|
return resp.json()["access_token"]
|
|
|
|
def upload_file(filepath, workspace="test_workspace"):
|
|
token = get_token()
|
|
url = "http://localhost:3015/documents/upload"
|
|
headers = {
|
|
"Authorization": f"Bearer {token}",
|
|
"accept": "application/json",
|
|
}
|
|
with open(filepath, "rb") as f:
|
|
files = {"file": f}
|
|
data = {
|
|
"workspace": workspace,
|
|
"chunk_size": 1200,
|
|
"chunk_overlap": 100,
|
|
}
|
|
response = requests.post(url, headers=headers, files=files, data=data)
|
|
print(f"File: {filepath}")
|
|
print(f"Status: {response.status_code}")
|
|
print(f"Response: {response.text}")
|
|
if response.status_code != 200:
|
|
print(f"Upload failed")
|
|
else:
|
|
print("Upload succeeded")
|
|
return response.status_code == 200
|
|
|
|
if __name__ == "__main__":
|
|
files = ["test/ocr.pdf", "test/test.docx", "test/tir.docx"]
|
|
for f in files:
|
|
if not os.path.exists(f):
|
|
print(f"File {f} not found")
|
|
continue
|
|
success = upload_file(f)
|
|
if not success:
|
|
sys.exit(1)
|
|
print("All files uploaded successfully") |