HumanSignal / label-studio-sdk

Label Studio SDK
https://api.labelstud.io
Apache License 2.0
99 stars 61 forks source link

Unable to import multiple tasks via URL #74

Closed wish2023 closed 1 year ago

wish2023 commented 2 years ago

Hi, I'm working on importing multiple tasks to a project via URL:

url1 = "http://localhost:8081/synthetics5.png"
url2 = "http://localhost:8081/synthetics6.png"
data1 = {"url": url2}
data2 = [{"url": url1}, {"url": url2}]
r = ls.make_request('POST', f'/api/projects/{project_id}/import', data=data2)
print(r.status_code)

I can successfully import using data1 but get the following error using data2 :

Traceback (most recent call last):
  File "/home/user/Desktop/label-studio/api_handling.py", line 47, in <module>
    r = ls.make_request('POST', f'/api/projects/{project_id}/import', data=data2)
  File "/home/user/Desktop/label-studio/venv/lib/python3.9/site-packages/label_studio_sdk/client.py", line 254, in make_request
    response = self.session.request(method, self.get_url(url), headers=self.headers, *args, **kwargs)
  File "/home/user/Desktop/label-studio/venv/lib/python3.9/site-packages/requests/sessions.py", line 515, in request
    prep = self.prepare_request(req)
  File "/home/user/Desktop/label-studio/venv/lib/python3.9/site-packages/requests/sessions.py", line 443, in prepare_request
    p.prepare(
  File "/home/user/Desktop/label-studio/venv/lib/python3.9/site-packages/requests/models.py", line 321, in prepare
    self.prepare_body(data, files, json)
  File "/home/user/Desktop/label-studio/venv/lib/python3.9/site-packages/requests/models.py", line 517, in prepare_body
    body = self._encode_params(data)
  File "/home/user/Desktop/label-studio/venv/lib/python3.9/site-packages/requests/models.py", line 99, in _encode_params
    for k, vs in to_key_val_list(data):
ValueError: not enough values to unpack (expected 2, got 1)
KonstantinKorotaev commented 1 year ago

Hi @wish2023 Please wrap your data in json.dumps:

r = ls.make_request('POST', f'/api/projects/{project_id}/import', data= json.dumps(data2))

and add extra headers to your LS client:

headers = {'Content-type': 'application/json'}
ls = client.Client(url='http://127.0.0.1:8000/', extra_headers=headers)