Spaces:
Sleeping
Sleeping
Update app.py
Browse filesupdated table and model
app.py
CHANGED
@@ -1,17 +1,18 @@
|
|
1 |
import gradio as gr
|
|
|
2 |
from transformers import AutoModelForSequenceClassification, AutoTokenizer
|
3 |
import torch
|
4 |
import pickle
|
5 |
|
6 |
-
#
|
7 |
model_name = "peterkros/cofogv1-bert"
|
8 |
model = AutoModelForSequenceClassification.from_pretrained(model_name)
|
9 |
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
10 |
|
11 |
-
# Load the label encoder
|
12 |
with open('label_encoder.pkl', 'rb') as file:
|
13 |
label_encoder = pickle.load(file)
|
14 |
|
|
|
15 |
def predict(text):
|
16 |
inputs = tokenizer(text, return_tensors="pt", padding=True, truncation=True, max_length=512)
|
17 |
with torch.no_grad():
|
@@ -21,22 +22,35 @@ def predict(text):
|
|
21 |
predicted_label = label_encoder.inverse_transform([predicted_class])[0]
|
22 |
return predicted_label
|
23 |
|
24 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
25 |
markdown_text = """
|
26 |
- Trained with ~1500 rows of data on bert-large-uncased, English.
|
27 |
- Input one budget line per time.
|
28 |
-
- Accuracy of the model is ~
|
29 |
"""
|
30 |
|
|
|
31 |
iface = gr.Interface(
|
32 |
-
fn=predict,
|
33 |
-
inputs=gr.components.Textbox(lines=1, placeholder="Enter Budget line here...", label="Budget Input"),
|
34 |
-
outputs=gr.components.Textbox(label="Classification Output"),
|
35 |
title="COFOG Level 1 Classification",
|
36 |
description=markdown_text,
|
37 |
-
allow_flagging="auto"
|
38 |
)
|
39 |
|
40 |
# Run the interface
|
41 |
if __name__ == "__main__":
|
42 |
iface.launch()
|
|
|
|
1 |
import gradio as gr
|
2 |
+
import pandas as pd
|
3 |
from transformers import AutoModelForSequenceClassification, AutoTokenizer
|
4 |
import torch
|
5 |
import pickle
|
6 |
|
7 |
+
# Existing code for loading model and tokenizer
|
8 |
model_name = "peterkros/cofogv1-bert"
|
9 |
model = AutoModelForSequenceClassification.from_pretrained(model_name)
|
10 |
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
11 |
|
|
|
12 |
with open('label_encoder.pkl', 'rb') as file:
|
13 |
label_encoder = pickle.load(file)
|
14 |
|
15 |
+
# Existing prediction function
|
16 |
def predict(text):
|
17 |
inputs = tokenizer(text, return_tensors="pt", padding=True, truncation=True, max_length=512)
|
18 |
with torch.no_grad():
|
|
|
22 |
predicted_label = label_encoder.inverse_transform([predicted_class])[0]
|
23 |
return predicted_label
|
24 |
|
25 |
+
# Function to display the metrics table
|
26 |
+
def show_table():
|
27 |
+
data = {
|
28 |
+
"Epoch": [1, 2, 3, 4, 5],
|
29 |
+
"Training Loss": ["No log", "No log", "No log", "No log", "No log"],
|
30 |
+
"Validation Loss": [2.095209, 1.419945, 0.683810, 0.460408, 0.422096],
|
31 |
+
"Accuracy": [0.340764, 0.662420, 0.850318, 0.872611, 0.888535]
|
32 |
+
}
|
33 |
+
df = pd.DataFrame(data)
|
34 |
+
return df
|
35 |
+
|
36 |
+
# Existing markdown text
|
37 |
markdown_text = """
|
38 |
- Trained with ~1500 rows of data on bert-large-uncased, English.
|
39 |
- Input one budget line per time.
|
40 |
+
- Accuracy of the model is ~88%.
|
41 |
"""
|
42 |
|
43 |
+
# Update the Gradio Interface to include the table
|
44 |
iface = gr.Interface(
|
45 |
+
fn=[predict, show_table],
|
46 |
+
inputs=[gr.components.Textbox(lines=1, placeholder="Enter Budget line here...", label="Budget Input"), None],
|
47 |
+
outputs=[gr.components.Textbox(label="Classification Output"), gr.outputs.Dataframe(type="pandas")],
|
48 |
title="COFOG Level 1 Classification",
|
49 |
description=markdown_text,
|
50 |
+
allow_flagging="auto"
|
51 |
)
|
52 |
|
53 |
# Run the interface
|
54 |
if __name__ == "__main__":
|
55 |
iface.launch()
|
56 |
+
|