Skip to content
This repository has been archived by the owner on Mar 7, 2018. It is now read-only.

Allow environment to override http/https for apis #45

Merged
merged 1 commit into from Jul 5, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -14,5 +14,6 @@ object Constants {
val FeatureServiceHost = "FORTIS_FEATURE_SERVICE_HOST"
val OxfordVisionToken= "OXFORD_VISION_TOKEN"
val OxfordLanguageToken = "OXFORD_LANGUAGE_TOKEN"
val BlobHost = "FORTIS_BLOB_HOST"
}
}
Expand Up @@ -31,9 +31,8 @@ object Pipeline {
// broadcast changes across the Spark cluster.

val geofence = Geofence(north = 49.6185146245, west = -124.9578052195, south = 46.8691952854, east = -121.0945042053)
val modelsProvider = new ZipModelsProvider(
language => s"https://fortiscentral.blob.core.windows.net/opener/opener-$language.zip",
Settings.modelsDir)
val entityModelsProvider = new ZipModelsProvider(language => s"${Settings.blobHost}/opener/opener-$language.zip", Settings.modelsDir)
val sentimentModelsProvider = new ZipModelsProvider(language => s"${Settings.blobHost}/sentiment/sentiment-$language.zip", Settings.modelsDir)

val featureServiceClient = new FeatureServiceClient(Settings.featureServiceHost)
val locationsExtractorFactory = new LocationsExtractorFactory(featureServiceClient, geofence).buildLookup()
Expand Down Expand Up @@ -76,19 +75,19 @@ object Pipeline {
}

def addEntities(event: ExtendedFortisEvent[T]): ExtendedFortisEvent[T] = {
val entities = analyzer.extractEntities(event.details, new PeopleRecognizer(modelsProvider, event.analysis.language))
val entities = analyzer.extractEntities(event.details, new PeopleRecognizer(entityModelsProvider, event.analysis.language))
event.copy(analysis = event.analysis.copy(entities = entities))
}

def addSentiments(event: ExtendedFortisEvent[T]): ExtendedFortisEvent[T] = {
val sentiments = analyzer.detectSentiment(event.details,
new SentimentDetector(modelsProvider, event.analysis.language, sentimentDetectorAuth))
new SentimentDetector(sentimentModelsProvider, event.analysis.language, sentimentDetectorAuth))
event.copy(analysis = event.analysis.copy(sentiments = sentiments))
}

def addLocations(event: ExtendedFortisEvent[T]): ExtendedFortisEvent[T] = {
val locations = analyzer.extractLocations(event.details,
locationsExtractorFactory.create(Some(new PlaceRecognizer(modelsProvider, event.analysis.language))))
locationsExtractorFactory.create(Some(new PlaceRecognizer(entityModelsProvider, event.analysis.language))))
event.copy(analysis = event.analysis.copy(locations = locations))
}

Expand Down
Expand Up @@ -24,6 +24,7 @@ object ProjectFortis extends App {
val featureServiceHost = envOrFail(Constants.Env.FeatureServiceHost)
val oxfordLanguageToken = envOrFail(Constants.Env.OxfordLanguageToken)
val oxfordVisionToken = envOrFail(Constants.Env.OxfordVisionToken)
val blobHost = envOrElse(Constants.Env.BlobHost, "https://fortiscentral.blob.core.windows.net")

val appInsightsKey = envOrNone(Constants.Env.AppInsightsKey)
val modelsDir = envOrNone(Constants.Env.LanguageModelDir)
Expand Down
Expand Up @@ -5,6 +5,7 @@ trait Settings {
val featureServiceHost: String
val oxfordLanguageToken: String
val oxfordVisionToken: String
val blobHost: String
val appInsightsKey: Option[String]
val modelsDir: Option[String]
}
Expand Up @@ -6,7 +6,7 @@ import net.liftweb.json

import scalaj.http.Http

case class ImageAnalysisAuth(key: String, apiHost: String = "westus.api.cognitive.microsoft.com")
case class ImageAnalysisAuth(key: String, apiHost: String = "https://westus.api.cognitive.microsoft.com")

@SerialVersionUID(100L)
class ImageAnalyzer(auth: ImageAnalysisAuth, featureServiceClient: FeatureServiceClient) extends Serializable {
Expand All @@ -17,7 +17,7 @@ class ImageAnalyzer(auth: ImageAnalysisAuth, featureServiceClient: FeatureServic
}

protected def callCognitiveServices(requestBody: String): String = {
Http(s"https://${auth.apiHost}/vision/v1.0/analyze")
Http(s"${auth.apiHost}/vision/v1.0/analyze")
.params(
"details" -> "Celebrities,Landmarks",
"visualFeatures" -> "Categories,Tags,Description,Faces")
Expand Down
Expand Up @@ -4,7 +4,7 @@ import net.liftweb.json

import scalaj.http.Http

case class LanguageDetectorAuth(key: String, apiHost: String = "westus.api.cognitive.microsoft.com")
case class LanguageDetectorAuth(key: String, apiHost: String = "https://westus.api.cognitive.microsoft.com")

@SerialVersionUID(100L)
class LanguageDetector(
Expand All @@ -24,7 +24,7 @@ class LanguageDetector(
}

protected def callCognitiveServices(requestBody: String): String = {
Http(s"https://${auth.apiHost}/text/analytics/v2.0/languages")
Http(s"${auth.apiHost}/text/analytics/v2.0/languages")
.params(
"numberOfLanguagesToDetect" -> "1")
.headers(
Expand Down
Expand Up @@ -40,17 +40,17 @@ class FeatureServiceClient(host: String) extends Serializable with Loggable {
}

protected def fetchBboxResponse(geofence: Geofence): Try[String] = {
val fetch = s"http://$host/features/bbox/${geofence.north}/${geofence.west}/${geofence.south}/${geofence.east}"
val fetch = s"$host/features/bbox/${geofence.north}/${geofence.west}/${geofence.south}/${geofence.east}"
fetchResponse(fetch)
}

protected def fetchPointResponse(latitude: Double, longitude: Double): Try[String] = {
val fetch = s"http://$host/features/point/$latitude/$longitude"
val fetch = s"$host/features/point/$latitude/$longitude"
fetchResponse(fetch)
}

protected def fetchNameResponse(names: Iterable[String]): Try[String] = {
val fetch = s"http://$host/features/name/${names.mkString(",")}"
val fetch = s"$host/features/name/${names.mkString(",")}"
fetchResponse(fetch)
}

Expand Down
Expand Up @@ -4,7 +4,7 @@ import net.liftweb.json

import scalaj.http.Http

case class SentimentDetectorAuth(key: String, apiHost: String = "westus.api.cognitive.microsoft.com")
case class SentimentDetectorAuth(key: String, apiHost: String = "https://westus.api.cognitive.microsoft.com")

@SerialVersionUID(100L)
class CognitiveServicesSentimentDetector(
Expand All @@ -20,7 +20,7 @@ class CognitiveServicesSentimentDetector(
}

protected def callCognitiveServices(requestBody: String): String = {
Http(s"https://${auth.apiHost}/text/analytics/v2.0/sentiment")
Http(s"${auth.apiHost}/text/analytics/v2.0/sentiment")
.headers(
"Content-Type" -> "application/json",
"Ocp-Apim-Subscription-Key" -> auth.key)
Expand Down