yenniejun commited on
Commit
7b34f91
Β·
1 Parent(s): 54548e5

Trying gradio again

Browse files
Files changed (1) hide show
  1. app.py +32 -56
app.py CHANGED
@@ -1,57 +1,33 @@
1
- """
2
- HuggingFace Spaces that:
3
- - loads in HanmunRoBERTa model https://huggingface.co/bdsl/HanmunRoBERTa
4
- - optionally strips text of punctuation and unwanted charactesr
5
- - predicts century for the input text
6
- - Visualizes prediction scores for each century
7
-
8
- # https://huggingface.co/blog/streamlit-spaces
9
- # https://huggingface.co/docs/hub/en/spaces-sdks-streamlit
10
-
11
- """
12
-
13
- import streamlit as st
14
- from transformers import pipeline
15
- from string import punctuation
16
- import pandas as pd
17
- # from huggingface_hub import InferenceClient
18
- # client = InferenceClient(model="bdsl/HanmunRoBERTa")
19
-
20
- # Load the pipeline with the HanmunRoBERTa model
21
- model_pipeline = pipeline(task="text-classification", model="bdsl/HanmunRoBERTa")
22
-
23
- # Streamlit app layout
24
- title = "HanmunRoBERTa Century Classifier"
25
- st.set_page_config(page_title=title, page_icon="πŸ“š")
26
- st.title(title)
27
-
28
- # Checkbox to remove punctuation
29
- remove_punct = st.checkbox(label="Remove punctuation", value=True)
30
-
31
- # Text area for user input
32
- input_str = st.text_area("Input text", height=275)
33
-
34
- # Remove punctuation if checkbox is selected
35
- if remove_punct and input_str:
36
- # Specify the characters to remove
37
- characters_to_remove = "β—‹β–‘()〔〕:\"。·, ?ㆍ" + punctuation
38
- translating = str.maketrans('', '', characters_to_remove)
39
- input_str = input_str.translate(translating)
40
-
41
- # Display the input text after processing
42
- st.write("Processed input:", input_str)
43
-
44
- # Predict and display the classification scores if input is provided
45
- if st.button("Classify"):
46
- if input_str:
47
- predictions = model_pipeline(input_str)
48
-
49
- # Prepare the data for plotting
50
- labels = [prediction['label'] for prediction in predictions]
51
- scores = [prediction['score'] for prediction in predictions]
52
- data = pd.DataFrame({"Label": labels, "Score": scores})
53
-
54
- # Displaying predictions as a bar chart
55
- st.bar_chart(data.set_index('Label'))
56
  else:
57
- st.write("Please enter some text to classify.")
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+
3
+ title = "RoBERTa"
4
+
5
+ description = "Gradio Demo for RoBERTa. To use it, simply add your text, or click one of the examples to load them. Read more at the links below."
6
+
7
+ article = "<p style='text-align: center'><a href='https://arxiv.org/abs/1907.11692' target='_blank'>RoBERTa: A Robustly Optimized BERT Pretraining Approach</a></p>"
8
+
9
+ examples = [
10
+ ['The goal of life is <mask>.','roberta-base']
11
+ ]
12
+
13
+ io1 = gr.Interface.load("huggingface/roberta-base")
14
+
15
+ io2 = gr.Interface.load("huggingface/roberta-large")
16
+
17
+
18
+ def inference(inputtext, model):
19
+ if model == "roberta-base":
20
+ outlabel = io1(inputtext)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
21
  else:
22
+ outlabel = io2(inputtext)
23
+ return outlabel
24
+
25
+
26
+ gr.Interface(
27
+ inference,
28
+ [gr.inputs.Textbox(label="Context",lines=10),gr.inputs.Dropdown(choices=["roberta-base","roberta-large"], type="value", default="roberta-base", label="model")],
29
+ [gr.outputs.Label(label="Output")],
30
+ examples=examples,
31
+ article=article,
32
+ title=title,
33
+ description=description).launch(enable_queue=True)