Spaces:
Runtime error
Runtime error
Commit
·
27880c1
0
Parent(s):
Duplicate from dperales/ITACA_Insurance_Core_v4
Browse filesCo-authored-by: DANIEL <[email protected]>
- .gitattributes +34 -0
- OCR_Detector.py +12 -0
- Object_Detector.py +146 -0
- README.md +13 -0
- Roboto-Light.ttf +0 -0
- app.py +337 -0
- app_v3.txt +320 -0
- appv2.py +320 -0
- detect_language.py +29 -0
- emotion_detection.py +67 -0
- itaca_logo.png +0 -0
- keyword_extraction.py +145 -0
- models.py +26 -0
- named_entity_recognition.py +60 -0
- part_of_speech_tagging.py +24 -0
- requirements.txt +19 -0
- sentiment_analysis.py +78 -0
- sentiment_analysis_v2.py +93 -0
.gitattributes
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
*.7z filter=lfs diff=lfs merge=lfs -text
|
| 2 |
+
*.arrow filter=lfs diff=lfs merge=lfs -text
|
| 3 |
+
*.bin filter=lfs diff=lfs merge=lfs -text
|
| 4 |
+
*.bz2 filter=lfs diff=lfs merge=lfs -text
|
| 5 |
+
*.ckpt filter=lfs diff=lfs merge=lfs -text
|
| 6 |
+
*.ftz filter=lfs diff=lfs merge=lfs -text
|
| 7 |
+
*.gz filter=lfs diff=lfs merge=lfs -text
|
| 8 |
+
*.h5 filter=lfs diff=lfs merge=lfs -text
|
| 9 |
+
*.joblib filter=lfs diff=lfs merge=lfs -text
|
| 10 |
+
*.lfs.* filter=lfs diff=lfs merge=lfs -text
|
| 11 |
+
*.mlmodel filter=lfs diff=lfs merge=lfs -text
|
| 12 |
+
*.model filter=lfs diff=lfs merge=lfs -text
|
| 13 |
+
*.msgpack filter=lfs diff=lfs merge=lfs -text
|
| 14 |
+
*.npy filter=lfs diff=lfs merge=lfs -text
|
| 15 |
+
*.npz filter=lfs diff=lfs merge=lfs -text
|
| 16 |
+
*.onnx filter=lfs diff=lfs merge=lfs -text
|
| 17 |
+
*.ot filter=lfs diff=lfs merge=lfs -text
|
| 18 |
+
*.parquet filter=lfs diff=lfs merge=lfs -text
|
| 19 |
+
*.pb filter=lfs diff=lfs merge=lfs -text
|
| 20 |
+
*.pickle filter=lfs diff=lfs merge=lfs -text
|
| 21 |
+
*.pkl filter=lfs diff=lfs merge=lfs -text
|
| 22 |
+
*.pt filter=lfs diff=lfs merge=lfs -text
|
| 23 |
+
*.pth filter=lfs diff=lfs merge=lfs -text
|
| 24 |
+
*.rar filter=lfs diff=lfs merge=lfs -text
|
| 25 |
+
*.safetensors filter=lfs diff=lfs merge=lfs -text
|
| 26 |
+
saved_model/**/* filter=lfs diff=lfs merge=lfs -text
|
| 27 |
+
*.tar.* filter=lfs diff=lfs merge=lfs -text
|
| 28 |
+
*.tflite filter=lfs diff=lfs merge=lfs -text
|
| 29 |
+
*.tgz filter=lfs diff=lfs merge=lfs -text
|
| 30 |
+
*.wasm filter=lfs diff=lfs merge=lfs -text
|
| 31 |
+
*.xz filter=lfs diff=lfs merge=lfs -text
|
| 32 |
+
*.zip filter=lfs diff=lfs merge=lfs -text
|
| 33 |
+
*.zst filter=lfs diff=lfs merge=lfs -text
|
| 34 |
+
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
OCR_Detector.py
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import pandas as pd
|
| 2 |
+
import numpy as np
|
| 3 |
+
import streamlit as st
|
| 4 |
+
import easyocr
|
| 5 |
+
import PIL
|
| 6 |
+
from PIL import Image, ImageDraw
|
| 7 |
+
|
| 8 |
+
class OCRDetector:
|
| 9 |
+
|
| 10 |
+
def __init__(self):
|
| 11 |
+
# it will only detect the English and Spanish part of the image as text
|
| 12 |
+
self.reader = easyocr.Reader(['es','en'], gpu=False)
|
Object_Detector.py
ADDED
|
@@ -0,0 +1,146 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import tensorflow as tf
|
| 3 |
+
import tensorflow_hub as hub
|
| 4 |
+
# Load compressed models from tensorflow_hub
|
| 5 |
+
os.environ['TFHUB_MODEL_LOAD_FORMAT'] = 'COMPRESSED'
|
| 6 |
+
|
| 7 |
+
import matplotlib.pyplot as plt
|
| 8 |
+
import matplotlib as mpl
|
| 9 |
+
|
| 10 |
+
# For drawing onto the image.
|
| 11 |
+
import numpy as np
|
| 12 |
+
from tensorflow.python.ops.numpy_ops import np_config
|
| 13 |
+
np_config.enable_numpy_behavior()
|
| 14 |
+
from PIL import Image
|
| 15 |
+
from PIL import ImageColor
|
| 16 |
+
from PIL import ImageDraw
|
| 17 |
+
from PIL import ImageFont
|
| 18 |
+
import time
|
| 19 |
+
|
| 20 |
+
import streamlit as st
|
| 21 |
+
|
| 22 |
+
# For measuring the inference time.
|
| 23 |
+
import time
|
| 24 |
+
|
| 25 |
+
|
| 26 |
+
class ObjectDetector:
|
| 27 |
+
|
| 28 |
+
def __init__(self):
|
| 29 |
+
# Load Tokenizer & Model
|
| 30 |
+
# hub_location = 'cardiffnlp/twitter-roberta-base-sentiment'
|
| 31 |
+
# self.tokenizer = AutoTokenizer.from_pretrained(hub_location)
|
| 32 |
+
# self.model = AutoModelForSequenceClassification.from_pretrained(hub_location)
|
| 33 |
+
|
| 34 |
+
# Change model labels in config
|
| 35 |
+
# self.model.config.id2label[0] = "Negative"
|
| 36 |
+
# self.model.config.id2label[1] = "Neutral"
|
| 37 |
+
# self.model.config.id2label[2] = "Positive"
|
| 38 |
+
# self.model.config.label2id["Negative"] = self.model.config.label2id.pop("LABEL_0")
|
| 39 |
+
# self.model.config.label2id["Neutral"] = self.model.config.label2id.pop("LABEL_1")
|
| 40 |
+
# self.model.config.label2id["Positive"] = self.model.config.label2id.pop("LABEL_2")
|
| 41 |
+
|
| 42 |
+
# Instantiate explainer
|
| 43 |
+
# self.explainer = SequenceClassificationExplainer(self.model, self.tokenizer)
|
| 44 |
+
|
| 45 |
+
# module_handle = "https://tfhub.dev/google/faster_rcnn/openimages_v4/inception_resnet_v2/1"
|
| 46 |
+
module_handle = "https://tfhub.dev/google/openimages_v4/ssd/mobilenet_v2/1"
|
| 47 |
+
self.detector = hub.load(module_handle).signatures['default']
|
| 48 |
+
|
| 49 |
+
def run_detector(self, path):
|
| 50 |
+
img = path
|
| 51 |
+
|
| 52 |
+
converted_img = tf.image.convert_image_dtype(img, tf.float32)[tf.newaxis, ...]
|
| 53 |
+
|
| 54 |
+
start_time = time.time()
|
| 55 |
+
result = self.detector(converted_img)
|
| 56 |
+
end_time = time.time()
|
| 57 |
+
|
| 58 |
+
result = {key:value.numpy() for key,value in result.items()}
|
| 59 |
+
|
| 60 |
+
primer = format(result["detection_class_entities"][0]) + ' ' + format(round(result["detection_scores"][0]*100)) + '%'
|
| 61 |
+
|
| 62 |
+
image_with_boxes = self.draw_boxes(
|
| 63 |
+
img, result["detection_boxes"],
|
| 64 |
+
result["detection_class_entities"], result["detection_scores"])
|
| 65 |
+
|
| 66 |
+
# display_image(image_with_boxes)
|
| 67 |
+
return image_with_boxes, primer
|
| 68 |
+
|
| 69 |
+
def display_image(self, image):
|
| 70 |
+
fig = plt.figure(figsize=(20, 15))
|
| 71 |
+
plt.grid(False)
|
| 72 |
+
plt.imshow(image)
|
| 73 |
+
|
| 74 |
+
def draw_bounding_box_on_image(self, image,
|
| 75 |
+
ymin,
|
| 76 |
+
xmin,
|
| 77 |
+
ymax,
|
| 78 |
+
xmax,
|
| 79 |
+
color,
|
| 80 |
+
font,
|
| 81 |
+
thickness=4,
|
| 82 |
+
display_str_list=()):
|
| 83 |
+
"""Adds a bounding box to an image."""
|
| 84 |
+
draw = ImageDraw.Draw(image)
|
| 85 |
+
im_width, im_height = image.size
|
| 86 |
+
(left, right, top, bottom) = (xmin * im_width, xmax * im_width,
|
| 87 |
+
ymin * im_height, ymax * im_height)
|
| 88 |
+
draw.line([(left, top), (left, bottom), (right, bottom), (right, top),
|
| 89 |
+
(left, top)],
|
| 90 |
+
width=thickness,
|
| 91 |
+
fill=color)
|
| 92 |
+
|
| 93 |
+
# If the total height of the display strings added to the top of the bounding
|
| 94 |
+
# box exceeds the top of the image, stack the strings below the bounding box
|
| 95 |
+
# instead of above.
|
| 96 |
+
display_str_heights = [font.getsize(ds)[1] for ds in display_str_list]
|
| 97 |
+
# Each display_str has a top and bottom margin of 0.05x.
|
| 98 |
+
total_display_str_height = (1 + 2 * 0.05) * sum(display_str_heights)
|
| 99 |
+
|
| 100 |
+
if top > total_display_str_height:
|
| 101 |
+
text_bottom = top
|
| 102 |
+
else:
|
| 103 |
+
text_bottom = top + total_display_str_height
|
| 104 |
+
# Reverse list and print from bottom to top.
|
| 105 |
+
for display_str in display_str_list[::-1]:
|
| 106 |
+
text_width, text_height = font.getsize(display_str)
|
| 107 |
+
margin = np.ceil(0.05 * text_height)
|
| 108 |
+
draw.rectangle([(left, text_bottom - text_height - 2 * margin),
|
| 109 |
+
(left + text_width, text_bottom)],
|
| 110 |
+
fill=color)
|
| 111 |
+
draw.text((left + margin, text_bottom - text_height - margin),
|
| 112 |
+
display_str,
|
| 113 |
+
fill="black",
|
| 114 |
+
font=font)
|
| 115 |
+
text_bottom -= text_height - 2 * margin
|
| 116 |
+
|
| 117 |
+
def draw_boxes(self, image, boxes, class_names, scores, max_boxes=10, min_score=0.4):
|
| 118 |
+
"""Overlay labeled boxes on an image with formatted scores and label names."""
|
| 119 |
+
colors = list(ImageColor.colormap.values())
|
| 120 |
+
|
| 121 |
+
try:
|
| 122 |
+
font = ImageFont.truetype("./Roboto-Light.ttf", 24)
|
| 123 |
+
|
| 124 |
+
except IOError:
|
| 125 |
+
print("Font not found, using default font.")
|
| 126 |
+
font = ImageFont.load_default()
|
| 127 |
+
|
| 128 |
+
for i in range(min(boxes.shape[0], max_boxes)):
|
| 129 |
+
if scores[i] >= min_score:
|
| 130 |
+
ymin, xmin, ymax, xmax = tuple(boxes[i])
|
| 131 |
+
display_str = "{}: {}%".format(class_names[i].decode("ascii"),
|
| 132 |
+
int(100 * scores[i]))
|
| 133 |
+
color = colors[hash(class_names[i]) % len(colors)]
|
| 134 |
+
image_pil = Image.fromarray(np.uint8(image)).convert("RGB")
|
| 135 |
+
self.draw_bounding_box_on_image(
|
| 136 |
+
image_pil,
|
| 137 |
+
ymin,
|
| 138 |
+
xmin,
|
| 139 |
+
ymax,
|
| 140 |
+
xmax,
|
| 141 |
+
color,
|
| 142 |
+
font,
|
| 143 |
+
display_str_list=[display_str])
|
| 144 |
+
np.copyto(image, np.array(image_pil))
|
| 145 |
+
return image
|
| 146 |
+
|
README.md
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
---
|
| 2 |
+
title: ITACA Insurance Core V4
|
| 3 |
+
emoji: 🏃
|
| 4 |
+
colorFrom: pink
|
| 5 |
+
colorTo: pink
|
| 6 |
+
sdk: streamlit
|
| 7 |
+
sdk_version: 1.19.0
|
| 8 |
+
app_file: app.py
|
| 9 |
+
pinned: true
|
| 10 |
+
duplicated_from: dperales/ITACA_Insurance_Core_v4
|
| 11 |
+
---
|
| 12 |
+
|
| 13 |
+
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|
Roboto-Light.ttf
ADDED
|
Binary file (170 kB). View file
|
|
|
app.py
ADDED
|
@@ -0,0 +1,337 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import pandas as pd
|
| 3 |
+
import numpy as np
|
| 4 |
+
import easyocr
|
| 5 |
+
import streamlit as st
|
| 6 |
+
from annotated_text import annotated_text
|
| 7 |
+
from streamlit_option_menu import option_menu
|
| 8 |
+
from sentiment_analysis_v2 import SentimentAnalysis
|
| 9 |
+
from keyword_extraction import KeywordExtractor
|
| 10 |
+
from part_of_speech_tagging import POSTagging
|
| 11 |
+
from emotion_detection import EmotionDetection
|
| 12 |
+
from named_entity_recognition import NamedEntityRecognition
|
| 13 |
+
from Object_Detector import ObjectDetector
|
| 14 |
+
from OCR_Detector import OCRDetector
|
| 15 |
+
from detect_language import LanguageDetector
|
| 16 |
+
|
| 17 |
+
import PIL
|
| 18 |
+
from PIL import Image
|
| 19 |
+
from PIL import ImageColor
|
| 20 |
+
from PIL import ImageDraw
|
| 21 |
+
from PIL import ImageFont
|
| 22 |
+
import time
|
| 23 |
+
|
| 24 |
+
# Imports de Object Detection
|
| 25 |
+
import tensorflow as tf
|
| 26 |
+
import tensorflow_hub as hub
|
| 27 |
+
# Load compressed models from tensorflow_hub
|
| 28 |
+
os.environ['TFHUB_MODEL_LOAD_FORMAT'] = 'COMPRESSED'
|
| 29 |
+
import matplotlib.pyplot as plt
|
| 30 |
+
import matplotlib as mpl
|
| 31 |
+
# For drawing onto the image.
|
| 32 |
+
import numpy as np
|
| 33 |
+
from tensorflow.python.ops.numpy_ops import np_config
|
| 34 |
+
np_config.enable_numpy_behavior()
|
| 35 |
+
|
| 36 |
+
import torch
|
| 37 |
+
import librosa
|
| 38 |
+
from models import infere_speech_emotion, infere_text_emotion, infere_voice2text
|
| 39 |
+
|
| 40 |
+
from transformers import pipeline
|
| 41 |
+
|
| 42 |
+
def main():
|
| 43 |
+
|
| 44 |
+
st.set_page_config(layout="wide")
|
| 45 |
+
|
| 46 |
+
hide_streamlit_style = """
|
| 47 |
+
<style>
|
| 48 |
+
#MainMenu {visibility: hidden;}
|
| 49 |
+
footer {visibility: hidden;}
|
| 50 |
+
</style>
|
| 51 |
+
"""
|
| 52 |
+
st.markdown(hide_streamlit_style, unsafe_allow_html=True)
|
| 53 |
+
|
| 54 |
+
@st.cache_resource
|
| 55 |
+
def load_sentiment_model():
|
| 56 |
+
return SentimentAnalysis()
|
| 57 |
+
|
| 58 |
+
@st.cache_resource
|
| 59 |
+
def load_keyword_model():
|
| 60 |
+
return KeywordExtractor()
|
| 61 |
+
|
| 62 |
+
@st.cache_resource
|
| 63 |
+
def load_pos_model():
|
| 64 |
+
return POSTagging()
|
| 65 |
+
|
| 66 |
+
@st.cache_resource
|
| 67 |
+
def load_emotion_model():
|
| 68 |
+
return EmotionDetection()
|
| 69 |
+
|
| 70 |
+
@st.cache_resource
|
| 71 |
+
def load_ner_model():
|
| 72 |
+
return NamedEntityRecognition()
|
| 73 |
+
|
| 74 |
+
@st.cache_resource
|
| 75 |
+
def load_objectdetector_model():
|
| 76 |
+
return ObjectDetector()
|
| 77 |
+
|
| 78 |
+
@st.cache_resource
|
| 79 |
+
def load_ocrdetector_model():
|
| 80 |
+
return OCRDetector()
|
| 81 |
+
|
| 82 |
+
@st.cache_resource
|
| 83 |
+
def load_langdetector_model():
|
| 84 |
+
return LanguageDetector()
|
| 85 |
+
|
| 86 |
+
sentiment_analyzer = load_sentiment_model()
|
| 87 |
+
keyword_extractor = load_keyword_model()
|
| 88 |
+
pos_tagger = load_pos_model()
|
| 89 |
+
emotion_detector = load_emotion_model()
|
| 90 |
+
ner = load_ner_model()
|
| 91 |
+
objectdetector1 = load_objectdetector_model()
|
| 92 |
+
ocrdetector1 = load_ocrdetector_model()
|
| 93 |
+
langdetector1 = load_langdetector_model()
|
| 94 |
+
|
| 95 |
+
def rectangle(image, result):
|
| 96 |
+
draw = ImageDraw.Draw(image)
|
| 97 |
+
for res in result:
|
| 98 |
+
top_left = tuple(res[0][0]) # top left coordinates as tuple
|
| 99 |
+
bottom_right = tuple(res[0][2]) # bottom right coordinates as tuple
|
| 100 |
+
draw.rectangle((top_left, bottom_right), outline="blue", width=2)
|
| 101 |
+
st.image(image)
|
| 102 |
+
|
| 103 |
+
example_text = "My name is Daniel: The attention to detail, swift resolution, and accuracy demonstrated by ITACA Insurance Company in Spain in handling my claim were truly impressive. This undoubtedly reflects their commitment to being a customer-centric insurance provider."
|
| 104 |
+
|
| 105 |
+
with st.sidebar:
|
| 106 |
+
image = Image.open('./itaca_logo.png')
|
| 107 |
+
st.image(image,width=150) #use_column_width=True)
|
| 108 |
+
page = option_menu(menu_title='Menu',
|
| 109 |
+
menu_icon="robot",
|
| 110 |
+
options=["Sentiment Analysis",
|
| 111 |
+
"Keyword Extraction",
|
| 112 |
+
"Part of Speech Tagging",
|
| 113 |
+
"Emotion Detection",
|
| 114 |
+
"Named Entity Recognition",
|
| 115 |
+
"Speech & Text Emotion",
|
| 116 |
+
"Object Detector",
|
| 117 |
+
"OCR Detector"],
|
| 118 |
+
icons=["chat-dots",
|
| 119 |
+
"key",
|
| 120 |
+
"tag",
|
| 121 |
+
"emoji-heart-eyes",
|
| 122 |
+
"building",
|
| 123 |
+
"book",
|
| 124 |
+
"camera",
|
| 125 |
+
"list-task"],
|
| 126 |
+
default_index=0
|
| 127 |
+
)
|
| 128 |
+
|
| 129 |
+
st.title('ITACA Insurance Core AI Module')
|
| 130 |
+
|
| 131 |
+
# Replace '20px' with your desired font size
|
| 132 |
+
font_size = '20px'
|
| 133 |
+
|
| 134 |
+
if page == "Sentiment Analysis":
|
| 135 |
+
st.header('Sentiment Analysis')
|
| 136 |
+
# st.markdown("")
|
| 137 |
+
st.write(
|
| 138 |
+
"""
|
| 139 |
+
"""
|
| 140 |
+
)
|
| 141 |
+
|
| 142 |
+
text = st.text_area("Paste text here", value=example_text)
|
| 143 |
+
|
| 144 |
+
if st.button('🔥 Run!'):
|
| 145 |
+
with st.spinner("Loading..."):
|
| 146 |
+
o_lang = langdetector1.predict_language(text)
|
| 147 |
+
|
| 148 |
+
preds, html = sentiment_analyzer.run(text, o_lang)
|
| 149 |
+
st.success('All done!')
|
| 150 |
+
st.write("")
|
| 151 |
+
st.subheader("Sentiment Predictions")
|
| 152 |
+
st.bar_chart(data=preds, width=0, height=0, use_container_width=True)
|
| 153 |
+
st.write("")
|
| 154 |
+
st.subheader("Sentiment Justification")
|
| 155 |
+
raw_html = html._repr_html_()
|
| 156 |
+
st.components.v1.html(raw_html, height=500)
|
| 157 |
+
|
| 158 |
+
elif page == "Keyword Extraction":
|
| 159 |
+
st.header('Keyword Extraction')
|
| 160 |
+
# st.markdown("")
|
| 161 |
+
st.write(
|
| 162 |
+
"""
|
| 163 |
+
"""
|
| 164 |
+
)
|
| 165 |
+
|
| 166 |
+
text = st.text_area("Paste text here", value=example_text)
|
| 167 |
+
|
| 168 |
+
max_keywords = st.slider('# of Keywords Max Limit', min_value=1, max_value=10, value=5, step=1)
|
| 169 |
+
|
| 170 |
+
if st.button('🔥 Run!'):
|
| 171 |
+
with st.spinner("Loading..."):
|
| 172 |
+
annotation, keywords = keyword_extractor.generate(text, max_keywords)
|
| 173 |
+
st.success('All done!')
|
| 174 |
+
|
| 175 |
+
if annotation:
|
| 176 |
+
st.subheader("Keyword Annotation")
|
| 177 |
+
st.write("")
|
| 178 |
+
annotated_text(*annotation)
|
| 179 |
+
st.text("")
|
| 180 |
+
|
| 181 |
+
st.subheader("Extracted Keywords")
|
| 182 |
+
st.write("")
|
| 183 |
+
df = pd.DataFrame(keywords, columns=['Extracted Keywords'])
|
| 184 |
+
csv = df.to_csv(index=False).encode('utf-8')
|
| 185 |
+
st.download_button('Download Keywords to CSV', csv, file_name='news_intelligence_keywords.csv')
|
| 186 |
+
|
| 187 |
+
data_table = st.table(df)
|
| 188 |
+
|
| 189 |
+
elif page == "Part of Speech Tagging":
|
| 190 |
+
st.header('Part of Speech Tagging')
|
| 191 |
+
# st.markdown("")
|
| 192 |
+
st.write(
|
| 193 |
+
"""
|
| 194 |
+
"""
|
| 195 |
+
)
|
| 196 |
+
|
| 197 |
+
text = st.text_area("Paste text here", value=example_text)
|
| 198 |
+
|
| 199 |
+
if st.button('🔥 Run!'):
|
| 200 |
+
with st.spinner("Loading..."):
|
| 201 |
+
preds = pos_tagger.classify(text)
|
| 202 |
+
st.success('All done!')
|
| 203 |
+
st.write("")
|
| 204 |
+
st.subheader("Part of Speech tags")
|
| 205 |
+
annotated_text(*preds)
|
| 206 |
+
st.write("")
|
| 207 |
+
st.components.v1.iframe('https://www.ling.upenn.edu/courses/Fall_2003/ling001/penn_treebank_pos.html', height=1000)
|
| 208 |
+
|
| 209 |
+
elif page == "Emotion Detection":
|
| 210 |
+
st.header('Emotion Detection')
|
| 211 |
+
# st.markdown("")
|
| 212 |
+
st.write(
|
| 213 |
+
"""
|
| 214 |
+
"""
|
| 215 |
+
)
|
| 216 |
+
|
| 217 |
+
text = st.text_area("Paste text here", value=example_text)
|
| 218 |
+
|
| 219 |
+
if st.button('🔥 Run!'):
|
| 220 |
+
with st.spinner("Loading..."):
|
| 221 |
+
preds, html = emotion_detector.run(text)
|
| 222 |
+
st.success('All done!')
|
| 223 |
+
st.write("")
|
| 224 |
+
st.subheader("Emotion Predictions")
|
| 225 |
+
st.bar_chart(data=preds, width=0, height=0, use_container_width=True)
|
| 226 |
+
raw_html = html._repr_html_()
|
| 227 |
+
st.write("")
|
| 228 |
+
st.subheader("Emotion Justification")
|
| 229 |
+
st.components.v1.html(raw_html, height=500)
|
| 230 |
+
|
| 231 |
+
elif page == "Named Entity Recognition":
|
| 232 |
+
st.header('Named Entity Recognition')
|
| 233 |
+
# st.markdown("")
|
| 234 |
+
st.write(
|
| 235 |
+
"""
|
| 236 |
+
"""
|
| 237 |
+
)
|
| 238 |
+
|
| 239 |
+
text = st.text_area("Paste text here", value=example_text)
|
| 240 |
+
|
| 241 |
+
if st.button('🔥 Run!'):
|
| 242 |
+
with st.spinner("Loading..."):
|
| 243 |
+
preds, ner_annotation = ner.classify(text)
|
| 244 |
+
st.success('All done!')
|
| 245 |
+
st.write("")
|
| 246 |
+
st.subheader("NER Predictions")
|
| 247 |
+
annotated_text(*ner_annotation)
|
| 248 |
+
st.write("")
|
| 249 |
+
st.subheader("NER Prediction Metadata")
|
| 250 |
+
st.write(preds)
|
| 251 |
+
|
| 252 |
+
elif page == "Object Detector":
|
| 253 |
+
st.header('Object Detector')
|
| 254 |
+
st.write(
|
| 255 |
+
"""
|
| 256 |
+
"""
|
| 257 |
+
)
|
| 258 |
+
|
| 259 |
+
img_file_buffer = st.file_uploader("Load an image", type=["png", "jpg", "jpeg"])
|
| 260 |
+
if img_file_buffer is not None:
|
| 261 |
+
image = np.array(Image.open(img_file_buffer))
|
| 262 |
+
|
| 263 |
+
if st.button('🔥 Run!'):
|
| 264 |
+
with st.spinner("Loading..."):
|
| 265 |
+
img, primero = objectdetector1.run_detector(image)
|
| 266 |
+
st.success('The first image detected is: ' + primero)
|
| 267 |
+
st.image(img, caption="Imagen", use_column_width=True)
|
| 268 |
+
|
| 269 |
+
elif page == "OCR Detector":
|
| 270 |
+
st.header('OCR Detector')
|
| 271 |
+
st.write(
|
| 272 |
+
"""
|
| 273 |
+
"""
|
| 274 |
+
)
|
| 275 |
+
|
| 276 |
+
file = st.file_uploader("Load an image", type=["png", "jpg", "jpeg"])
|
| 277 |
+
|
| 278 |
+
#read the csv file and display the dataframe
|
| 279 |
+
if file is not None:
|
| 280 |
+
image = Image.open(file) # read image with PIL library
|
| 281 |
+
|
| 282 |
+
if st.button('🔥 Run!'):
|
| 283 |
+
with st.spinner("Loading..."):
|
| 284 |
+
result = ocrdetector1.reader.readtext(np.array(image)) # turn image to numpy array
|
| 285 |
+
|
| 286 |
+
# collect the results in dictionary:
|
| 287 |
+
textdic_easyocr = {}
|
| 288 |
+
for idx in range(len(result)):
|
| 289 |
+
pred_coor = result[idx][0]
|
| 290 |
+
pred_text = result[idx][1]
|
| 291 |
+
pred_confidence = result[idx][2]
|
| 292 |
+
textdic_easyocr[pred_text] = {}
|
| 293 |
+
textdic_easyocr[pred_text]['pred_confidence'] = pred_confidence
|
| 294 |
+
|
| 295 |
+
# get boxes on the image
|
| 296 |
+
rectangle(image, result)
|
| 297 |
+
|
| 298 |
+
# create a dataframe which shows the predicted text and prediction confidence
|
| 299 |
+
df = pd.DataFrame.from_dict(textdic_easyocr).T
|
| 300 |
+
st.table(df)
|
| 301 |
+
elif page == "Speech & Text Emotion":
|
| 302 |
+
st.header('Speech & Text Emotion')
|
| 303 |
+
st.write(
|
| 304 |
+
"""
|
| 305 |
+
"""
|
| 306 |
+
)
|
| 307 |
+
uploaded_file = st.file_uploader("Choose an audio file", type=["mp3", "wav", "ogg"])
|
| 308 |
+
|
| 309 |
+
if uploaded_file is not None:
|
| 310 |
+
st.audio(uploaded_file, format='audio/' + uploaded_file.type.split('/')[1])
|
| 311 |
+
st.write("Audio file uploaded and playing.")
|
| 312 |
+
|
| 313 |
+
else:
|
| 314 |
+
st.write("Please upload an audio file.")
|
| 315 |
+
|
| 316 |
+
if st.button("Analysis"):
|
| 317 |
+
with st.spinner("Loading..."):
|
| 318 |
+
st.header('Results of the Audio & Text analysis:')
|
| 319 |
+
samples, sample_rate = librosa.load(uploaded_file, sr=16000)
|
| 320 |
+
p_voice2text = infere_voice2text (samples)
|
| 321 |
+
p_speechemotion = infere_speech_emotion(samples)
|
| 322 |
+
p_textemotion = infere_text_emotion(p_voice2text)
|
| 323 |
+
st.subheader("Text from the Audio:")
|
| 324 |
+
st.write(p_voice2text)
|
| 325 |
+
st.write("---")
|
| 326 |
+
st.subheader("Speech emotion:")
|
| 327 |
+
st.write(p_speechemotion)
|
| 328 |
+
st.write("---")
|
| 329 |
+
st.subheader("Text emotion:")
|
| 330 |
+
st.write(p_textemotion)
|
| 331 |
+
st.write("---")
|
| 332 |
+
|
| 333 |
+
try:
|
| 334 |
+
main()
|
| 335 |
+
except Exception as e:
|
| 336 |
+
st.sidebar.error(f"An error occurred: {e}")
|
| 337 |
+
|
app_v3.txt
ADDED
|
@@ -0,0 +1,320 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import pandas as pd
|
| 3 |
+
import numpy as np
|
| 4 |
+
import easyocr
|
| 5 |
+
import streamlit as st
|
| 6 |
+
from annotated_text import annotated_text
|
| 7 |
+
from streamlit_option_menu import option_menu
|
| 8 |
+
from sentiment_analysis import SentimentAnalysis
|
| 9 |
+
from keyword_extraction import KeywordExtractor
|
| 10 |
+
from part_of_speech_tagging import POSTagging
|
| 11 |
+
from emotion_detection import EmotionDetection
|
| 12 |
+
from named_entity_recognition import NamedEntityRecognition
|
| 13 |
+
from Object_Detector import ObjectDetector
|
| 14 |
+
from OCR_Detector import OCRDetector
|
| 15 |
+
import PIL
|
| 16 |
+
from PIL import Image
|
| 17 |
+
from PIL import ImageColor
|
| 18 |
+
from PIL import ImageDraw
|
| 19 |
+
from PIL import ImageFont
|
| 20 |
+
import time
|
| 21 |
+
|
| 22 |
+
# Imports de Object Detection
|
| 23 |
+
import tensorflow as tf
|
| 24 |
+
import tensorflow_hub as hub
|
| 25 |
+
# Load compressed models from tensorflow_hub
|
| 26 |
+
os.environ['TFHUB_MODEL_LOAD_FORMAT'] = 'COMPRESSED'
|
| 27 |
+
import matplotlib.pyplot as plt
|
| 28 |
+
import matplotlib as mpl
|
| 29 |
+
# For drawing onto the image.
|
| 30 |
+
import numpy as np
|
| 31 |
+
from tensorflow.python.ops.numpy_ops import np_config
|
| 32 |
+
np_config.enable_numpy_behavior()
|
| 33 |
+
|
| 34 |
+
import torch
|
| 35 |
+
import librosa
|
| 36 |
+
from models import infere_speech_emotion, infere_text_emotion, infere_voice2text
|
| 37 |
+
|
| 38 |
+
st.set_page_config(layout="wide")
|
| 39 |
+
|
| 40 |
+
hide_streamlit_style = """
|
| 41 |
+
<style>
|
| 42 |
+
#MainMenu {visibility: hidden;}
|
| 43 |
+
footer {visibility: hidden;}
|
| 44 |
+
</style>
|
| 45 |
+
"""
|
| 46 |
+
st.markdown(hide_streamlit_style, unsafe_allow_html=True)
|
| 47 |
+
|
| 48 |
+
@st.cache_resource
|
| 49 |
+
def load_sentiment_model():
|
| 50 |
+
return SentimentAnalysis()
|
| 51 |
+
|
| 52 |
+
@st.cache_resource
|
| 53 |
+
def load_keyword_model():
|
| 54 |
+
return KeywordExtractor()
|
| 55 |
+
|
| 56 |
+
@st.cache_resource
|
| 57 |
+
def load_pos_model():
|
| 58 |
+
return POSTagging()
|
| 59 |
+
|
| 60 |
+
@st.cache_resource
|
| 61 |
+
def load_emotion_model():
|
| 62 |
+
return EmotionDetection()
|
| 63 |
+
|
| 64 |
+
@st.cache_resource
|
| 65 |
+
def load_ner_model():
|
| 66 |
+
return NamedEntityRecognition()
|
| 67 |
+
|
| 68 |
+
@st.cache_resource
|
| 69 |
+
def load_objectdetector_model():
|
| 70 |
+
return ObjectDetector()
|
| 71 |
+
|
| 72 |
+
@st.cache_resource
|
| 73 |
+
def load_ocrdetector_model():
|
| 74 |
+
return OCRDetector()
|
| 75 |
+
|
| 76 |
+
sentiment_analyzer = load_sentiment_model()
|
| 77 |
+
keyword_extractor = load_keyword_model()
|
| 78 |
+
pos_tagger = load_pos_model()
|
| 79 |
+
emotion_detector = load_emotion_model()
|
| 80 |
+
ner = load_ner_model()
|
| 81 |
+
objectdetector1 = load_objectdetector_model()
|
| 82 |
+
ocrdetector1 = load_ocrdetector_model()
|
| 83 |
+
|
| 84 |
+
def rectangle(image, result):
|
| 85 |
+
draw = ImageDraw.Draw(image)
|
| 86 |
+
for res in result:
|
| 87 |
+
top_left = tuple(res[0][0]) # top left coordinates as tuple
|
| 88 |
+
bottom_right = tuple(res[0][2]) # bottom right coordinates as tuple
|
| 89 |
+
draw.rectangle((top_left, bottom_right), outline="blue", width=2)
|
| 90 |
+
st.image(image)
|
| 91 |
+
|
| 92 |
+
example_text = "My name is Daniel: The attention to detail, swift resolution, and accuracy demonstrated by ITACA Insurance Company in Spain in handling my claim were truly impressive. This undoubtedly reflects their commitment to being a customer-centric insurance provider."
|
| 93 |
+
|
| 94 |
+
with st.sidebar:
|
| 95 |
+
image = Image.open('./itaca_logo.png')
|
| 96 |
+
st.image(image,width=150) #use_column_width=True)
|
| 97 |
+
page = option_menu(menu_title='Menu',
|
| 98 |
+
menu_icon="robot",
|
| 99 |
+
options=["Sentiment Analysis",
|
| 100 |
+
"Keyword Extraction",
|
| 101 |
+
"Part of Speech Tagging",
|
| 102 |
+
"Emotion Detection",
|
| 103 |
+
"Named Entity Recognition",
|
| 104 |
+
"Speech & Text Emotion",
|
| 105 |
+
"Object Detector",
|
| 106 |
+
"OCR Detector"],
|
| 107 |
+
icons=["chat-dots",
|
| 108 |
+
"key",
|
| 109 |
+
"tag",
|
| 110 |
+
"emoji-heart-eyes",
|
| 111 |
+
"building",
|
| 112 |
+
"book",
|
| 113 |
+
"camera",
|
| 114 |
+
"list-task"],
|
| 115 |
+
default_index=0
|
| 116 |
+
)
|
| 117 |
+
|
| 118 |
+
st.title('ITACA Insurance Core AI Module')
|
| 119 |
+
|
| 120 |
+
# Replace '20px' with your desired font size
|
| 121 |
+
font_size = '20px'
|
| 122 |
+
|
| 123 |
+
if page == "Sentiment Analysis":
|
| 124 |
+
st.header('Sentiment Analysis')
|
| 125 |
+
# st.markdown("")
|
| 126 |
+
st.write(
|
| 127 |
+
"""
|
| 128 |
+
"""
|
| 129 |
+
)
|
| 130 |
+
|
| 131 |
+
text = st.text_area("Paste text here", value=example_text)
|
| 132 |
+
|
| 133 |
+
if st.button('🔥 Run!'):
|
| 134 |
+
with st.spinner("Loading..."):
|
| 135 |
+
preds, html = sentiment_analyzer.run(text)
|
| 136 |
+
st.success('All done!')
|
| 137 |
+
st.write("")
|
| 138 |
+
st.subheader("Sentiment Predictions")
|
| 139 |
+
st.bar_chart(data=preds, width=0, height=0, use_container_width=True)
|
| 140 |
+
st.write("")
|
| 141 |
+
st.subheader("Sentiment Justification")
|
| 142 |
+
raw_html = html._repr_html_()
|
| 143 |
+
st.components.v1.html(raw_html, height=500)
|
| 144 |
+
|
| 145 |
+
elif page == "Keyword Extraction":
|
| 146 |
+
st.header('Keyword Extraction')
|
| 147 |
+
# st.markdown("")
|
| 148 |
+
st.write(
|
| 149 |
+
"""
|
| 150 |
+
"""
|
| 151 |
+
)
|
| 152 |
+
|
| 153 |
+
text = st.text_area("Paste text here", value=example_text)
|
| 154 |
+
|
| 155 |
+
max_keywords = st.slider('# of Keywords Max Limit', min_value=1, max_value=10, value=5, step=1)
|
| 156 |
+
|
| 157 |
+
if st.button('🔥 Run!'):
|
| 158 |
+
with st.spinner("Loading..."):
|
| 159 |
+
annotation, keywords = keyword_extractor.generate(text, max_keywords)
|
| 160 |
+
st.success('All done!')
|
| 161 |
+
|
| 162 |
+
if annotation:
|
| 163 |
+
st.subheader("Keyword Annotation")
|
| 164 |
+
st.write("")
|
| 165 |
+
annotated_text(*annotation)
|
| 166 |
+
st.text("")
|
| 167 |
+
|
| 168 |
+
st.subheader("Extracted Keywords")
|
| 169 |
+
st.write("")
|
| 170 |
+
df = pd.DataFrame(keywords, columns=['Extracted Keywords'])
|
| 171 |
+
csv = df.to_csv(index=False).encode('utf-8')
|
| 172 |
+
st.download_button('Download Keywords to CSV', csv, file_name='news_intelligence_keywords.csv')
|
| 173 |
+
|
| 174 |
+
data_table = st.table(df)
|
| 175 |
+
|
| 176 |
+
elif page == "Part of Speech Tagging":
|
| 177 |
+
st.header('Part of Speech Tagging')
|
| 178 |
+
# st.markdown("")
|
| 179 |
+
st.write(
|
| 180 |
+
"""
|
| 181 |
+
"""
|
| 182 |
+
)
|
| 183 |
+
|
| 184 |
+
text = st.text_area("Paste text here", value=example_text)
|
| 185 |
+
|
| 186 |
+
if st.button('🔥 Run!'):
|
| 187 |
+
with st.spinner("Loading..."):
|
| 188 |
+
preds = pos_tagger.classify(text)
|
| 189 |
+
st.success('All done!')
|
| 190 |
+
st.write("")
|
| 191 |
+
st.subheader("Part of Speech tags")
|
| 192 |
+
annotated_text(*preds)
|
| 193 |
+
st.write("")
|
| 194 |
+
st.components.v1.iframe('https://www.ling.upenn.edu/courses/Fall_2003/ling001/penn_treebank_pos.html', height=1000)
|
| 195 |
+
|
| 196 |
+
elif page == "Emotion Detection":
|
| 197 |
+
st.header('Emotion Detection')
|
| 198 |
+
# st.markdown("")
|
| 199 |
+
st.write(
|
| 200 |
+
"""
|
| 201 |
+
"""
|
| 202 |
+
)
|
| 203 |
+
|
| 204 |
+
text = st.text_area("Paste text here", value=example_text)
|
| 205 |
+
|
| 206 |
+
if st.button('🔥 Run!'):
|
| 207 |
+
with st.spinner("Loading..."):
|
| 208 |
+
preds, html = emotion_detector.run(text)
|
| 209 |
+
st.success('All done!')
|
| 210 |
+
st.write("")
|
| 211 |
+
st.subheader("Emotion Predictions")
|
| 212 |
+
st.bar_chart(data=preds, width=0, height=0, use_container_width=True)
|
| 213 |
+
raw_html = html._repr_html_()
|
| 214 |
+
st.write("")
|
| 215 |
+
st.subheader("Emotion Justification")
|
| 216 |
+
st.components.v1.html(raw_html, height=500)
|
| 217 |
+
|
| 218 |
+
elif page == "Named Entity Recognition":
|
| 219 |
+
st.header('Named Entity Recognition')
|
| 220 |
+
# st.markdown("")
|
| 221 |
+
st.write(
|
| 222 |
+
"""
|
| 223 |
+
"""
|
| 224 |
+
)
|
| 225 |
+
|
| 226 |
+
text = st.text_area("Paste text here", value=example_text)
|
| 227 |
+
|
| 228 |
+
if st.button('🔥 Run!'):
|
| 229 |
+
with st.spinner("Loading..."):
|
| 230 |
+
preds, ner_annotation = ner.classify(text)
|
| 231 |
+
st.success('All done!')
|
| 232 |
+
st.write("")
|
| 233 |
+
st.subheader("NER Predictions")
|
| 234 |
+
annotated_text(*ner_annotation)
|
| 235 |
+
st.write("")
|
| 236 |
+
st.subheader("NER Prediction Metadata")
|
| 237 |
+
st.write(preds)
|
| 238 |
+
|
| 239 |
+
elif page == "Object Detector":
|
| 240 |
+
st.header('Object Detector')
|
| 241 |
+
st.write(
|
| 242 |
+
"""
|
| 243 |
+
"""
|
| 244 |
+
)
|
| 245 |
+
|
| 246 |
+
img_file_buffer = st.file_uploader("Load an image", type=["png", "jpg", "jpeg"])
|
| 247 |
+
if img_file_buffer is not None:
|
| 248 |
+
image = np.array(Image.open(img_file_buffer))
|
| 249 |
+
|
| 250 |
+
if st.button('🔥 Run!'):
|
| 251 |
+
with st.spinner("Loading..."):
|
| 252 |
+
img, primero = objectdetector1.run_detector(image)
|
| 253 |
+
st.success('The first image detected is: ' + primero)
|
| 254 |
+
st.image(img, caption="Imagen", use_column_width=True)
|
| 255 |
+
|
| 256 |
+
elif page == "OCR Detector":
|
| 257 |
+
st.header('OCR Detector')
|
| 258 |
+
st.write(
|
| 259 |
+
"""
|
| 260 |
+
"""
|
| 261 |
+
)
|
| 262 |
+
|
| 263 |
+
file = st.file_uploader("Load an image", type=["png", "jpg", "jpeg"])
|
| 264 |
+
|
| 265 |
+
#read the csv file and display the dataframe
|
| 266 |
+
if file is not None:
|
| 267 |
+
image = Image.open(file) # read image with PIL library
|
| 268 |
+
|
| 269 |
+
if st.button('🔥 Run!'):
|
| 270 |
+
with st.spinner("Loading..."):
|
| 271 |
+
result = ocrdetector1.reader.readtext(np.array(image)) # turn image to numpy array
|
| 272 |
+
|
| 273 |
+
# collect the results in dictionary:
|
| 274 |
+
textdic_easyocr = {}
|
| 275 |
+
for idx in range(len(result)):
|
| 276 |
+
pred_coor = result[idx][0]
|
| 277 |
+
pred_text = result[idx][1]
|
| 278 |
+
pred_confidence = result[idx][2]
|
| 279 |
+
textdic_easyocr[pred_text] = {}
|
| 280 |
+
textdic_easyocr[pred_text]['pred_confidence'] = pred_confidence
|
| 281 |
+
|
| 282 |
+
# get boxes on the image
|
| 283 |
+
rectangle(image, result)
|
| 284 |
+
|
| 285 |
+
# create a dataframe which shows the predicted text and prediction confidence
|
| 286 |
+
df = pd.DataFrame.from_dict(textdic_easyocr).T
|
| 287 |
+
st.table(df)
|
| 288 |
+
elif page == "Speech & Text Emotion":
|
| 289 |
+
st.header('Speech & Text Emotion')
|
| 290 |
+
st.write(
|
| 291 |
+
"""
|
| 292 |
+
"""
|
| 293 |
+
)
|
| 294 |
+
uploaded_file = st.file_uploader("Choose an audio file", type=["mp3", "wav", "ogg"])
|
| 295 |
+
|
| 296 |
+
if uploaded_file is not None:
|
| 297 |
+
st.audio(uploaded_file, format='audio/' + uploaded_file.type.split('/')[1])
|
| 298 |
+
st.write("Audio file uploaded and playing.")
|
| 299 |
+
|
| 300 |
+
else:
|
| 301 |
+
st.write("Please upload an audio file.")
|
| 302 |
+
|
| 303 |
+
if st.button("Analysis"):
|
| 304 |
+
with st.spinner("Loading..."):
|
| 305 |
+
st.header('Results of the Audio & Text analysis:')
|
| 306 |
+
samples, sample_rate = librosa.load(uploaded_file, sr=16000)
|
| 307 |
+
p_voice2text = infere_voice2text (samples)
|
| 308 |
+
p_speechemotion = infere_speech_emotion(samples)
|
| 309 |
+
p_textemotion = infere_text_emotion(p_voice2text)
|
| 310 |
+
st.subheader("Text from the Audio:")
|
| 311 |
+
st.write(p_voice2text)
|
| 312 |
+
st.write("---")
|
| 313 |
+
st.subheader("Speech emotion:")
|
| 314 |
+
st.write(p_speechemotion)
|
| 315 |
+
st.write("---")
|
| 316 |
+
st.subheader("Text emotion:")
|
| 317 |
+
st.write(p_textemotion)
|
| 318 |
+
st.write("---")
|
| 319 |
+
|
| 320 |
+
|
appv2.py
ADDED
|
@@ -0,0 +1,320 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import pandas as pd
|
| 3 |
+
import numpy as np
|
| 4 |
+
import easyocr
|
| 5 |
+
import streamlit as st
|
| 6 |
+
from annotated_text import annotated_text
|
| 7 |
+
from streamlit_option_menu import option_menu
|
| 8 |
+
from sentiment_analysis import SentimentAnalysis
|
| 9 |
+
from keyword_extraction import KeywordExtractor
|
| 10 |
+
from part_of_speech_tagging import POSTagging
|
| 11 |
+
from emotion_detection import EmotionDetection
|
| 12 |
+
from named_entity_recognition import NamedEntityRecognition
|
| 13 |
+
from Object_Detector import ObjectDetector
|
| 14 |
+
from OCR_Detector import OCRDetector
|
| 15 |
+
import PIL
|
| 16 |
+
from PIL import Image
|
| 17 |
+
from PIL import ImageColor
|
| 18 |
+
from PIL import ImageDraw
|
| 19 |
+
from PIL import ImageFont
|
| 20 |
+
import time
|
| 21 |
+
|
| 22 |
+
# Imports de Object Detection
|
| 23 |
+
import tensorflow as tf
|
| 24 |
+
import tensorflow_hub as hub
|
| 25 |
+
# Load compressed models from tensorflow_hub
|
| 26 |
+
os.environ['TFHUB_MODEL_LOAD_FORMAT'] = 'COMPRESSED'
|
| 27 |
+
import matplotlib.pyplot as plt
|
| 28 |
+
import matplotlib as mpl
|
| 29 |
+
# For drawing onto the image.
|
| 30 |
+
import numpy as np
|
| 31 |
+
from tensorflow.python.ops.numpy_ops import np_config
|
| 32 |
+
np_config.enable_numpy_behavior()
|
| 33 |
+
|
| 34 |
+
import torch
|
| 35 |
+
import librosa
|
| 36 |
+
from models import infere_speech_emotion, infere_text_emotion, infere_voice2text
|
| 37 |
+
|
| 38 |
+
st.set_page_config(layout="wide")
|
| 39 |
+
|
| 40 |
+
hide_streamlit_style = """
|
| 41 |
+
<style>
|
| 42 |
+
#MainMenu {visibility: hidden;}
|
| 43 |
+
footer {visibility: hidden;}
|
| 44 |
+
</style>
|
| 45 |
+
"""
|
| 46 |
+
st.markdown(hide_streamlit_style, unsafe_allow_html=True)
|
| 47 |
+
|
| 48 |
+
@st.cache_resource
|
| 49 |
+
def load_sentiment_model():
|
| 50 |
+
return SentimentAnalysis()
|
| 51 |
+
|
| 52 |
+
@st.cache_resource
|
| 53 |
+
def load_keyword_model():
|
| 54 |
+
return KeywordExtractor()
|
| 55 |
+
|
| 56 |
+
@st.cache_resource
|
| 57 |
+
def load_pos_model():
|
| 58 |
+
return POSTagging()
|
| 59 |
+
|
| 60 |
+
@st.cache_resource
|
| 61 |
+
def load_emotion_model():
|
| 62 |
+
return EmotionDetection()
|
| 63 |
+
|
| 64 |
+
@st.cache_resource
|
| 65 |
+
def load_ner_model():
|
| 66 |
+
return NamedEntityRecognition()
|
| 67 |
+
|
| 68 |
+
@st.cache_resource
|
| 69 |
+
def load_objectdetector_model():
|
| 70 |
+
return ObjectDetector()
|
| 71 |
+
|
| 72 |
+
@st.cache_resource
|
| 73 |
+
def load_ocrdetector_model():
|
| 74 |
+
return OCRDetector()
|
| 75 |
+
|
| 76 |
+
sentiment_analyzer = load_sentiment_model()
|
| 77 |
+
keyword_extractor = load_keyword_model()
|
| 78 |
+
pos_tagger = load_pos_model()
|
| 79 |
+
emotion_detector = load_emotion_model()
|
| 80 |
+
ner = load_ner_model()
|
| 81 |
+
objectdetector1 = load_objectdetector_model()
|
| 82 |
+
ocrdetector1 = load_ocrdetector_model()
|
| 83 |
+
|
| 84 |
+
def rectangle(image, result):
|
| 85 |
+
draw = ImageDraw.Draw(image)
|
| 86 |
+
for res in result:
|
| 87 |
+
top_left = tuple(res[0][0]) # top left coordinates as tuple
|
| 88 |
+
bottom_right = tuple(res[0][2]) # bottom right coordinates as tuple
|
| 89 |
+
draw.rectangle((top_left, bottom_right), outline="blue", width=2)
|
| 90 |
+
st.image(image)
|
| 91 |
+
|
| 92 |
+
example_text = "My name is Daniel: The attention to detail, swift resolution, and accuracy demonstrated by ITACA Insurance Company in Spain in handling my claim were truly impressive. This undoubtedly reflects their commitment to being a customer-centric insurance provider."
|
| 93 |
+
|
| 94 |
+
with st.sidebar:
|
| 95 |
+
image = Image.open('./itaca_logo.png')
|
| 96 |
+
st.image(image,width=150) #use_column_width=True)
|
| 97 |
+
page = option_menu(menu_title='Menu',
|
| 98 |
+
menu_icon="robot",
|
| 99 |
+
options=["Sentiment Analysis",
|
| 100 |
+
"Keyword Extraction",
|
| 101 |
+
"Part of Speech Tagging",
|
| 102 |
+
"Emotion Detection",
|
| 103 |
+
"Named Entity Recognition",
|
| 104 |
+
"Speech & Text Emotion",
|
| 105 |
+
"Object Detector",
|
| 106 |
+
"OCR Detector"],
|
| 107 |
+
icons=["chat-dots",
|
| 108 |
+
"key",
|
| 109 |
+
"tag",
|
| 110 |
+
"emoji-heart-eyes",
|
| 111 |
+
"building",
|
| 112 |
+
"book",
|
| 113 |
+
"camera",
|
| 114 |
+
"list-task"],
|
| 115 |
+
default_index=0
|
| 116 |
+
)
|
| 117 |
+
|
| 118 |
+
st.title('ITACA Insurance Core AI Module')
|
| 119 |
+
|
| 120 |
+
# Replace '20px' with your desired font size
|
| 121 |
+
font_size = '20px'
|
| 122 |
+
|
| 123 |
+
if page == "Sentiment Analysis":
|
| 124 |
+
st.header('Sentiment Analysis')
|
| 125 |
+
# st.markdown("")
|
| 126 |
+
st.write(
|
| 127 |
+
"""
|
| 128 |
+
"""
|
| 129 |
+
)
|
| 130 |
+
|
| 131 |
+
text = st.text_area("Paste text here", value=example_text)
|
| 132 |
+
|
| 133 |
+
if st.button('🔥 Run!'):
|
| 134 |
+
with st.spinner("Loading..."):
|
| 135 |
+
preds, html = sentiment_analyzer.run(text)
|
| 136 |
+
st.success('All done!')
|
| 137 |
+
st.write("")
|
| 138 |
+
st.subheader("Sentiment Predictions")
|
| 139 |
+
st.bar_chart(data=preds, width=0, height=0, use_container_width=True)
|
| 140 |
+
st.write("")
|
| 141 |
+
st.subheader("Sentiment Justification")
|
| 142 |
+
raw_html = html._repr_html_()
|
| 143 |
+
st.components.v1.html(raw_html, height=500)
|
| 144 |
+
|
| 145 |
+
elif page == "Keyword Extraction":
|
| 146 |
+
st.header('Keyword Extraction')
|
| 147 |
+
# st.markdown("")
|
| 148 |
+
st.write(
|
| 149 |
+
"""
|
| 150 |
+
"""
|
| 151 |
+
)
|
| 152 |
+
|
| 153 |
+
text = st.text_area("Paste text here", value=example_text)
|
| 154 |
+
|
| 155 |
+
max_keywords = st.slider('# of Keywords Max Limit', min_value=1, max_value=10, value=5, step=1)
|
| 156 |
+
|
| 157 |
+
if st.button('🔥 Run!'):
|
| 158 |
+
with st.spinner("Loading..."):
|
| 159 |
+
annotation, keywords = keyword_extractor.generate(text, max_keywords)
|
| 160 |
+
st.success('All done!')
|
| 161 |
+
|
| 162 |
+
if annotation:
|
| 163 |
+
st.subheader("Keyword Annotation")
|
| 164 |
+
st.write("")
|
| 165 |
+
annotated_text(*annotation)
|
| 166 |
+
st.text("")
|
| 167 |
+
|
| 168 |
+
st.subheader("Extracted Keywords")
|
| 169 |
+
st.write("")
|
| 170 |
+
df = pd.DataFrame(keywords, columns=['Extracted Keywords'])
|
| 171 |
+
csv = df.to_csv(index=False).encode('utf-8')
|
| 172 |
+
st.download_button('Download Keywords to CSV', csv, file_name='news_intelligence_keywords.csv')
|
| 173 |
+
|
| 174 |
+
data_table = st.table(df)
|
| 175 |
+
|
| 176 |
+
elif page == "Part of Speech Tagging":
|
| 177 |
+
st.header('Part of Speech Tagging')
|
| 178 |
+
# st.markdown("")
|
| 179 |
+
st.write(
|
| 180 |
+
"""
|
| 181 |
+
"""
|
| 182 |
+
)
|
| 183 |
+
|
| 184 |
+
text = st.text_area("Paste text here", value=example_text)
|
| 185 |
+
|
| 186 |
+
if st.button('🔥 Run!'):
|
| 187 |
+
with st.spinner("Loading..."):
|
| 188 |
+
preds = pos_tagger.classify(text)
|
| 189 |
+
st.success('All done!')
|
| 190 |
+
st.write("")
|
| 191 |
+
st.subheader("Part of Speech tags")
|
| 192 |
+
annotated_text(*preds)
|
| 193 |
+
st.write("")
|
| 194 |
+
st.components.v1.iframe('https://www.ling.upenn.edu/courses/Fall_2003/ling001/penn_treebank_pos.html', height=1000)
|
| 195 |
+
|
| 196 |
+
elif page == "Emotion Detection":
|
| 197 |
+
st.header('Emotion Detection')
|
| 198 |
+
# st.markdown("")
|
| 199 |
+
st.write(
|
| 200 |
+
"""
|
| 201 |
+
"""
|
| 202 |
+
)
|
| 203 |
+
|
| 204 |
+
text = st.text_area("Paste text here", value=example_text)
|
| 205 |
+
|
| 206 |
+
if st.button('🔥 Run!'):
|
| 207 |
+
with st.spinner("Loading..."):
|
| 208 |
+
preds, html = emotion_detector.run(text)
|
| 209 |
+
st.success('All done!')
|
| 210 |
+
st.write("")
|
| 211 |
+
st.subheader("Emotion Predictions")
|
| 212 |
+
st.bar_chart(data=preds, width=0, height=0, use_container_width=True)
|
| 213 |
+
raw_html = html._repr_html_()
|
| 214 |
+
st.write("")
|
| 215 |
+
st.subheader("Emotion Justification")
|
| 216 |
+
st.components.v1.html(raw_html, height=500)
|
| 217 |
+
|
| 218 |
+
elif page == "Named Entity Recognition":
|
| 219 |
+
st.header('Named Entity Recognition')
|
| 220 |
+
# st.markdown("")
|
| 221 |
+
st.write(
|
| 222 |
+
"""
|
| 223 |
+
"""
|
| 224 |
+
)
|
| 225 |
+
|
| 226 |
+
text = st.text_area("Paste text here", value=example_text)
|
| 227 |
+
|
| 228 |
+
if st.button('🔥 Run!'):
|
| 229 |
+
with st.spinner("Loading..."):
|
| 230 |
+
preds, ner_annotation = ner.classify(text)
|
| 231 |
+
st.success('All done!')
|
| 232 |
+
st.write("")
|
| 233 |
+
st.subheader("NER Predictions")
|
| 234 |
+
annotated_text(*ner_annotation)
|
| 235 |
+
st.write("")
|
| 236 |
+
st.subheader("NER Prediction Metadata")
|
| 237 |
+
st.write(preds)
|
| 238 |
+
|
| 239 |
+
elif page == "Object Detector":
|
| 240 |
+
st.header('Object Detector')
|
| 241 |
+
st.write(
|
| 242 |
+
"""
|
| 243 |
+
"""
|
| 244 |
+
)
|
| 245 |
+
|
| 246 |
+
img_file_buffer = st.file_uploader("Load an image", type=["png", "jpg", "jpeg"])
|
| 247 |
+
if img_file_buffer is not None:
|
| 248 |
+
image = np.array(Image.open(img_file_buffer))
|
| 249 |
+
|
| 250 |
+
if st.button('🔥 Run!'):
|
| 251 |
+
with st.spinner("Loading..."):
|
| 252 |
+
img, primero = objectdetector1.run_detector(image)
|
| 253 |
+
st.success('The first image detected is: ' + primero)
|
| 254 |
+
st.image(img, caption="Imagen", use_column_width=True)
|
| 255 |
+
|
| 256 |
+
elif page == "OCR Detector":
|
| 257 |
+
st.header('OCR Detector')
|
| 258 |
+
st.write(
|
| 259 |
+
"""
|
| 260 |
+
"""
|
| 261 |
+
)
|
| 262 |
+
|
| 263 |
+
file = st.file_uploader("Load an image", type=["png", "jpg", "jpeg"])
|
| 264 |
+
|
| 265 |
+
#read the csv file and display the dataframe
|
| 266 |
+
if file is not None:
|
| 267 |
+
image = Image.open(file) # read image with PIL library
|
| 268 |
+
|
| 269 |
+
if st.button('🔥 Run!'):
|
| 270 |
+
with st.spinner("Loading..."):
|
| 271 |
+
result = ocrdetector1.reader.readtext(np.array(image)) # turn image to numpy array
|
| 272 |
+
|
| 273 |
+
# collect the results in dictionary:
|
| 274 |
+
textdic_easyocr = {}
|
| 275 |
+
for idx in range(len(result)):
|
| 276 |
+
pred_coor = result[idx][0]
|
| 277 |
+
pred_text = result[idx][1]
|
| 278 |
+
pred_confidence = result[idx][2]
|
| 279 |
+
textdic_easyocr[pred_text] = {}
|
| 280 |
+
textdic_easyocr[pred_text]['pred_confidence'] = pred_confidence
|
| 281 |
+
|
| 282 |
+
# get boxes on the image
|
| 283 |
+
rectangle(image, result)
|
| 284 |
+
|
| 285 |
+
# create a dataframe which shows the predicted text and prediction confidence
|
| 286 |
+
df = pd.DataFrame.from_dict(textdic_easyocr).T
|
| 287 |
+
st.table(df)
|
| 288 |
+
elif page == "Speech & Text Emotion":
|
| 289 |
+
st.header('Speech & Text Emotion')
|
| 290 |
+
st.write(
|
| 291 |
+
"""
|
| 292 |
+
"""
|
| 293 |
+
)
|
| 294 |
+
uploaded_file = st.file_uploader("Choose an audio file", type=["mp3", "wav", "ogg"])
|
| 295 |
+
|
| 296 |
+
if uploaded_file is not None:
|
| 297 |
+
st.audio(uploaded_file, format='audio/' + uploaded_file.type.split('/')[1])
|
| 298 |
+
st.write("Audio file uploaded and playing.")
|
| 299 |
+
|
| 300 |
+
else:
|
| 301 |
+
st.write("Please upload an audio file.")
|
| 302 |
+
|
| 303 |
+
if st.button("Analysis"):
|
| 304 |
+
with st.spinner("Loading..."):
|
| 305 |
+
st.header('Results of the Audio & Text analysis:')
|
| 306 |
+
samples, sample_rate = librosa.load(uploaded_file, sr=16000)
|
| 307 |
+
p_voice2text = infere_voice2text (samples)
|
| 308 |
+
p_speechemotion = infere_speech_emotion(samples)
|
| 309 |
+
p_textemotion = infere_text_emotion(p_voice2text)
|
| 310 |
+
st.subheader("Text from the Audio:")
|
| 311 |
+
st.write(p_voice2text)
|
| 312 |
+
st.write("---")
|
| 313 |
+
st.subheader("Speech emotion:")
|
| 314 |
+
st.write(p_speechemotion)
|
| 315 |
+
st.write("---")
|
| 316 |
+
st.subheader("Text emotion:")
|
| 317 |
+
st.write(p_textemotion)
|
| 318 |
+
st.write("---")
|
| 319 |
+
|
| 320 |
+
|
detect_language.py
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from transformers import AutoTokenizer, AutoModelForSequenceClassification
|
| 2 |
+
|
| 3 |
+
class LanguageDetector:
|
| 4 |
+
|
| 5 |
+
def __init__(self):
|
| 6 |
+
# Download the model file
|
| 7 |
+
#model_path = hf_hub_download("facebook/fasttext-language-identification", "model.bin")
|
| 8 |
+
# Load the FastText model
|
| 9 |
+
#self.model = fasttext.load_model(model_path)
|
| 10 |
+
|
| 11 |
+
self.tokenizer = AutoTokenizer.from_pretrained("papluca/xlm-roberta-base-language-detection")
|
| 12 |
+
self.model = AutoModelForSequenceClassification.from_pretrained("papluca/xlm-roberta-base-language-detection")
|
| 13 |
+
|
| 14 |
+
# Function to predict the language of a text
|
| 15 |
+
def predict_language(self, text):
|
| 16 |
+
# Tokenize the input text
|
| 17 |
+
inputs = self.tokenizer(text, return_tensors="pt")
|
| 18 |
+
|
| 19 |
+
# Get the model's predictions
|
| 20 |
+
outputs = self.model(**inputs)
|
| 21 |
+
|
| 22 |
+
# Find the index of the highest score
|
| 23 |
+
prediction_idx = outputs.logits.argmax(dim=-1).item()
|
| 24 |
+
|
| 25 |
+
# Convert the index to the corresponding language code using the model's config.id2label
|
| 26 |
+
language_code = self.model.config.id2label[prediction_idx]
|
| 27 |
+
|
| 28 |
+
return language_code
|
| 29 |
+
|
emotion_detection.py
ADDED
|
@@ -0,0 +1,67 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from transformers import AutoTokenizer, AutoModelForSequenceClassification
|
| 2 |
+
from transformers_interpret import SequenceClassificationExplainer
|
| 3 |
+
import torch
|
| 4 |
+
import pandas as pd
|
| 5 |
+
|
| 6 |
+
|
| 7 |
+
class EmotionDetection:
|
| 8 |
+
"""
|
| 9 |
+
Emotion Detection on text data.
|
| 10 |
+
Attributes:
|
| 11 |
+
tokenizer: An instance of Hugging Face Tokenizer
|
| 12 |
+
model: An instance of Hugging Face Model
|
| 13 |
+
explainer: An instance of SequenceClassificationExplainer from Transformers interpret
|
| 14 |
+
"""
|
| 15 |
+
|
| 16 |
+
def __init__(self):
|
| 17 |
+
hub_location = 'cardiffnlp/twitter-roberta-base-emotion'
|
| 18 |
+
self.tokenizer = AutoTokenizer.from_pretrained(hub_location)
|
| 19 |
+
self.model = AutoModelForSequenceClassification.from_pretrained(hub_location)
|
| 20 |
+
self.explainer = SequenceClassificationExplainer(self.model, self.tokenizer)
|
| 21 |
+
|
| 22 |
+
def justify(self, text):
|
| 23 |
+
"""
|
| 24 |
+
Get html annotation for displaying emotion justification over text.
|
| 25 |
+
Parameters:
|
| 26 |
+
text (str): The user input string to emotion justification
|
| 27 |
+
Returns:
|
| 28 |
+
html (hmtl): html object for plotting emotion prediction justification
|
| 29 |
+
"""
|
| 30 |
+
|
| 31 |
+
word_attributions = self.explainer(text)
|
| 32 |
+
html = self.explainer.visualize("example.html")
|
| 33 |
+
|
| 34 |
+
return html
|
| 35 |
+
|
| 36 |
+
def classify(self, text):
|
| 37 |
+
"""
|
| 38 |
+
Recognize Emotion in text.
|
| 39 |
+
Parameters:
|
| 40 |
+
text (str): The user input string to perform emotion classification on
|
| 41 |
+
Returns:
|
| 42 |
+
predictions (str): The predicted probabilities for emotion classes
|
| 43 |
+
"""
|
| 44 |
+
|
| 45 |
+
tokens = self.tokenizer.encode_plus(text, add_special_tokens=False, return_tensors='pt')
|
| 46 |
+
outputs = self.model(**tokens)
|
| 47 |
+
probs = torch.nn.functional.softmax(outputs[0], dim=-1)
|
| 48 |
+
probs = probs.mean(dim=0).detach().numpy()
|
| 49 |
+
labels = list(self.model.config.id2label.values())
|
| 50 |
+
preds = pd.Series(probs, index=labels, name='Predicted Probability')
|
| 51 |
+
|
| 52 |
+
return preds
|
| 53 |
+
|
| 54 |
+
def run(self, text):
|
| 55 |
+
"""
|
| 56 |
+
Classify and Justify Emotion in text.
|
| 57 |
+
Parameters:
|
| 58 |
+
text (str): The user input string to perform emotion classification on
|
| 59 |
+
Returns:
|
| 60 |
+
predictions (str): The predicted probabilities for emotion classes
|
| 61 |
+
html (hmtl): html object for plotting emotion prediction justification
|
| 62 |
+
"""
|
| 63 |
+
|
| 64 |
+
preds = self.classify(text)
|
| 65 |
+
html = self.justify(text)
|
| 66 |
+
|
| 67 |
+
return preds, html
|
itaca_logo.png
ADDED
|
keyword_extraction.py
ADDED
|
@@ -0,0 +1,145 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import nltk
|
| 2 |
+
import pytextrank
|
| 3 |
+
import re
|
| 4 |
+
from operator import itemgetter
|
| 5 |
+
import en_core_web_sm
|
| 6 |
+
|
| 7 |
+
|
| 8 |
+
class KeywordExtractor:
|
| 9 |
+
"""
|
| 10 |
+
Keyword Extraction on text data
|
| 11 |
+
Attributes:
|
| 12 |
+
nlp: An instance English pipeline optimized for CPU for spacy
|
| 13 |
+
"""
|
| 14 |
+
|
| 15 |
+
def __init__(self):
|
| 16 |
+
self.nlp = en_core_web_sm.load()
|
| 17 |
+
self.nlp.add_pipe("textrank")
|
| 18 |
+
|
| 19 |
+
def get_keywords(self, text, max_keywords):
|
| 20 |
+
"""
|
| 21 |
+
Extract keywords from text.
|
| 22 |
+
Parameters:
|
| 23 |
+
text (str): The user input string to extract keywords from
|
| 24 |
+
Returns:
|
| 25 |
+
kws (list): list of extracted keywords
|
| 26 |
+
"""
|
| 27 |
+
|
| 28 |
+
doc = self.nlp(text)
|
| 29 |
+
|
| 30 |
+
kws = [i.text for i in doc._.phrases[:max_keywords]]
|
| 31 |
+
|
| 32 |
+
return kws
|
| 33 |
+
|
| 34 |
+
def get_keyword_indices(self, kws, text):
|
| 35 |
+
"""
|
| 36 |
+
Extract keywords from text.
|
| 37 |
+
Parameters:
|
| 38 |
+
kws (list): list of extracted keywords
|
| 39 |
+
text (str): The user input string to extract keywords from
|
| 40 |
+
Returns:
|
| 41 |
+
keyword_indices (list): list of indices for keyword boundaries in text
|
| 42 |
+
"""
|
| 43 |
+
|
| 44 |
+
keyword_indices = []
|
| 45 |
+
for s in kws:
|
| 46 |
+
indices = [[m.start(), m.end()] for m in re.finditer(re.escape(s), text)]
|
| 47 |
+
keyword_indices.extend(indices)
|
| 48 |
+
|
| 49 |
+
return keyword_indices
|
| 50 |
+
|
| 51 |
+
def merge_overlapping_indices(self, keyword_indices):
|
| 52 |
+
"""
|
| 53 |
+
Merge overlapping keyword indices.
|
| 54 |
+
Parameters:
|
| 55 |
+
keyword_indices (list): list of indices for keyword boundaries in text
|
| 56 |
+
Returns:
|
| 57 |
+
keyword_indices (list): list of indices for keyword boundaries in with overlapping combined
|
| 58 |
+
"""
|
| 59 |
+
|
| 60 |
+
# Sort the array on the basis of start values of intervals.
|
| 61 |
+
keyword_indices.sort()
|
| 62 |
+
|
| 63 |
+
stack = []
|
| 64 |
+
# insert first interval into stack
|
| 65 |
+
stack.append(keyword_indices[0])
|
| 66 |
+
for i in keyword_indices[1:]:
|
| 67 |
+
# Check for overlapping interval,
|
| 68 |
+
# if interval overlap
|
| 69 |
+
if (stack[-1][0] <= i[0] <= stack[-1][-1]) or (stack[-1][-1] == i[0]-1):
|
| 70 |
+
stack[-1][-1] = max(stack[-1][-1], i[-1])
|
| 71 |
+
else:
|
| 72 |
+
stack.append(i)
|
| 73 |
+
return stack
|
| 74 |
+
|
| 75 |
+
def merge_until_finished(self, keyword_indices):
|
| 76 |
+
"""
|
| 77 |
+
Loop until no overlapping keyword indices left.
|
| 78 |
+
Parameters:
|
| 79 |
+
keyword_indices (list): list of indices for keyword boundaries in text
|
| 80 |
+
Returns:
|
| 81 |
+
keyword_indices (list): list of indices for keyword boundaries in with overlapping combined
|
| 82 |
+
"""
|
| 83 |
+
|
| 84 |
+
len_indices = 0
|
| 85 |
+
while True:
|
| 86 |
+
# Merge overlapping indices
|
| 87 |
+
merged = self.merge_overlapping_indices(keyword_indices)
|
| 88 |
+
# Check to see if merging reduced number of annotation indices
|
| 89 |
+
# If merging did not reduce list return final indicies
|
| 90 |
+
if len_indices == len(merged):
|
| 91 |
+
out_indices = sorted(merged, key=itemgetter(0))
|
| 92 |
+
return out_indices
|
| 93 |
+
else:
|
| 94 |
+
len_indices = len(merged)
|
| 95 |
+
|
| 96 |
+
def get_annotation(self, text, keyword_indices):
|
| 97 |
+
"""
|
| 98 |
+
Create text annotation for extracted keywords.
|
| 99 |
+
Parameters:
|
| 100 |
+
keyword_indices (list): list of indices for keyword boundaries in text
|
| 101 |
+
Returns:
|
| 102 |
+
annotation (list): list of tuples for generating html
|
| 103 |
+
"""
|
| 104 |
+
|
| 105 |
+
# Turn list to numpy array
|
| 106 |
+
arr = list(text)
|
| 107 |
+
|
| 108 |
+
# Loop through indices in list and insert delimeters
|
| 109 |
+
for idx in sorted(keyword_indices, reverse=True):
|
| 110 |
+
arr.insert(idx[0], "<kw>")
|
| 111 |
+
arr.insert(idx[1]+1, "<!kw> <kw>")
|
| 112 |
+
|
| 113 |
+
# join array
|
| 114 |
+
joined_annotation = ''.join(arr)
|
| 115 |
+
|
| 116 |
+
# split array on delimeter
|
| 117 |
+
split = joined_annotation.split('<kw>')
|
| 118 |
+
|
| 119 |
+
# Create annotation for keywords in text
|
| 120 |
+
annotation = [(x.replace('<!kw> ', ''), "KEY", "#26aaef") if "<!kw>" in x else x for x in split]
|
| 121 |
+
|
| 122 |
+
return annotation
|
| 123 |
+
|
| 124 |
+
def generate(self, text, max_keywords):
|
| 125 |
+
"""
|
| 126 |
+
Create text annotation for extracted keywords.
|
| 127 |
+
Parameters:
|
| 128 |
+
text (str): The user input string to extract keywords from
|
| 129 |
+
max_keywords (int): Limit on number of keywords to generate
|
| 130 |
+
Returns:
|
| 131 |
+
annotation (list): list of tuples for generating html
|
| 132 |
+
kws (list): list of extracted keywords
|
| 133 |
+
"""
|
| 134 |
+
|
| 135 |
+
kws = self.get_keywords(text, max_keywords)
|
| 136 |
+
|
| 137 |
+
indices = list(self.get_keyword_indices(kws, text))
|
| 138 |
+
if indices:
|
| 139 |
+
indices_merged = self.merge_until_finished(indices)
|
| 140 |
+
annotation = self.get_annotation(text, indices_merged)
|
| 141 |
+
else:
|
| 142 |
+
annotation = None
|
| 143 |
+
|
| 144 |
+
return annotation, kws
|
| 145 |
+
|
models.py
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Import the necessary libraries
|
| 2 |
+
from transformers import pipeline
|
| 3 |
+
|
| 4 |
+
# Initialize the text classification model with a pre-trained model
|
| 5 |
+
model_text_emotion = pipeline("text-classification", model="j-hartmann/emotion-english-distilroberta-base")
|
| 6 |
+
|
| 7 |
+
# Initialize the audio classification model with a pre-trained SER model
|
| 8 |
+
model_speech_emotion = pipeline("audio-classification", model="aherzberg/ser_model_fixed_label")
|
| 9 |
+
|
| 10 |
+
# Initialize the automatic speech recognition model with a pre-trained model that is capable of converting speech to text
|
| 11 |
+
model_voice2text = pipeline("automatic-speech-recognition", model="openai/whisper-tiny.en")
|
| 12 |
+
|
| 13 |
+
# A function that uses the initialized text classification model to predict the emotion of a given text input
|
| 14 |
+
def infere_text_emotion(text):
|
| 15 |
+
return model_text_emotion(text)[0]["label"].capitalize()
|
| 16 |
+
|
| 17 |
+
# A function that uses the initialized audio classification model to predict the emotion of a given speech input
|
| 18 |
+
def infere_speech_emotion(text):
|
| 19 |
+
# Dict that maps the speech model emotions with the text's ones
|
| 20 |
+
emotions_dict = {"angry": "Anger", "disgust": "Disgust", "fear": "Fear", "happy": "Joy", "neutral": "Neutral", "sad": "Sadness"}
|
| 21 |
+
inference = model_speech_emotion(text)[0]["label"]
|
| 22 |
+
return emotions_dict[inference]
|
| 23 |
+
|
| 24 |
+
# A function that uses the initialized automatic speech recognition model to convert speech (as an audio file) to text
|
| 25 |
+
def infere_voice2text(audio_file):
|
| 26 |
+
return model_voice2text(audio_file)["text"]
|
named_entity_recognition.py
ADDED
|
@@ -0,0 +1,60 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from transformers import AutoTokenizer, AutoModelForTokenClassification
|
| 2 |
+
from transformers import pipeline
|
| 3 |
+
|
| 4 |
+
|
| 5 |
+
class NamedEntityRecognition:
|
| 6 |
+
"""
|
| 7 |
+
Named Entity Recognition on text data.
|
| 8 |
+
Attributes:
|
| 9 |
+
tokenizer: An instance of Hugging Face Tokenizer
|
| 10 |
+
model: An instance of Hugging Face Model
|
| 11 |
+
nlp: An instance of Hugging Face Named Entity Recognition pipeline
|
| 12 |
+
"""
|
| 13 |
+
|
| 14 |
+
def __init__(self):
|
| 15 |
+
tokenizer = AutoTokenizer.from_pretrained("xlm-roberta-large-finetuned-conll03-english")
|
| 16 |
+
model = AutoModelForTokenClassification.from_pretrained("xlm-roberta-large-finetuned-conll03-english")
|
| 17 |
+
self.nlp = pipeline("ner", model=model, tokenizer=tokenizer, grouped_entities=True)
|
| 18 |
+
|
| 19 |
+
def get_annotation(self, preds, text):
|
| 20 |
+
"""
|
| 21 |
+
Get html annotation for displaying entities over text.
|
| 22 |
+
Parameters:
|
| 23 |
+
preds (dict): List of entities and their associated metadata
|
| 24 |
+
text (str): The user input string to generate entity tags for
|
| 25 |
+
Returns:
|
| 26 |
+
final_annotation (list): List of tuples to pass to text annotation html creator
|
| 27 |
+
"""
|
| 28 |
+
|
| 29 |
+
splits = [0]
|
| 30 |
+
entities = {}
|
| 31 |
+
for i in preds:
|
| 32 |
+
splits.append(i['start'])
|
| 33 |
+
splits.append(i['end'])
|
| 34 |
+
entities[i['word']] = i['entity_group']
|
| 35 |
+
|
| 36 |
+
# Exclude bad preds
|
| 37 |
+
exclude = ['', '.', '. ', ' ']
|
| 38 |
+
for x in exclude:
|
| 39 |
+
if x in entities.keys():
|
| 40 |
+
entities.pop(x)
|
| 41 |
+
|
| 42 |
+
parts = [text[i:j] for i, j in zip(splits, splits[1:] + [None])]
|
| 43 |
+
|
| 44 |
+
final_annotation = [(x, entities[x], "") if x in entities.keys() else x for x in parts]
|
| 45 |
+
|
| 46 |
+
return final_annotation
|
| 47 |
+
|
| 48 |
+
def classify(self, text):
|
| 49 |
+
"""
|
| 50 |
+
Recognize Named Entities in text.
|
| 51 |
+
Parameters:
|
| 52 |
+
text (str): The user input string to generate entity tags for
|
| 53 |
+
Returns:
|
| 54 |
+
predictions (str): The user input string to generate entity tags for
|
| 55 |
+
ner_annotation (str): The user input string to generate entity tags for
|
| 56 |
+
"""
|
| 57 |
+
|
| 58 |
+
preds = self.nlp(text)
|
| 59 |
+
ner_annotation = self.get_annotation(preds, text)
|
| 60 |
+
return preds, ner_annotation
|
part_of_speech_tagging.py
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import nltk
|
| 2 |
+
from nltk.tokenize import word_tokenize
|
| 3 |
+
nltk.download('punkt')
|
| 4 |
+
nltk.download('averaged_perceptron_tagger')
|
| 5 |
+
|
| 6 |
+
|
| 7 |
+
class POSTagging:
|
| 8 |
+
"""Part of Speech Tagging on text data"""
|
| 9 |
+
|
| 10 |
+
def __init__(self):
|
| 11 |
+
pass
|
| 12 |
+
|
| 13 |
+
def classify(self, text):
|
| 14 |
+
"""
|
| 15 |
+
Generate Part of Speech tags.
|
| 16 |
+
Parameters:
|
| 17 |
+
text (str): The user input string to generate tags for
|
| 18 |
+
Returns:
|
| 19 |
+
predictions (list): list of tuples containing words and their respective tags
|
| 20 |
+
"""
|
| 21 |
+
|
| 22 |
+
text = word_tokenize(text)
|
| 23 |
+
predictions = nltk.pos_tag(text)
|
| 24 |
+
return predictions
|
requirements.txt
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
Pillow
|
| 2 |
+
streamlit
|
| 3 |
+
pandas
|
| 4 |
+
numpy
|
| 5 |
+
matplotlib
|
| 6 |
+
tensorflow
|
| 7 |
+
tensorflow-hub
|
| 8 |
+
scikit-learn
|
| 9 |
+
easyocr
|
| 10 |
+
nltk~=3.5
|
| 11 |
+
typing-extensions
|
| 12 |
+
streamlit-option-menu~=0.3.2
|
| 13 |
+
st-annotated-text~=3.0.0
|
| 14 |
+
transformers-interpret~=0.7.2
|
| 15 |
+
htbuilder==0.6.0
|
| 16 |
+
pytextrank~=3.2.3
|
| 17 |
+
spacy~=3.0.5
|
| 18 |
+
librosa
|
| 19 |
+
en-core-web-sm @ https://github.com/explosion/spacy-models/releases/download/en_core_web_sm-3.0.0/en_core_web_sm-3.0.0-py3-none-any.whl
|
sentiment_analysis.py
ADDED
|
@@ -0,0 +1,78 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from transformers import AutoTokenizer, AutoModelForSequenceClassification
|
| 2 |
+
from transformers_interpret import SequenceClassificationExplainer
|
| 3 |
+
import torch
|
| 4 |
+
import pandas as pd
|
| 5 |
+
|
| 6 |
+
|
| 7 |
+
class SentimentAnalysis:
|
| 8 |
+
"""
|
| 9 |
+
Sentiment on text data.
|
| 10 |
+
Attributes:
|
| 11 |
+
tokenizer: An instance of Hugging Face Tokenizer
|
| 12 |
+
model: An instance of Hugging Face Model
|
| 13 |
+
explainer: An instance of SequenceClassificationExplainer from Transformers interpret
|
| 14 |
+
"""
|
| 15 |
+
|
| 16 |
+
def __init__(self):
|
| 17 |
+
# Load Tokenizer & Model
|
| 18 |
+
hub_location = 'cardiffnlp/twitter-roberta-base-sentiment'
|
| 19 |
+
#hub_location = 'dccuchile/bert-base-spanish-wwm-uncased'
|
| 20 |
+
self.tokenizer = AutoTokenizer.from_pretrained(hub_location)
|
| 21 |
+
self.model = AutoModelForSequenceClassification.from_pretrained(hub_location)
|
| 22 |
+
|
| 23 |
+
# Change model labels in config
|
| 24 |
+
self.model.config.id2label[0] = "Negative"
|
| 25 |
+
self.model.config.id2label[1] = "Neutral"
|
| 26 |
+
self.model.config.id2label[2] = "Positive"
|
| 27 |
+
self.model.config.label2id["Negative"] = self.model.config.label2id.pop("LABEL_0")
|
| 28 |
+
self.model.config.label2id["Neutral"] = self.model.config.label2id.pop("LABEL_1")
|
| 29 |
+
self.model.config.label2id["Positive"] = self.model.config.label2id.pop("LABEL_2")
|
| 30 |
+
|
| 31 |
+
# Instantiate explainer
|
| 32 |
+
self.explainer = SequenceClassificationExplainer(self.model, self.tokenizer)
|
| 33 |
+
|
| 34 |
+
def justify(self, text):
|
| 35 |
+
"""
|
| 36 |
+
Get html annotation for displaying sentiment justification over text.
|
| 37 |
+
Parameters:
|
| 38 |
+
text (str): The user input string to sentiment justification
|
| 39 |
+
Returns:
|
| 40 |
+
html (hmtl): html object for plotting sentiment prediction justification
|
| 41 |
+
"""
|
| 42 |
+
|
| 43 |
+
word_attributions = self.explainer(text)
|
| 44 |
+
html = self.explainer.visualize("example.html")
|
| 45 |
+
|
| 46 |
+
return html
|
| 47 |
+
|
| 48 |
+
def classify(self, text):
|
| 49 |
+
"""
|
| 50 |
+
Recognize Sentiment in text.
|
| 51 |
+
Parameters:
|
| 52 |
+
text (str): The user input string to perform sentiment classification on
|
| 53 |
+
Returns:
|
| 54 |
+
predictions (str): The predicted probabilities for sentiment classes
|
| 55 |
+
"""
|
| 56 |
+
|
| 57 |
+
tokens = self.tokenizer.encode_plus(text, add_special_tokens=False, return_tensors='pt')
|
| 58 |
+
outputs = self.model(**tokens)
|
| 59 |
+
probs = torch.nn.functional.softmax(outputs[0], dim=-1)
|
| 60 |
+
probs = probs.mean(dim=0).detach().numpy()
|
| 61 |
+
predictions = pd.Series(probs, index=["Negative", "Neutral", "Positive"], name='Predicted Probability')
|
| 62 |
+
|
| 63 |
+
return predictions
|
| 64 |
+
|
| 65 |
+
def run(self, text):
|
| 66 |
+
"""
|
| 67 |
+
Classify and Justify Sentiment in text.
|
| 68 |
+
Parameters:
|
| 69 |
+
text (str): The user input string to perform sentiment classification on
|
| 70 |
+
Returns:
|
| 71 |
+
predictions (str): The predicted probabilities for sentiment classes
|
| 72 |
+
html (hmtl): html object for plotting sentiment prediction justification
|
| 73 |
+
"""
|
| 74 |
+
|
| 75 |
+
predictions = self.classify(text)
|
| 76 |
+
html = self.justify(text)
|
| 77 |
+
|
| 78 |
+
return predictions, html
|
sentiment_analysis_v2.py
ADDED
|
@@ -0,0 +1,93 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from transformers import AutoTokenizer, AutoModelForSequenceClassification
|
| 2 |
+
from transformers_interpret import SequenceClassificationExplainer
|
| 3 |
+
import torch
|
| 4 |
+
import pandas as pd
|
| 5 |
+
|
| 6 |
+
|
| 7 |
+
class SentimentAnalysis:
|
| 8 |
+
"""
|
| 9 |
+
Sentiment on text data.
|
| 10 |
+
Attributes:
|
| 11 |
+
tokenizer: An instance of Hugging Face Tokenizer
|
| 12 |
+
model: An instance of Hugging Face Model
|
| 13 |
+
explainer: An instance of SequenceClassificationExplainer from Transformers interpret
|
| 14 |
+
"""
|
| 15 |
+
|
| 16 |
+
def __init__(self):
|
| 17 |
+
# Load Tokenizer & Model
|
| 18 |
+
hub_location = 'cardiffnlp/twitter-roberta-base-sentiment'
|
| 19 |
+
self.tokenizer = AutoTokenizer.from_pretrained(hub_location)
|
| 20 |
+
self.model = AutoModelForSequenceClassification.from_pretrained(hub_location)
|
| 21 |
+
|
| 22 |
+
hub_location_sp = 'finiteautomata/beto-sentiment-analysis'
|
| 23 |
+
self.tokenizer_sp = AutoTokenizer.from_pretrained(hub_location_sp)
|
| 24 |
+
self.model_sp = AutoModelForSequenceClassification.from_pretrained(hub_location_sp)
|
| 25 |
+
|
| 26 |
+
# Change model labels in config
|
| 27 |
+
self.model.config.id2label[0] = "Negative"
|
| 28 |
+
self.model.config.id2label[1] = "Neutral"
|
| 29 |
+
self.model.config.id2label[2] = "Positive"
|
| 30 |
+
self.model.config.label2id["Negative"] = self.model.config.label2id.pop("LABEL_0")
|
| 31 |
+
self.model.config.label2id["Neutral"] = self.model.config.label2id.pop("LABEL_1")
|
| 32 |
+
self.model.config.label2id["Positive"] = self.model.config.label2id.pop("LABEL_2")
|
| 33 |
+
|
| 34 |
+
# Instantiate explainer
|
| 35 |
+
self.explainer = SequenceClassificationExplainer(self.model, self.tokenizer)
|
| 36 |
+
self.explainer_sp = SequenceClassificationExplainer(self.model_sp, self.tokenizer_sp)
|
| 37 |
+
|
| 38 |
+
def justify(self, text, lang):
|
| 39 |
+
"""
|
| 40 |
+
Get html annotation for displaying sentiment justification over text.
|
| 41 |
+
Parameters:
|
| 42 |
+
text (str): The user input string to sentiment justification
|
| 43 |
+
Returns:
|
| 44 |
+
html (hmtl): html object for plotting sentiment prediction justification
|
| 45 |
+
"""
|
| 46 |
+
|
| 47 |
+
if lang == 'es':
|
| 48 |
+
word_attributions = self.explainer_sp(text)
|
| 49 |
+
html = self.explainer_sp.visualize("example.html")
|
| 50 |
+
else:
|
| 51 |
+
word_attributions = self.explainer(text)
|
| 52 |
+
html = self.explainer.visualize("example.html")
|
| 53 |
+
|
| 54 |
+
return html
|
| 55 |
+
|
| 56 |
+
def classify(self, text, lang):
|
| 57 |
+
"""
|
| 58 |
+
Recognize Sentiment in text.
|
| 59 |
+
Parameters:
|
| 60 |
+
text (str): The user input string to perform sentiment classification on
|
| 61 |
+
Returns:
|
| 62 |
+
predictions (str): The predicted probabilities for sentiment classes
|
| 63 |
+
"""
|
| 64 |
+
|
| 65 |
+
if lang == 'es':
|
| 66 |
+
tokens = self.tokenizer_sp.encode_plus(text, add_special_tokens=False, return_tensors='pt')
|
| 67 |
+
outputs = self.model_sp(**tokens)
|
| 68 |
+
probs = torch.nn.functional.softmax(outputs[0], dim=-1)
|
| 69 |
+
probs = probs.mean(dim=0).detach().numpy()
|
| 70 |
+
predictions = pd.Series(probs, index=["Negative", "Neutral", "Positive"], name='Predicted Probability')
|
| 71 |
+
else:
|
| 72 |
+
tokens = self.tokenizer.encode_plus(text, add_special_tokens=False, return_tensors='pt')
|
| 73 |
+
outputs = self.model(**tokens)
|
| 74 |
+
probs = torch.nn.functional.softmax(outputs[0], dim=-1)
|
| 75 |
+
probs = probs.mean(dim=0).detach().numpy()
|
| 76 |
+
predictions = pd.Series(probs, index=["Negative", "Neutral", "Positive"], name='Predicted Probability')
|
| 77 |
+
|
| 78 |
+
return predictions
|
| 79 |
+
|
| 80 |
+
def run(self, text, lang):
|
| 81 |
+
"""
|
| 82 |
+
Classify and Justify Sentiment in text.
|
| 83 |
+
Parameters:
|
| 84 |
+
text (str): The user input string to perform sentiment classification on
|
| 85 |
+
Returns:
|
| 86 |
+
predictions (str): The predicted probabilities for sentiment classes
|
| 87 |
+
html (hmtl): html object for plotting sentiment prediction justification
|
| 88 |
+
"""
|
| 89 |
+
|
| 90 |
+
predictions = self.classify(text, lang)
|
| 91 |
+
html = self.justify(text, lang)
|
| 92 |
+
|
| 93 |
+
return predictions, html
|