<commit_before>import httplib2
from django.conf import settings
from oauth2client.client import SignedJwtAssertionCredentials
from apiclient import discovery

class ModelManager:

	def get_model(self, model):
		if not hasattr(self, 'api'):
			self.authorize()
		return HostedModel(self.api, model)

	# TODO Change this function
	def get_model_tmp(self, project, model):
		if not hasattr(self, 'api'):
			self.authorize()
		return TrainedModel(self.api, project, model)

	@classmethod
	def authorize(self):
		# TODO Validate credentials
		private_key = open(settings.GOOGLE_PREDICTION_PRIVATE_KEY).read()
		http = httplib2.Http()

		credentials = SignedJwtAssertionCredentials(
			 	settings.GOOGLE_PREDICTION_PROJECT_EMAIL,
			 	private_key,
			 	[
			      'https://www.googleapis.com/auth/devstorage.full_control',
			      'https://www.googleapis.com/auth/devstorage.read_only',
			      'https://www.googleapis.com/auth/devstorage.read_write',
			      'https://www.googleapis.com/auth/prediction',
			    ]
			)

		credentials.authorize(http)
		ModelManager.api = discovery.build('prediction', 'v1.6', http=http)

class HostedModel:
	HOSTED_PROJECT_ID = 414649711441

	models = ModelManager().get_model

	def __init__(self, api, model):
		self.api = api
		self.model = model

	def predict(self, inputData):
		# TODO check for lists in input
		body = {'input': {'csvInstance': [inputData]}}
		return ModelManager.api.hostedmodels().predict(
			project=self.HOSTED_PROJECT_ID,
			hostedModelName=self.model,
			body=body
		).execute()


class TrainedModel:

	PROJECT_ID = None

	# TODO Change function structure
	models = ModelManager().get_model_tmp

	def __init__(self, api, project, model):
		self.api, self.PROJECT_ID, self.model = api, project, model

	def predict(self, inputData):
		if not isinstance(inputData, (list, tuple)):
			inputData = [inputData]

		body = {'input': {'csvInstance': inputData}}
		return ModelManager.api.trainedmodels().predict(
			project=self.PROJECT_ID,
			id=self.model,
			body=body
		).execute()
<commit_msg>Add list() to TrainedModel class<commit_after>import httplib2
from django.conf import settings
from oauth2client.client import SignedJwtAssertionCredentials
from apiclient import discovery

class ModelManager:

	def get_model(self, model):
		if not hasattr(self, 'api'):
			self.authorize()
		return HostedModel(self.api, model)

	# TODO Change this function
	def get_model_tmp(self, project, model):
		if not hasattr(self, 'api'):
			self.authorize()
		return TrainedModel(self.api, project, model)

	@classmethod
	def authorize(self):
		# TODO Validate credentials
		private_key = open(settings.GOOGLE_PREDICTION_PRIVATE_KEY).read()
		http = httplib2.Http()

		credentials = SignedJwtAssertionCredentials(
			 	settings.GOOGLE_PREDICTION_PROJECT_EMAIL,
			 	private_key,
			 	[
			      'https://www.googleapis.com/auth/devstorage.full_control',
			      'https://www.googleapis.com/auth/devstorage.read_only',
			      'https://www.googleapis.com/auth/devstorage.read_write',
			      'https://www.googleapis.com/auth/prediction',
			    ]
			)

		credentials.authorize(http)
		ModelManager.api = discovery.build('prediction', 'v1.6', http=http)

class HostedModel:
	HOSTED_PROJECT_ID = 414649711441

	models = ModelManager().get_model

	def __init__(self, api, model):
		self.api = api
		self.model = model

	def predict(self, inputData):
		# TODO check for lists in input
		body = {'input': {'csvInstance': [inputData]}}
		return ModelManager.api.hostedmodels().predict(
			project=self.HOSTED_PROJECT_ID,
			hostedModelName=self.model,
			body=body
		).execute()


class TrainedModel:

	PROJECT_ID = None

	# TODO Change function structure
	models = ModelManager().get_model_tmp

	def __init__(self, api, project, model):
		self.api, self.PROJECT_ID, self.model = api, project, model

	def list(self):
		return ModelManager.api.trainedmodels().list(
			project=self.PROJECT_ID
		).execute()

	def predict(self, inputData):
		if not isinstance(inputData, (list, tuple)):
			inputData = [inputData]

		body = {'input': {'csvInstance': inputData}}
		return ModelManager.api.trainedmodels().predict(
			project=self.PROJECT_ID,
			id=self.model,
			body=body
		).execute()

{'fraction_non_alphanumeric': 0.1171875, 'fraction_numerical': 0.008049242424242424, 'mean_word_length': 4.34134007585335, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 8, 'lorem ipsum': 0, 'www.': 8, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '895440', 'n_tokens_mistral': 1558, 'n_tokens_neox': 1376, 'n_words': 292}