Skip to content

Commit 19952d9

Browse files
Merge pull request #60 from diffgram/schema
Add conversational class
2 parents d14a1ed + 739bccc commit 19952d9

File tree

6 files changed

+236
-0
lines changed

6 files changed

+236
-0
lines changed

Diff for: .gitignore

+4
Original file line numberDiff line numberDiff line change
@@ -31,5 +31,9 @@ sdk/diffgram/utils/__pycache__/
3131

3232
venv/*
3333

34+
diffgram.egg-info
35+
36+
playground.py
37+
3438
sdk/build/*
3539
sdk/dist/

Diff for: sdk/diffgram/core/core.py

+4
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
from diffgram.brain.train import Train
1717
from diffgram.export.export import Export
1818
from diffgram.task.task import Task
19+
from diffgram.schema.schema import Schema
20+
from diffgram.schema.attribute import Attribute
1921
from requests.auth import HTTPBasicAuth
2022

2123

@@ -86,6 +88,8 @@ def __init__(
8688

8789
self.label_schema_list = self.get_label_schema_list()
8890

91+
self.schema = Schema(self)
92+
self.attribute = Attribute(self)
8993

9094

9195
def get_member_list(self):

Diff for: sdk/diffgram/file/conversational.py

+115
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
from .compound_file import CompoundFile
2+
from uuid import uuid4
3+
import operator
4+
5+
class Conversational:
6+
def __init__(self, project, name):
7+
self.parent = CompoundFile(
8+
project=project,
9+
name=name,
10+
directory_id=project.default_directory.id,
11+
file_type="compound/conversational"
12+
)
13+
self.project = project
14+
self.messgaes_meta = []
15+
16+
self.add_conversationa_attributes_if_doesnt_exist()
17+
18+
def add_conversationa_attributes_if_doesnt_exist(self):
19+
default_schema = self.project.schema.default_schema()
20+
attribute_list = self.project.attribute.list(default_schema)
21+
22+
message_author_attribute = None
23+
message_time_attribute = None
24+
message_date_attribute = None
25+
26+
for attribute in attribute_list['attribute_group_list']:
27+
if attribute['name'] == 'message_author':
28+
message_author_attribute = attribute
29+
elif attribute['name'] == 'message_time':
30+
message_time_attribute = attribute
31+
elif attribute['name'] == 'message_date':
32+
message_date_attribute = attribute
33+
34+
if message_author_attribute is None:
35+
message_author_attribute = self.project.attribute.new(default_schema)
36+
self.project.attribute.update(
37+
message_author_attribute,
38+
prompt="Author",
39+
kind="text",
40+
name="message_author",
41+
is_global = True,
42+
global_type = 'file',
43+
is_read_only=True
44+
)
45+
46+
if message_time_attribute is None:
47+
message_time_attribute = self.project.attribute.new(default_schema)
48+
self.project.attribute.update(
49+
message_time_attribute,
50+
prompt="Time",
51+
kind="time",
52+
name="message_time",
53+
is_global = True,
54+
global_type = 'file',
55+
is_read_only=True
56+
)
57+
58+
if message_date_attribute is None:
59+
message_date_attribute = self.project.attribute.new(default_schema)
60+
self.project.attribute.update(
61+
message_date_attribute,
62+
prompt="Date",
63+
kind="date",
64+
name="message_date",
65+
is_global = True,
66+
global_type = 'file',
67+
is_read_only=True
68+
)
69+
70+
self.author_attribute = message_author_attribute
71+
self.time_attribute = message_time_attribute
72+
self.date_attribute = message_date_attribute
73+
74+
def add_message(self, message_file, author=None, time=None, date=None):
75+
message_meta = {
76+
"author": author,
77+
"time": time,
78+
"date": date
79+
}
80+
81+
self.messgaes_meta.append(message_meta)
82+
83+
self.parent.add_child_from_local(path=message_file, ordinal=len(self.messgaes_meta))
84+
85+
def _new_global_instance(self):
86+
return {
87+
"creation_ref_id": str(uuid4()),
88+
"type": "global",
89+
"attribute_groups": {}
90+
}
91+
92+
93+
def upload(self):
94+
self.parent.upload()
95+
child_files = self.parent.fetch_child_files()
96+
child_files.sort(key=operator.attrgetter('id'))
97+
98+
for index in range(0, len(child_files)):
99+
global_instance_for_child = self._new_global_instance()
100+
101+
if self.messgaes_meta[index]["author"] is not None:
102+
global_instance_for_child["attribute_groups"][self.author_attribute["id"]] = self.messgaes_meta[index]["author"]
103+
if self.messgaes_meta[index]["time"] is not None:
104+
global_instance_for_child["attribute_groups"][self.time_attribute["id"]] = self.messgaes_meta[index]["time"]
105+
if self.messgaes_meta[index]["date"] is not None:
106+
global_instance_for_child["attribute_groups"][self.date_attribute["id"]] = self.messgaes_meta[index]["date"]
107+
108+
payload = {
109+
"instance_list": [global_instance_for_child],
110+
"and_complete": False,
111+
"child_file_save_id": child_files[index].id
112+
}
113+
114+
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)
115+
self.project.handle_errors(response)

Diff for: sdk/diffgram/schema/__init__.py

Whitespace-only changes.

Diff for: sdk/diffgram/schema/attribute.py

+65
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
class Attribute:
2+
def __init__(self, project):
3+
self.project = project
4+
5+
def list(self, schema):
6+
url = f'/api/v1/project/{self.project.project_string_id}/attribute/template/list'
7+
payload = {
8+
"schema_id": schema['id'],
9+
"mode": "from_project"
10+
}
11+
12+
response = self.project.session.post(url = self.project.host + url, json=payload)
13+
14+
self.project.handle_errors(response)
15+
data = response.json()
16+
17+
return data
18+
19+
def new(self, schema):
20+
url = f'/api/v1/project/{self.project.project_string_id}/attribute/group/new'
21+
payload = {
22+
"schema_id": schema['id']
23+
}
24+
25+
response = self.project.session.post(url = self.project.host + url, json=payload)
26+
27+
self.project.handle_errors(response)
28+
data = response.json()
29+
30+
return data['attribute_template_group']
31+
32+
def update(self,
33+
attribute,
34+
prompt,
35+
kind,
36+
name = None,
37+
is_global = False,
38+
label_file_list = None,
39+
is_read_only = False,
40+
global_type = 'file',
41+
):
42+
url = f'/api/v1/project/{self.project.project_string_id}/attribute/group/update'
43+
payload = {
44+
"group_id": attribute['id'],
45+
"mode": "UPDATE",
46+
"prompt": prompt,
47+
"name": name,
48+
"kind": kind,
49+
"is_global": is_global,
50+
"label_file_list": label_file_list,
51+
"global_type": global_type,
52+
"is_read_only": is_read_only
53+
}
54+
55+
response = self.project.session.post(url = self.project.host + url, json=payload)
56+
57+
self.project.handle_errors(response)
58+
data = response.json()
59+
60+
return data
61+
62+
def add_options(self, attribute, options):
63+
pass
64+
65+

Diff for: sdk/diffgram/schema/schema.py

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
class Schema:
2+
def __init__(self, project):
3+
self.project = project
4+
5+
def list(self):
6+
url = f'/api/v1/project/{self.project.project_string_id}/labels-schema'
7+
response = self.project.session.get(url = self.project.host + url)
8+
self.project.handle_errors(response)
9+
data = response.json()
10+
11+
return data
12+
13+
def default_schema(self):
14+
url = f'/api/v1/project/{self.project.project_string_id}/labels-schema'
15+
response = self.project.session.get(url = self.project.host + url)
16+
self.project.handle_errors(response)
17+
data = response.json()
18+
19+
default_schema = None
20+
21+
for schema in data:
22+
if schema['is_default'] == True:
23+
default_schema = schema
24+
25+
return default_schema
26+
27+
28+
def new(self, name):
29+
url = f'/api/v1/project/{self.project.project_string_id}/labels-schema/new'
30+
payload = {
31+
"name": name
32+
}
33+
34+
response = self.project.session.post(url = self.project.host + url, json=payload)
35+
36+
self.project.handle_errors(response)
37+
data = response.json()
38+
39+
return data
40+
41+
def update(self, name):
42+
#Todo
43+
pass
44+
45+
def archive(self, name):
46+
#Todo
47+
pass
48+

0 commit comments

Comments
 (0)