Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,18 +1,17 @@
|
|
1 |
-
import gradio as gr
|
2 |
-
import pandas as pd
|
3 |
from transformers import AutoModelForSequenceClassification, AutoTokenizer
|
4 |
import torch
|
5 |
import pickle
|
6 |
|
7 |
-
#
|
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,35 +21,33 @@ def predict(text):
|
|
22 |
predicted_label = label_encoder.inverse_transform([predicted_class])[0]
|
23 |
return predicted_label
|
24 |
|
25 |
-
#
|
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=
|
46 |
-
inputs=
|
47 |
-
outputs=[gr.components.Textbox(label="Classification Output"), gr.
|
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 |
|
|
|
|
1 |
+
import gradio as gr
|
|
|
2 |
from transformers import AutoModelForSequenceClassification, AutoTokenizer
|
3 |
import torch
|
4 |
import pickle
|
5 |
|
6 |
+
# Load the model and tokenizer from Hugging Face Hub
|
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 |
predicted_label = label_encoder.inverse_transform([predicted_class])[0]
|
22 |
return predicted_label
|
23 |
|
24 |
+
# Define the markdown text with bullet points
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 ~88%.
|
29 |
"""
|
30 |
+
markdown_table = """
|
31 |
+
| Epoch | Training Loss | Validation Loss | Accuracy |
|
32 |
+
|-------|---------------|-----------------|-----------|
|
33 |
+
| 1 | No log | 2.095209 | 0.340764 |
|
34 |
+
| 2 | No log | 1.419945 | 0.662420 |
|
35 |
+
| 3 | No log | 0.683810 | 0.850318 |
|
36 |
+
| 4 | No log | 0.460408 | 0.872611 |
|
37 |
+
| 5 | No log | 0.422096 | 0.888535 |
|
38 |
+
"""
|
39 |
|
|
|
40 |
iface = gr.Interface(
|
41 |
+
fn=predict,
|
42 |
+
inputs=gr.components.Textbox(lines=1, placeholder="Enter Budget line here...", label="Budget Input"),
|
43 |
+
outputs=[gr.components.Textbox(label="Classification Output"), gr.components.Article(markdown_table)],
|
44 |
title="COFOG Level 1 Classification",
|
45 |
description=markdown_text,
|
46 |
+
allow_flagging="auto" # Enables flagging
|
47 |
)
|
48 |
|
49 |
# Run the interface
|
50 |
if __name__ == "__main__":
|
51 |
iface.launch()
|
52 |
|
53 |
+
|