HanmunRoBERTa / app.py
yenniejun's picture
Old gradio version
a686b95
raw
history blame
1.81 kB
"""
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
# https://www.gradio.app/docs/interface
# https://huggingface.co/spaces/docs-demos/roberta-base/blob/main/app.py
"""
import gradio as gr
from string import punctuation
title = "HanmunRoBERTa Century Classifier"
description = "Century classifier for classical Chinese and Korean texts"
# Load the HanmunRoBERTa model
hanmun_roberta = gr.load("huggingface/bdsl/HanmunRoBERTa")
def strip_text(inputtext):
characters_to_remove = "β—‹β–‘()〔〕:\"。·, ?ㆍ" + punctuation
translating = str.maketrans('', '', characters_to_remove)
return inputtext.translate(translating)
def inference(inputtext, model, strip_text_flag):
if strip_text_flag:
inputtext = strip_text(inputtext)
if model == "HanmunRoBERTa":
outlabel = hanmun_roberta(inputtext)
return outlabel
# Define some example inputs for your interface
examples = [["Example text 1", "HanmunRoBERTa", True],
["Example text 2", "HanmunRoBERTa", True]]
# Set up the Gradio interface
gr.Interface(
inference,
[gr.inputs.Textbox(label="Input text", lines=10),
gr.inputs.Dropdown(choices=["HanmunRoBERTa"],
type="value",
default="HanmunRoBERTa",
label="Model"),
gr.inputs.Checkbox(label="Remove punctuation")],
[gr.outputs.Label(label="Output")],
examples=examples,
title=title,
description=description).launch(enable_queue=True)