Roland Szabo commited on
Commit
b9dcc50
1 Parent(s): 3b5be8d

AWS Detect language

Browse files
Files changed (1) hide show
  1. app.py +10 -1
app.py CHANGED
@@ -1,5 +1,6 @@
1
  import gradio as gr
2
  import spacy
 
3
  from transformers import pipeline
4
  import boto3
5
  nlp = spacy.load("en_core_web_sm")
@@ -27,8 +28,16 @@ def greet(model_type, text):
27
  for entity in output]}
28
  elif model_type == "AWS Comprehend":
29
  client = boto3.client('comprehend')
 
 
 
 
 
 
 
 
30
  response = client.detect_entities(
31
- Text=text, LanguageCode='en')
32
  print(response)
33
  return {"text": text, "entities": [{"word": entity["Text"], "entity": entity["Type"], "start": entity['BeginOffset'], 'end': entity['EndOffset']}
34
  for entity in response["Entities"]]}
 
1
  import gradio as gr
2
  import spacy
3
+ from botocore.exceptions import ClientError
4
  from transformers import pipeline
5
  import boto3
6
  nlp = spacy.load("en_core_web_sm")
 
28
  for entity in output]}
29
  elif model_type == "AWS Comprehend":
30
  client = boto3.client('comprehend')
31
+ try:
32
+ response = client.detect_dominant_language(Text=text)
33
+ languages = response['Languages']
34
+ print("Detected %s languages.", len(languages))
35
+ language = languages[0]['LanguageCode']
36
+ except ClientError:
37
+ print("Couldn't detect languages.")
38
+ language = 'en'
39
  response = client.detect_entities(
40
+ Text=text, LanguageCode=language)
41
  print(response)
42
  return {"text": text, "entities": [{"word": entity["Text"], "entity": entity["Type"], "start": entity['BeginOffset'], 'end': entity['EndOffset']}
43
  for entity in response["Entities"]]}