File size: 1,987 Bytes
9e6613e
 
 
 
 
 
 
1d9d1b1
 
 
9e6613e
 
1d9d1b1
 
 
 
9e6613e
 
1d9d1b1
9e6613e
1d9d1b1
 
 
 
 
 
 
 
 
 
 
 
 
 
c06481e
1d9d1b1
 
c06481e
 
1d9d1b1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""
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
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.title(title)
st.set_page_config(layout=layout, page_title=title, page_icon="πŸ“š")

# 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)
        
        # Prepare the data for plotting
        labels = [prediction['label'] for prediction in predictions]
        scores = [prediction['score'] for prediction in predictions]
        data = pd.DataFrame({"Label": labels, "Score": scores})
        
        # Displaying predictions as a bar chart
        st.bar_chart(data.set_index('Label'))
    else:
        st.write("Please enter some text to classify.")