1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

PythonでWatson DiscoveryのCollectionにDocumentを追加

Last updated at Posted at 2024-11-03
# %%
from ibm_watson import DiscoveryV2
from ibm_cloud_sdk_core.authenticators import IAMAuthenticator

# %%
apikey='APIKEY'
version='VERSION'
authenticator = IAMAuthenticator(apikey=apikey)
discovery = DiscoveryV2(version=version, authenticator=authenticator)

# %%
service_url='SERVICE_URL'
discovery.set_service_url(service_url=service_url)

# %%
response = discovery.list_projects().get_result()
print(response)

# %%
projects = response['projects']
for project in projects:
    print(project)

# %%
name = 'PROJECT_NAME'
projects = [project for project in response['projects'] if project['name'] == name]
for project in projects:
    print(project)

# %%
project = projects[0]
response = discovery.list_collections(project_id=project['project_id']).get_result()
print(response)

# %%
collections = response['collections']
for collection in collections:
    print(collection)

# %%
collection = collections[0]
response = discovery.get_collection(project_id=project['project_id'], collection_id=collection['collection_id']).get_result()
print(response)

# %%
fp = 'CSV FILE PATH'
with open(file=fp, mode='rb') as file:
    response = discovery.add_document(
        project_id=project['project_id'],
        collection_id=collection['collection_id'],
        file=file,
        file_content_type='text/csv',
    ).get_result()
print(response)

# %%

とりあえずすげーでかいデータはCUIで登録しておいて、以下を参考にするのが吉な気がしている…
https://qiita.com/nax/items/9037e511c0948eb5a173

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?