File size: 3,018 Bytes
d3dbd6c
 
 
 
 
 
7b34f91
d3dbd6c
 
7b34f91
d3dbd6c
7b34f91
d3dbd6c
 
 
 
 
 
 
7b34f91
d3dbd6c
 
7b34f91
d3dbd6c
 
7b34f91
d3dbd6c
 
 
 
7b34f91
d3dbd6c
 
7b34f91
d3dbd6c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6fc21a4
d3dbd6c
1
2
3
4
5
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
78
79
80
81
82
83
84
"""
HuggingFace Spaces that:
- loads in HanmunRoBERTa model https://huggingface.co/bdsl/HanmunRoBERTa
- optionally strips text of punctuation and unwanted charactesr
- predicts century for the input text
- Visualizes prediction scores for each century

# https://huggingface.co/blog/streamlit-spaces
# https://huggingface.co/docs/hub/en/spaces-sdks-streamlit

"""

import streamlit as st
from transformers import pipeline
from string import punctuation
import pandas as pd
import plotly.express as px
import plotly.graph_objects as go
colors = px.colors.qualitative.Plotly

# from huggingface_hub import InferenceClient
# client = InferenceClient(model="bdsl/HanmunRoBERTa")

# Load the pipeline with the HanmunRoBERTa model
model_pipeline = pipeline(task="text-classification", model="bdsl/HanmunRoBERTa")

# Streamlit app layout
title = "HanmunRoBERTa Century Classifier"
st.set_page_config(page_title=title, page_icon="πŸ“š")
st.title(title)

# Checkbox to remove punctuation
remove_punct = st.checkbox(label="Remove punctuation", value=True)

# Text area for user input
input_str = st.text_area("Input text", height=275)

# Remove punctuation if checkbox is selected
if remove_punct and input_str:
    # Specify the characters to remove
    characters_to_remove = "β—‹β–‘()〔〕:\"。·, ?ㆍ" + punctuation
    translating = str.maketrans('', '', characters_to_remove)
    input_str = input_str.translate(translating)

# Display the input text after processing
st.write("Processed input:", input_str)

# Predict and display the classification scores if input is provided
if st.button("Classify"):
    if input_str:
        predictions = model_pipeline(input_str)
        data = pd.DataFrame(predictions)
        data=data.sort_values(by='score', ascending=True)
        data.label = data.label.astype(str)

        
        # Displaying predictions as a bar chart
        fig = go.Figure(
            go.Bar(
                x=data.score.values,
                y=[f'{i}th Century' for i in data.label.values],
                orientation='h', 
                text=[f'{score:.3f}' for score in data['score'].values],  # Format text with 2 decimal points
                textposition='outside',  # Position the text outside the bars
                hoverinfo='text',  # Use custom text for hover info
                hovertext=[f'{i}th Century<br>Score: {score:.3f}' for i, score in zip(data['label'], data['score'])],  # Custom hover text
                marker=dict(color=[colors[i % len(colors)] for i in range(len(data))]),  # Cycle through colors

            ))
        fig.update_traces(width=0.4)

        fig.update_layout(
            height=300,  # Custom height
            xaxis_title='Score',
            yaxis_title='',
            title='Model predictions and scores',
            margin=dict(l=100, r=200, t=50, b=50),
            uniformtext_minsize=8, 
            uniformtext_mode='hide',
        )
        st.pyplot(fig=fig)
    else:
        st.write("Please enter some text to classify.")