Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,94 +1,76 @@
|
|
| 1 |
-
import
|
| 2 |
-
|
| 3 |
-
|
| 4 |
-
import
|
| 5 |
-
import
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
for word, box, pred in zip(true_tokens, true_boxes, true_predictions):
|
| 78 |
-
st.write(f"Word: {word}, Box: {box}, Prediction: {pred}")
|
| 79 |
-
|
| 80 |
-
# Associate languages with their levels
|
| 81 |
-
languages_with_levels = {}
|
| 82 |
-
current_language = None
|
| 83 |
-
|
| 84 |
-
j = 0
|
| 85 |
-
for i in range(len(true_labels)):
|
| 86 |
-
if true_labels[i] == 'language':
|
| 87 |
-
current_language = true_tokens[j]
|
| 88 |
-
j += 1
|
| 89 |
-
if i + 1 < len(true_labels):
|
| 90 |
-
languages_with_levels[current_language] = true_labels[i + 1]
|
| 91 |
-
|
| 92 |
-
st.write("Languages and Levels:")
|
| 93 |
-
for language, level in languages_with_levels.items():
|
| 94 |
-
st.write(f"{language}: {level}")
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import numpy as np
|
| 3 |
+
import streamlit as st
|
| 4 |
+
from transformers import AutoModelForTokenClassification, AutoProcessor
|
| 5 |
+
from PIL import Image, ImageDraw, ImageFont
|
| 6 |
+
|
| 7 |
+
# Load the LayoutLMv3 model and processor
|
| 8 |
+
processor = AutoProcessor.from_pretrained("microsoft/layoutlmv3-base", apply_ocr=True)
|
| 9 |
+
model = AutoModelForTokenClassification.from_pretrained("capitaletech/language-levels-LayoutLMv3-v4")
|
| 10 |
+
|
| 11 |
+
labels = ["language", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10"]
|
| 12 |
+
|
| 13 |
+
label2id = {label: idx for idx, label in enumerate(labels)}
|
| 14 |
+
id2label = {v: k for k, v in label2id.items()}
|
| 15 |
+
label2color = {
|
| 16 |
+
'language': 'blue', '1': 'red', '2': 'red', '3': 'red',
|
| 17 |
+
'4': 'orange', '5': 'orange', '6': 'orange', '7': 'green',
|
| 18 |
+
'8': 'green', '9': 'green', '10': 'green'
|
| 19 |
+
}
|
| 20 |
+
|
| 21 |
+
def unnormalize_box(bbox, width, height):
|
| 22 |
+
return [
|
| 23 |
+
width * (bbox[0] / 1000),
|
| 24 |
+
height * (bbox[1] / 1000),
|
| 25 |
+
width * (bbox[2] / 1000),
|
| 26 |
+
height * (bbox[3] / 1000),
|
| 27 |
+
]
|
| 28 |
+
|
| 29 |
+
def iob_to_label(label):
|
| 30 |
+
return label
|
| 31 |
+
|
| 32 |
+
def process_image(image):
|
| 33 |
+
width, height = image.size
|
| 34 |
+
|
| 35 |
+
# Encode
|
| 36 |
+
encoding = processor(image, truncation=True, return_offsets_mapping=True, return_tensors="pt")
|
| 37 |
+
offset_mapping = encoding.pop('offset_mapping')
|
| 38 |
+
|
| 39 |
+
# Forward pass
|
| 40 |
+
outputs = model(**encoding)
|
| 41 |
+
|
| 42 |
+
# Get predictions
|
| 43 |
+
predictions = outputs.logits.argmax(-1).squeeze().tolist()
|
| 44 |
+
token_boxes = encoding.bbox.squeeze().tolist()
|
| 45 |
+
|
| 46 |
+
# Only keep non-subword predictions
|
| 47 |
+
is_subword = np.array(offset_mapping.squeeze().tolist())[:, 0] != 0
|
| 48 |
+
true_predictions = [id2label[pred] for idx, pred in enumerate(predictions) if not is_subword[idx]]
|
| 49 |
+
true_boxes = [unnormalize_box(box, width, height) for idx, box in enumerate(token_boxes) if not is_subword[idx]]
|
| 50 |
+
|
| 51 |
+
# Draw predictions over the image
|
| 52 |
+
draw = ImageDraw.Draw(image)
|
| 53 |
+
font = ImageFont.load_default()
|
| 54 |
+
for prediction, box in zip(true_predictions, true_boxes):
|
| 55 |
+
predicted_label = iob_to_label(prediction)
|
| 56 |
+
draw.rectangle(box, outline=label2color[predicted_label])
|
| 57 |
+
draw.text((box[0] + 10, box[1] - 10), text=predicted_label, fill=label2color[predicted_label], font=font)
|
| 58 |
+
|
| 59 |
+
return image
|
| 60 |
+
|
| 61 |
+
# Streamlit UI
|
| 62 |
+
st.title("Language Levels Extraction using LayoutLMv3 Model")
|
| 63 |
+
st.write("Use this application to predict language levels in CVs.")
|
| 64 |
+
|
| 65 |
+
uploaded_file = st.file_uploader("Choose an image...", type="png")
|
| 66 |
+
|
| 67 |
+
if uploaded_file is not None:
|
| 68 |
+
image = Image.open(uploaded_file)
|
| 69 |
+
st.image(image, caption='Uploaded Image', use_column_width=True)
|
| 70 |
+
|
| 71 |
+
if st.button('Predict'):
|
| 72 |
+
annotated_image = process_image(image)
|
| 73 |
+
st.image(annotated_image, caption='Annotated Image', use_column_width=True)
|
| 74 |
+
|
| 75 |
+
# Add your token if required
|
| 76 |
+
# os.environ["YOUR_TOKEN_ENV_VAR"] = "your_token_here"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|