Skip to content

Add conversational class #60

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Feb 27, 2023
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,9 @@ sdk/diffgram/utils/__pycache__/

venv/*

diffgram.egg-info

playground.py

sdk/build/*
sdk/dist/
4 changes: 4 additions & 0 deletions sdk/diffgram/core/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
from diffgram.brain.train import Train
from diffgram.export.export import Export
from diffgram.task.task import Task
from diffgram.schema.schema import Schema
from diffgram.schema.attribute import Attribute
from requests.auth import HTTPBasicAuth


Expand Down Expand Up @@ -86,6 +88,8 @@ def __init__(

self.label_schema_list = self.get_label_schema_list()

self.schema = Schema(self)
self.attribute = Attribute(self)


def get_member_list(self):
Expand Down
115 changes: 115 additions & 0 deletions sdk/diffgram/file/conversational.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
from .compound_file import CompoundFile
from uuid import uuid4
import operator

class Conversational:
def __init__(self, project, name):
self.parent = CompoundFile(
project=project,
name=name,
directory_id=project.default_directory.id,
file_type="compound/conversational"
)
self.project = project
self.messgaes_meta = []

self.add_conversationa_attributes_if_doesnt_exist()

def add_conversationa_attributes_if_doesnt_exist(self):
default_schema = self.project.schema.default_schema()
attribute_list = self.project.attribute.list(default_schema)

message_author_attribute = None
message_time_attribute = None
message_date_attribute = None

for attribute in attribute_list['attribute_group_list']:
if attribute['name'] == 'message_author':
message_author_attribute = attribute
elif attribute['name'] == 'message_time':
message_time_attribute = attribute
elif attribute['name'] == 'message_date':
message_date_attribute = attribute

if message_author_attribute is None:
message_author_attribute = self.project.attribute.new(default_schema)
self.project.attribute.update(
message_author_attribute,
prompt="Author",
kind="text",
name="message_author",
is_global = True,
global_type = 'file',
is_read_only=True
)

if message_time_attribute is None:
message_time_attribute = self.project.attribute.new(default_schema)
self.project.attribute.update(
message_time_attribute,
prompt="Time",
kind="time",
name="message_time",
is_global = True,
global_type = 'file',
is_read_only=True
)

if message_date_attribute is None:
message_date_attribute = self.project.attribute.new(default_schema)
self.project.attribute.update(
message_date_attribute,
prompt="Date",
kind="date",
name="message_date",
is_global = True,
global_type = 'file',
is_read_only=True
)

self.author_attribute = message_author_attribute
self.time_attribute = message_time_attribute
self.date_attribute = message_date_attribute

def add_message(self, message_file, author=None, time=None, date=None):
message_meta = {
"author": author,
"time": time,
"date": date
}

self.messgaes_meta.append(message_meta)

self.parent.add_child_from_local(path=message_file, ordinal=len(self.messgaes_meta))

def _new_global_instance(self):
return {
"creation_ref_id": str(uuid4()),
"type": "global",
"attribute_groups": {}
}


def upload(self):
self.parent.upload()
child_files = self.parent.fetch_child_files()
child_files.sort(key=operator.attrgetter('id'))

for index in range(0, len(child_files)):
global_instance_for_child = self._new_global_instance()

if self.messgaes_meta[index]["author"] is not None:
global_instance_for_child["attribute_groups"][self.author_attribute["id"]] = self.messgaes_meta[index]["author"]
if self.messgaes_meta[index]["time"] is not None:
global_instance_for_child["attribute_groups"][self.time_attribute["id"]] = self.messgaes_meta[index]["time"]
if self.messgaes_meta[index]["date"] is not None:
global_instance_for_child["attribute_groups"][self.date_attribute["id"]] = self.messgaes_meta[index]["date"]

payload = {
"instance_list": [global_instance_for_child],
"and_complete": False,
"child_file_save_id": child_files[index].id
}

response = self.project.session.post(url=self.project.host + f"/api/project/{self.project.project_string_id}/file/{child_files[index].id}/annotation/update", json=payload)
self.project.handle_errors(response)
Empty file added sdk/diffgram/schema/__init__.py
Empty file.
65 changes: 65 additions & 0 deletions sdk/diffgram/schema/attribute.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
class Attribute:
def __init__(self, project):
self.project = project

def list(self, schema):
url = f'/api/v1/project/{self.project.project_string_id}/attribute/template/list'
payload = {
"schema_id": schema['id'],
"mode": "from_project"
}

response = self.project.session.post(url = self.project.host + url, json=payload)

self.project.handle_errors(response)
data = response.json()

return data

def new(self, schema):
url = f'/api/v1/project/{self.project.project_string_id}/attribute/group/new'
payload = {
"schema_id": schema['id']
}

response = self.project.session.post(url = self.project.host + url, json=payload)

self.project.handle_errors(response)
data = response.json()

return data['attribute_template_group']

def update(self,
attribute,
prompt,
kind,
name = None,
is_global = False,
label_file_list = None,
is_read_only = False,
global_type = 'file',
):
url = f'/api/v1/project/{self.project.project_string_id}/attribute/group/update'
payload = {
"group_id": attribute['id'],
"mode": "UPDATE",
"prompt": prompt,
"name": name,
"kind": kind,
"is_global": is_global,
"label_file_list": label_file_list,
"global_type": global_type,
"is_read_only": is_read_only
}

response = self.project.session.post(url = self.project.host + url, json=payload)

self.project.handle_errors(response)
data = response.json()

return data

def add_options(self, attribute, options):
pass


48 changes: 48 additions & 0 deletions sdk/diffgram/schema/schema.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
class Schema:
def __init__(self, project):
self.project = project

def list(self):
url = f'/api/v1/project/{self.project.project_string_id}/labels-schema'
response = self.project.session.get(url = self.project.host + url)
self.project.handle_errors(response)
data = response.json()

return data

def default_schema(self):
url = f'/api/v1/project/{self.project.project_string_id}/labels-schema'
response = self.project.session.get(url = self.project.host + url)
self.project.handle_errors(response)
data = response.json()

default_schema = None

for schema in data:
if schema['is_default'] == True:
default_schema = schema

return default_schema


def new(self, name):
url = f'/api/v1/project/{self.project.project_string_id}/labels-schema/new'
payload = {
"name": name
}

response = self.project.session.post(url = self.project.host + url, json=payload)

self.project.handle_errors(response)
data = response.json()

return data

def update(self, name):
#Todo
pass

def archive(self, name):
#Todo
pass