Update app.py
Browse files
app.py
CHANGED
@@ -1,25 +1,32 @@
|
|
1 |
import streamlit as st
|
2 |
-
import
|
3 |
-
import
|
4 |
-
|
5 |
-
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
23 |
|
24 |
st.write("Enter the contract specifications in Finnish:")
|
25 |
|
@@ -28,9 +35,13 @@ contract_text = st.text_area("Contract Specifications (Finnish):", height=300)
|
|
28 |
|
29 |
if st.button("Classify"):
|
30 |
if contract_text:
|
31 |
-
|
32 |
|
33 |
st.write("Classified Contract Specifications:")
|
34 |
-
|
|
|
|
|
|
|
|
|
35 |
else:
|
36 |
-
st.write("Please enter the contract specifications.")
|
|
|
1 |
import streamlit as st
|
2 |
+
from transformers import BertTokenizer, BertForSequenceClassification
|
3 |
+
import torch
|
4 |
+
import torch.nn.functional as F
|
5 |
+
|
6 |
+
# Load the tokenizer and model
|
7 |
+
model_name = "TurkuNLP/bert-base-finnish-cased-v1"
|
8 |
+
tokenizer = BertTokenizer.from_pretrained(model_name)
|
9 |
+
model = BertForSequenceClassification.from_pretrained(model_name, num_labels=6) # Assuming 6 categories
|
10 |
+
|
11 |
+
# Define categories
|
12 |
+
categories = ["Urakka sisältää", "Urakka ei sisältää", "Tilaajan velvoitteet", "Käytäntöjen tarkennukset", "Hintojen tarkennukset", "Muu"]
|
13 |
+
|
14 |
+
# Function to classify lines
|
15 |
+
def classify_lines(text):
|
16 |
+
lines = text.split("\n")
|
17 |
+
categorized_lines = {category: [] for category in categories}
|
18 |
+
|
19 |
+
for line in lines:
|
20 |
+
if line.strip(): # Skip empty lines
|
21 |
+
inputs = tokenizer(line, return_tensors="pt", padding=True, truncation=True, max_length=512)
|
22 |
+
outputs = model(**inputs)
|
23 |
+
probs = F.softmax(outputs.logits, dim=1)
|
24 |
+
predicted_category = torch.argmax(probs, dim=1).item()
|
25 |
+
categorized_lines[categories[predicted_category]].append(line)
|
26 |
+
|
27 |
+
return categorized_lines
|
28 |
+
|
29 |
+
st.title("Finnish Contract Specifications Categorizer with TurkuNLP BERT")
|
30 |
|
31 |
st.write("Enter the contract specifications in Finnish:")
|
32 |
|
|
|
35 |
|
36 |
if st.button("Classify"):
|
37 |
if contract_text:
|
38 |
+
categories = classify_lines(contract_text)
|
39 |
|
40 |
st.write("Classified Contract Specifications:")
|
41 |
+
|
42 |
+
for category, lines in categories.items():
|
43 |
+
st.write(f"### {category}")
|
44 |
+
for line in lines:
|
45 |
+
st.write(f"- {line}")
|
46 |
else:
|
47 |
+
st.write("Please enter the contract specifications.")
|