ChristopherLucas / translateR

R Package for Cross-Language Topic Modeling
20 stars 17 forks source link

Microsoft Azure v2.0 is now obsolete, needs updated to v3.0? #14

Open Joseph-Watts opened 3 years ago

Joseph-Watts commented 3 years ago

The translate functions don’t seem to work for Microsoft any more. This may be because the code is based on Azure v2.0 and Microsoft now requires v3.0?

Not sure if this packages is still being maintained. It would be super useful to me if this function still worked.

richierocks commented 3 years ago

Thanks, I'll take a look at it this weekend.

Joseph-Watts commented 3 years ago

That would be fantastic thanks.

Joseph-Watts commented 3 years ago

Hi Richie, I appreciate the reply thanks, did you get the chance to take a peek at this on the weekend?

richierocks commented 3 years ago

I'm pretty rusty on this, and my Azure credentials had expired, so it's taken me a few hours to get this far:

library(httr)

x <- "It is Tuesday evening"
target.lang <- "fr"
source.lang <- "en"
api.key <- Sys.getenv("MICROSOFT_TRANSLATOR_API_KEY")

base_url <- "https://api.cognitive.microsofttranslator.com/translate"

query = list(
  "api-version" = "3.0",
  to = target.lang,
  from = source.lang
)
body <- data.frame(Text = x)

response <- POST(
  base_url, 
  body = body,
  add_headers(
    "Ocp-Apim-Subscription-Key" = api.key,
    "Content-Type" = "application/json; charset=UTF-8"
  ),
  query = query,
  encode = "json", 
  verbose(info = TRUE)
)
content(response)
#> [[1]]
#> [[1]]$translations
#> [[1]]$translations[[1]]
#> [[1]]$translations[[1]]$text
#> [1] "C’est mardi soir"
#> 
#> [[1]]$translations[[1]]$to
#> [1] "fr"

Wrapping the logic into a function ought to be straightforward, so I'll take a look at doing that tomorrow.

richierocks commented 3 years ago

OK, that was actually easier than expected.

This now works:

library(translateR)
translate(
  data.frame(x = c("It is Tuesday evening", "I am tired")), 
  content.field = "x", 
  microsoft.api.key = Sys.getenv("MICROSOFT_TRANSLATOR_API_KEY"), 
  source.lang = "en", 
  target.lang = "fr"
)

Though I just realized I accidentally pushed to master. Let me know if you want me to revert and push to a branch instead.

Joseph-Watts commented 3 years ago

Awesome, thanks very much Richie, that is very much appreciated!

I was getting an “Unauthorized (HTTP 401)” error with the updated code, which it due to my account being in a different region.

To get it working I’ve added a Ocp-Apim-Subscription-Region argument to the translate function here: https://github.com/Joseph-Watts/translateR For me, I need to specify the region as “eastasia”

Thanks very much for this, I wasnt able to get this working on my own.

I’ll have a play around with microsoftTranslateToken.R when I get a moment to see if I can get that working with v3 of Azure.

Much appreciated!