Skip to content

Commit 3ab83e9

Browse files
committed
TableQA changes
1 parent aff8e23 commit 3ab83e9

File tree

150 files changed

+2050
-355
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

150 files changed

+2050
-355
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
*.pyc

example_Stemmer.py

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
from firstlanguage_python.firstlanguage_client import Client
2+
from firstlanguage_python.configuration import Environment
3+
import jsonpickle
4+
5+
6+
client = Client(
7+
apikey='63af276e-34fe-4fd5-a474-0c69847f25e1',
8+
environment=Environment.PRODUCTION,)
9+
10+
reqbody='{"input":{"text":"அவள் வேகமாக ஓடினாள்","lang":"ta"} }'
11+
12+
body = jsonpickle.decode(reqbody)
13+
basic_api_controller = client.basic_api
14+
15+
advanced_api_controller = client.advanced_api
16+
17+
result = basic_api_controller.get_stemmer(body)
18+
19+
for res in result:
20+
print("Original Text passed: "+res.orginal_text)
21+
print("Stemmed result: "+res.stem)
22+
23+
reqbody='{"input":{"text":"Welcome to Google and FirstLanguage","lang":"en"} }'
24+
25+
body = jsonpickle.decode(reqbody)
26+
27+
result = advanced_api_controller.get_translate(body)
28+
29+
print(result)

examples/example_Stemmer.py

+10
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,18 @@
1212
body = jsonpickle.decode(reqbody)
1313
basic_api_controller = client.basic_api
1414

15+
advanced_api_controller = client.advanced_api
16+
1517
result = basic_api_controller.get_stemmer(body)
1618

1719
for res in result:
1820
print("Original Text passed: "+res.orginal_text)
1921
print("Stemmed result: "+res.stem)
22+
23+
reqbody='{"input":{"text":"Welcome to Google and FirstLanguage","lang":"ta"} }'
24+
25+
body = jsonpickle.decode(reqbody)
26+
27+
result = advanced_api_controller.get_translate(body)
28+
29+
print(result)
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.

firstlanguage_python/api_helper.py

+32-36
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# -*- coding: utf-8 -*-
22

33
"""
4-
firstlanguage
4+
firstlanguage_python
55
66
This file was automatically generated by APIMATIC v3.0 (
77
https://www.apimatic.io ).
@@ -142,13 +142,14 @@ def get_schema_path(path):
142142
return path
143143

144144
@staticmethod
145-
def serialize_array(key, array, formatting="indexed"):
145+
def serialize_array(key, array, formatting="indexed", is_query=False):
146146
"""Converts an array parameter to a list of key value tuples.
147147
148148
Args:
149149
key (str): The name of the parameter.
150150
array (list): The value of the parameter.
151151
formatting (str): The type of key formatting expected.
152+
is_query (bool): Decides if the parameters are for query or form.
152153
153154
Returns:
154155
list: A list with key value tuples for the array elements.
@@ -168,6 +169,16 @@ def serialize_array(key, array, formatting="indexed"):
168169
tuples += [("{0}[{1}]".format(key, index), element) for index, element in enumerate(array)]
169170
elif formatting == "plain":
170171
tuples += [(key, element) for element in array]
172+
elif is_query:
173+
if formatting == "csv":
174+
tuples += [(key, ",".join(str(x) for x in array))]
175+
176+
elif formatting == "psv":
177+
tuples += [(key, "|".join(str(x) for x in array))]
178+
179+
elif formatting == "tsv":
180+
tuples += [(key, "\t".join(str(x) for x in array))]
181+
171182
else:
172183
raise ValueError("Invalid format provided.")
173184
else:
@@ -231,41 +242,25 @@ def append_url_with_query_parameters(url,
231242
raise ValueError("URL is None.")
232243
if parameters is None:
233244
return url
234-
235-
for key, value in parameters.items():
245+
parameters = APIHelper.process_complex_types_parameters(parameters, array_serialization)
246+
for index, value in enumerate(parameters):
247+
key = value[0]
248+
val = value[1]
236249
seperator = '&' if '?' in url else '?'
237250
if value is not None:
238-
if isinstance(value, list):
239-
value = [element for element in value if element]
240-
if array_serialization == "csv":
241-
url += "{0}{1}={2}".format(
242-
seperator,
243-
key,
244-
",".join(quote(str(x), safe='') for x in value)
245-
)
246-
elif array_serialization == "psv":
247-
url += "{0}{1}={2}".format(
248-
seperator,
249-
key,
250-
"|".join(quote(str(x), safe='') for x in value)
251-
)
252-
elif array_serialization == "tsv":
253-
url += "{0}{1}={2}".format(
254-
seperator,
255-
key,
256-
"\t".join(quote(str(x), safe='') for x in value)
257-
)
258-
else:
259-
url += "{0}{1}".format(
260-
seperator,
261-
"&".join(("{0}={1}".format(k, quote(str(v), safe='')))
262-
for k, v in APIHelper.serialize_array(key, value, array_serialization))
263-
)
264-
else:
265-
url += "{0}{1}={2}".format(seperator, key, quote(str(value), safe=''))
251+
url += "{0}{1}={2}".format(seperator, key, quote(str(val), safe=''))
266252

267253
return url
268254

255+
@staticmethod
256+
def process_complex_types_parameters(query_parameters, array_serialization):
257+
processed_params = []
258+
for key, value in query_parameters.items():
259+
processed_params.extend(
260+
APIHelper.form_encode(value, key, array_serialization=array_serialization, is_query=True))
261+
return processed_params
262+
263+
269264
@staticmethod
270265
def clean_url(url):
271266
"""Validates and processes the given query Url to clean empty slashes.
@@ -315,14 +310,15 @@ def form_encode_parameters(form_parameters,
315310
@staticmethod
316311
def form_encode(obj,
317312
instance_name,
318-
array_serialization="indexed"):
313+
array_serialization="indexed", is_query=False):
319314
"""Encodes a model in a form-encoded manner such as person[Name]
320315
321316
Args:
322317
obj (object): The given Object to form encode.
323318
instance_name (string): The base name to appear before each entry
324319
for this object.
325320
array_serialization (string): The format of array parameter serialization.
321+
is_query (bool): Decides if the parameters are for query or form.
326322
327323
Returns:
328324
dict: A dictionary of form encoded properties of the model.
@@ -336,11 +332,11 @@ def form_encode(obj,
336332
if obj is None:
337333
return []
338334
elif isinstance(obj, list):
339-
for element in APIHelper.serialize_array(instance_name, obj, array_serialization):
340-
retval += APIHelper.form_encode(element[1], element[0], array_serialization)
335+
for element in APIHelper.serialize_array(instance_name, obj, array_serialization, is_query):
336+
retval += APIHelper.form_encode(element[1], element[0], array_serialization, is_query)
341337
elif isinstance(obj, dict):
342338
for item in obj:
343-
retval += APIHelper.form_encode(obj[item], instance_name + "[" + item + "]", array_serialization)
339+
retval += APIHelper.form_encode(obj[item], instance_name + "[" + item + "]", array_serialization, is_query)
344340
else:
345341
retval.append((instance_name, obj))
346342

firstlanguage_python/configuration.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# -*- coding: utf-8 -*-
22

33
"""
4-
firstlanguage
4+
firstlanguage_python
55
66
This file was automatically generated by APIMATIC v3.0 (
77
https://www.apimatic.io ).
@@ -72,7 +72,7 @@ def __init__(
7272
backoff_factor=2,
7373
retry_statuses=[408, 413, 429, 500, 502, 503, 504, 521, 522, 524],
7474
retry_methods=['GET', 'PUT'], environment=Environment.PRODUCTION,
75-
apikey='BHbLvTKeT4y9PACVomcGBF3GrCv1OUOc'
75+
apikey='TODO: Replace'
7676
):
7777
# The Http Client passed from the sdk user for making requests
7878
self._http_client_instance = http_client_instance
@@ -100,7 +100,7 @@ def __init__(
100100
# Current API environment
101101
self._environment = environment
102102

103-
# API Key can be copied from your dashboard
103+
# TODO: Replace
104104
self._apikey = apikey
105105

106106
# The Http Client to use for making requests.

firstlanguage_python/controllers/__init__.py

+1
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,5 @@
22
'base_controller',
33
'basic_ap_is_controller',
44
'advanced_ap_is_controller',
5+
'enterprise_only_controller',
56
]
Binary file not shown.
Binary file not shown.
Binary file not shown.

firstlanguage_python/controllers/advanced_api_controller.py

+106-12
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11
# -*- coding: utf-8 -*-
22

33
"""
4-
firstlanguage
4+
firstlanguage_python
55
66
This file was automatically generated by APIMATIC v3.0 (
77
https://www.apimatic.io ).
88
"""
99

1010
from firstlanguage_python.api_helper import APIHelper
1111
from firstlanguage_python.configuration import Server
12-
from firstlanguage_python.controllers.base_controller import BaseController
13-
from firstlanguage_python.http.auth.custom_header_auth import CustomHeaderAuth
12+
from firstlanguage_python.controllers.base_controller import BaseController
1413
from firstlanguage_python.models.responseclassify import Responseclassify
1514
from firstlanguage_python.models.api_qa_response import ApiQaResponse
15+
from firstlanguage_python.models.api_tableqa_response import ApiTableqaResponse
1616
from firstlanguage_python.models.api_ner_response import ApiNerResponse
1717
from firstlanguage_python.models.api_summary_response import ApiSummaryResponse
1818
from firstlanguage_python.models.api_translate_response import ApiTranslateResponse
@@ -24,10 +24,9 @@
2424

2525
class AdvancedAPIsController(BaseController):
2626

27-
"""A Controller to access Endpoints in the firstlanguage API."""
28-
29-
def __init__(self, config, call_back=None):
30-
super(AdvancedAPIsController, self).__init__(config, call_back)
27+
"""A Controller to access Endpoints in the firstlanguage_python API."""
28+
def __init__(self, config, auth_managers, call_back=None):
29+
super(AdvancedAPIsController, self).__init__(config, auth_managers, call_back)
3130

3231
def get_classification(self,
3332
body):
@@ -86,7 +85,9 @@ def get_classification(self,
8685

8786
# Prepare and execute request
8887
_request = self.config.http_client.post(_query_url, headers=_headers, parameters=APIHelper.json_serialize(body))
89-
CustomHeaderAuth.apply(self.config, _request)
88+
# Apply authentication scheme on request
89+
self.apply_auth_schemes(_request, 'global')
90+
9091
_response = self.execute_request(_request)
9192

9293
# Endpoint and global error handling using HTTP status codes.
@@ -147,7 +148,9 @@ def get_qa(self,
147148

148149
# Prepare and execute request
149150
_request = self.config.http_client.post(_query_url, headers=_headers, parameters=APIHelper.json_serialize(body))
150-
CustomHeaderAuth.apply(self.config, _request)
151+
# Apply authentication scheme on request
152+
self.apply_auth_schemes(_request, 'global')
153+
151154
_response = self.execute_request(_request)
152155

153156
# Endpoint and global error handling using HTTP status codes.
@@ -163,6 +166,91 @@ def get_qa(self,
163166

164167
return decoded
165168

169+
def get_table_qa(self,
170+
body=None):
171+
"""Does a POST request to /api/tableqa.
172+
173+
# TableQA : Defintion and it's usage
174+
Table QA uses TAPAS based model to get answers from table input. The
175+
table can be parsed into a JSON object or it can be a link to a CSV
176+
file. Currently only Plain CSV file is supported.
177+
This API works only for English language currently
178+
Example for flattend table in JSON Format:<br/>
179+
{"Cities": ["Paris, France", "London, England", "Lyon, France"],
180+
"Inhabitants": ["2.161", "8.982", "0.513"]}
181+
The API can return the original table rows from which the answer was
182+
extracted based on the flag 'sendBackRows'
183+
184+
Args:
185+
body (object, optional): Add a JSON Input as per the schema
186+
defined below. For URL input, if you are providing Google
187+
drive or Google Spreadsheet url ensure that you provide a link
188+
which can download the file directly and not the share link.
189+
Example: For Google Spreadsheet, the url format will be like
190+
below:
191+
https://docs.google.com/spreadsheets/d/1TtzPAHqpaTB7Ltdq0zwZ8Fa
192+
mF7O9aC4KH4EpmwI/export?format=csv&gid=151344200 Or for
193+
Google Drive, it will be like below:
194+
https://drive.google.com/uc?id=idofthefile For Flat table
195+
input check the example out.
196+
197+
Returns:
198+
list of ApiTableqaResponse: Response from the API. Answers for the
199+
questions posted along with the original rows if the flag is
200+
true.
201+
202+
The anwer will be of format:
203+
204+
AGGREGRATE >
205+
answer
206+
207+
Aggregrate can be one of the
208+
following:
209+
NONE,
210+
SUM,
211+
COUNT,
212+
AVERAGE
213+
214+
Raises:
215+
APIException: When an error occurs while fetching the data from
216+
the remote API. This exception includes the HTTP Response
217+
code, an error message, and the HTTP body that was received in
218+
the request.
219+
220+
"""
221+
222+
# Prepare query URL
223+
_url_path = '/api/tableqa'
224+
_query_builder = self.config.get_base_uri()
225+
_query_builder += _url_path
226+
_query_url = APIHelper.clean_url(_query_builder)
227+
228+
# Prepare headers
229+
_headers = {
230+
'accept': 'application/json',
231+
'Content-Type': 'application/json'
232+
}
233+
234+
# Prepare and execute request
235+
_request = self.config.http_client.post(_query_url, headers=_headers, parameters=APIHelper.json_serialize(body))
236+
# Apply authentication scheme on request
237+
self.apply_auth_schemes(_request, 'global')
238+
239+
_response = self.execute_request(_request)
240+
241+
# Endpoint and global error handling using HTTP status codes.
242+
if _response.status_code == 400:
243+
raise ErrorsException('Bad Request', _response)
244+
elif _response.status_code == 426:
245+
raise M426ErrorException('Please use HTTPS protocol', _response)
246+
elif _response.status_code == 429:
247+
raise APIException('Too Many Requests', _response)
248+
self.validate_response(_response)
249+
250+
decoded = APIHelper.json_deserialize(_response.text, ApiTableqaResponse.from_dictionary)
251+
252+
return decoded
253+
166254
def get_ner(self,
167255
body=None):
168256
"""Does a POST request to /api/ner.
@@ -221,7 +309,9 @@ def get_ner(self,
221309

222310
# Prepare and execute request
223311
_request = self.config.http_client.post(_query_url, headers=_headers, parameters=APIHelper.json_serialize(body))
224-
CustomHeaderAuth.apply(self.config, _request)
312+
# Apply authentication scheme on request
313+
self.apply_auth_schemes(_request, 'global')
314+
225315
_response = self.execute_request(_request)
226316

227317
# Endpoint and global error handling using HTTP status codes.
@@ -319,7 +409,9 @@ def get_summary(self,
319409

320410
# Prepare and execute request
321411
_request = self.config.http_client.post(_query_url, headers=_headers, parameters=APIHelper.json_serialize(body))
322-
CustomHeaderAuth.apply(self.config, _request)
412+
# Apply authentication scheme on request
413+
self.apply_auth_schemes(_request, 'global')
414+
323415
_response = self.execute_request(_request)
324416

325417
# Endpoint and global error handling using HTTP status codes.
@@ -432,7 +524,9 @@ def get_translate(self,
432524

433525
# Prepare and execute request
434526
_request = self.config.http_client.post(_query_url, headers=_headers, parameters=APIHelper.json_serialize(body))
435-
CustomHeaderAuth.apply(self.config, _request)
527+
# Apply authentication scheme on request
528+
self.apply_auth_schemes(_request, 'global')
529+
436530
_response = self.execute_request(_request)
437531

438532
# Endpoint and global error handling using HTTP status codes.

0 commit comments

Comments
 (0)