From 98efde808fdda23d1579b888020898d85411a04c Mon Sep 17 00:00:00 2001 From: Ishan Date: Tue, 27 Jun 2023 00:33:02 +0200 Subject: [PATCH] adds functionality to connect model to app --- .../config/models/AWSomeNLPTranslator.class | Bin 4019 -> 6142 bytes .../config/models/TranslationModel.class | Bin 1625 -> 1721 bytes .../lambda/config/resolvers/URLResolver.class | Bin 2925 -> 3051 bytes .../function/UserConfigFunction/build.gradle | 6 + .../config/models/AWSomeNLPTranslator.java | 139 +++++++++--------- .../config/models/TranslationModel.java | 5 +- .../lambda/config/resolvers/URLResolver.java | 3 + 7 files changed, 86 insertions(+), 67 deletions(-) diff --git a/amplify/backend/function/UserConfigFunction/bin/main/com/awsomenlp/lambda/config/models/AWSomeNLPTranslator.class b/amplify/backend/function/UserConfigFunction/bin/main/com/awsomenlp/lambda/config/models/AWSomeNLPTranslator.class index d6c23a18b89f4b6a695344aae387488e8a5ecf2e..caf9e242cf904691224134c996364567bdadf250 100644 GIT binary patch literal 6142 zcmb7I349z?8UMc=JDbh4p_`I+Inxw!Hd&IEQZ{WdNn097)6%4^EpkkDr^zI{Gt16w zo3>N|kAffw$|0iSiFlT(o0ceGR8YK7(R$%6o`@GJ6^Z{hyPNFhDB;(1-pqU7`>yx@ zzW06mH=oQ1P#w#icvSeWGyrK^LR-4kk6uteY# zcdI+y<0K5T-v@!ED$c>V0%b96NZ(@F&Q|@Jq;8W@b#2;?oDHoR({iUuP=h)JwfX$_ z*^VAlQI7^X*GiJzscCM872HDE4mHrVSgQ|>2r6jGo>l&`iu2Gcpv2{ls7WR>>9pO8 zC=qahidDFfx@nQf6t)?c)$Ni_ zZ&k6Hu?S^Av%6jY5-PhSR4!8S8np57c=)}B6J@mIyFTq>T3Ch-73;8`K(TXxQl7EJ zg{O;?GzKqLu@RjDr6lgkgo{A)+#)f%p*79xQWag;L|#-fyCiTPAu<<;XLqQ7mBr3tZemJbh9cC`Vx63f~?qsY_ zPxNYi@-X~e76GtbOBnJyGg;&e7`DKwlbS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d8}VdKtmY;flbT3|W7 z8JY&6VZDYC|2IsG`+++ItX;OEAM=wX8DdM?yX6jSw@!9qwzR()|N)cZx;w65(dT<_BM&@yK zgdabc!;k0ivt+fha}K{u)?7nftR@zsp{G=~e>IW}-M;fLHN3IC9`C${4cqHQ`&HD& zYLqAQ=phz;{4IUlUc?+H=kUf`w6V(faE$*qfT^uznj=hXJ(C<|GPlsnIFr@HbhR=$ zdztJ5ILpYLqtsbSJjFzhkuDihMe><}jR~HuQ+S?Hd4XRqB9B-3R&*L*`WY9W5}>A+ zFpu9cZc&~yzsH-5PLJ z7AhcwztR9+2Z^<|gteP8iWZ?(u>#h!#CooRJfRZS8r=4^zJtH`S*7h|M(vm C@+QOp diff --git a/amplify/backend/function/UserConfigFunction/bin/main/com/awsomenlp/lambda/config/models/TranslationModel.class b/amplify/backend/function/UserConfigFunction/bin/main/com/awsomenlp/lambda/config/models/TranslationModel.class index 9bffa998fc534ab88a81149839d2f2c8bb63d943..7c368b81274bd392b403cab63002a51ef124986a 100644 GIT binary patch delta 235 zcmcb~vy+$W)W2Q(7#J9A8LT#Pon>aS@R=x}Z0{HzoS&QO=MxZ8l$ckXlUR~pWX;GR zj76A{fyXm1FSW=yC$YFVwV0hjkCDM>as!j$Nm=m7m7#lXy<%b?F-sK&`)#9+c;<{-&n4x}v|BmsU) BGPC5s^=g8@)TkJS+ zYB8`fXiwH<(`GcB9Lc6=!05oB#=yj&n#`cipi#`A$)E*vf)oQYgEoUMgT5Lkg8_pP PgNcJAgDH?UcaQ`C93Uc! diff --git a/amplify/backend/function/UserConfigFunction/bin/main/com/awsomenlp/lambda/config/resolvers/URLResolver.class b/amplify/backend/function/UserConfigFunction/bin/main/com/awsomenlp/lambda/config/resolvers/URLResolver.class index fc6e2b58e84cc1fdfbb680e57fa8167283d7c75d..5bc0d77b894733093e29805c02955fe482860fdf 100644 GIT binary patch delta 553 zcmaix&o5k26vsbzYTkYGOk-l9o-}EMDx#UBs}_@3iHulLqPm$j2oVW0blPIHeybnl zwTsTu#+EiAI*D{)#o9{3%1ZnR^&IbQBo-F;&X{>=1>R#|N#k=T8T$G5r5)NMuJ}iNt!bM( z)D=c|NM?EBB}(qUFgA(U+5`4@9iZc;s7sM`4|1eCfAi_N9klf>lUrh(=h??8begPJ znH){*6X%FVj%guZ6g~a`gn1~LjKWhYP-sq>Vw!;PC(KaJ14=nyRj qz>DpoY=$ydKFYew-qyH0yDN#&?rSvlo|7wE-(w^%u_nU>Sic72Kxml& delta 412 zcmXAl$uC1u6vltI+IL^8lCH`VB$5&lL)aObG!_=b$`r9`1R-Lfr8UoUspQ4RMq*`W zN=Qi%R;>6B?8V;3aN2He?m6H2e&4zGGylu)d;Pt=2Z9`E9Ev*o-6*=KavFvvs;H1M z;O!cWN8L;j*Tn3kQI|8#jQwl`y%FZTxR_TARM>Au%~hlBR({Q%!Z#Y83fGQ>$A@b> zqZ1?J!*Rg*1Y`-LXXda-VcH9NK=W4j`D;QGPR j)N2xd6k=+WrZU^GM{= 200 && statusCode < 300) { - // Success response - return responseBody.toString(); - } else { - // Error response - throw new Exception("API request failed with status code: " - + statusCode); + public static String makePostRequest(String sentence) throws Exception { + OkHttpClient client = new OkHttpClient().newBuilder() + .build(); + MediaType mediaType = MediaType.parse("application/json"); + String requestBody = "{\"data\":{\"inputs\":\"" + + sentence + + "\",\"parameters\":{\"max_length\":256}}}"; + RequestBody body = RequestBody.create( + mediaType, + requestBody); + + Request request = new Request.Builder() + .url( + "https://2c9kzo2ub4.execute-api.eu-west-1.amazonaws.com/auth/ml-model-api" + ) + .method("POST", body) + .addHeader("authorizationToken", "abc123") + .addHeader("Content-Type", "application/json") + .build(); + try (Response response = client.newCall(request).execute()) { + if (!response.isSuccessful()) { + throw new IOException("Unexpected response code: " + response.code()); } + + String ret = response.body().string(); + System.out.println(ret); + + ObjectMapper objectMapper = new ObjectMapper(); + JsonNode jsonNode = objectMapper.readTree(ret); + ret = jsonNode.get(0).get("generated_text").asText(); + ret = ret.replace("``", "").replace("''", ""); + // make sure that ret is first utf-encoded then utf escape decoded + ret = new String(ret.getBytes("UTF-8"), "UTF-8"); + ret = StringEscapeUtils.unescapeJava(ret); + return ret; + } catch (IOException e) { + e.printStackTrace(); + System.out.println("Error making API call: " + e.getMessage()); + return "Oopsie doopsie: " + e.getMessage(); + } } /** diff --git a/amplify/backend/function/UserConfigFunction/src/main/java/com/awsomenlp/lambda/config/models/TranslationModel.java b/amplify/backend/function/UserConfigFunction/src/main/java/com/awsomenlp/lambda/config/models/TranslationModel.java index a84ac9e..04c7c7d 100644 --- a/amplify/backend/function/UserConfigFunction/src/main/java/com/awsomenlp/lambda/config/models/TranslationModel.java +++ b/amplify/backend/function/UserConfigFunction/src/main/java/com/awsomenlp/lambda/config/models/TranslationModel.java @@ -11,7 +11,10 @@ include = JsonTypeInfo.As.PROPERTY, property = "type") @JsonSubTypes({ - @JsonSubTypes.Type(value = AmazonTranslate.class, name = "amazonTranslate") + @JsonSubTypes.Type(value = AmazonTranslate.class, + name = "amazonTranslate"), + @JsonSubTypes.Type(value = AWSomeNLPTranslator.class, + name = "AWSomeNLPTranslator") }) public abstract class TranslationModel { private String id; diff --git a/amplify/backend/function/UserConfigFunction/src/main/java/com/awsomenlp/lambda/config/resolvers/URLResolver.java b/amplify/backend/function/UserConfigFunction/src/main/java/com/awsomenlp/lambda/config/resolvers/URLResolver.java index 4a5d9d9..e9983d3 100644 --- a/amplify/backend/function/UserConfigFunction/src/main/java/com/awsomenlp/lambda/config/resolvers/URLResolver.java +++ b/amplify/backend/function/UserConfigFunction/src/main/java/com/awsomenlp/lambda/config/resolvers/URLResolver.java @@ -68,6 +68,9 @@ public Text resolveDocument(Document doc) throws IOException { Elements paraEles = doc.select("p, h2"); List paragraphs = new ArrayList<>(); for (Element ele : paraEles) { + if (ele.text().equals("") || ele.text().equals(null)) { + continue; + } paragraphs.add(ele.text()); }