yenniejun commited on
Commit
3033c0a
Β·
1 Parent(s): 4f09935

Switching to Gradio

Browse files
Files changed (2) hide show
  1. app.py +30 -38
  2. requirements.txt +0 -3
app.py CHANGED
@@ -9,49 +9,41 @@ HuggingFace Spaces that:
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.title(title)
26
- st.set_page_config(layout=layout, page_title=title, page_icon="πŸ“š")
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.")
 
9
  # https://huggingface.co/docs/hub/en/spaces-sdks-streamlit
10
 
11
  """
12
+ import gradio as gr
 
 
13
  from string import punctuation
 
 
 
 
 
 
14
 
 
15
  title = "HanmunRoBERTa Century Classifier"
16
+ description = "Century classifier for classical Chinese and Korean texts"
 
17
 
18
+ hanmun_roberta = gr.Interface.load("bdsl/HanmunRoBERTa")
 
19
 
 
 
20
 
21
+ def inference(inputtext, model, strip_text):
22
+ if strip_text:
23
+ inputtext = strip_text(inputtext)
24
+ if model == "HanmunRoBERTa":
25
+ outlabel = hanmun_roberta(inputtext)
26
+ return outlabel
27
+
28
+ def strip_text(inputtext):
29
  characters_to_remove = "β—‹β–‘()〔〕:\"。·, ?ㆍ" + punctuation
30
  translating = str.maketrans('', '', characters_to_remove)
31
+ return inputtext.translate(translating)
32
+
33
+ gr.Interface(
34
+ inference,
35
+ [gr.inputs.Textbox(label="Input text",lines=10),
36
+ gr.inputs.Dropdown(choices=["HanmunRoBERTa"],
37
+ type="value",
38
+ default="HanmunRoBERTa",
39
+ label="model"),
40
+ gr.inputs.Checkbox(label="Remove punctuation", value=True),
41
+ ],
42
+ [gr.outputs.Label(label="Output")],
43
+ examples=examples,
44
+ title=title,
45
+ description=description).launch(enable_queue=True)
46
+
47
+
48
+
49
+
requirements.txt CHANGED
@@ -1,3 +0,0 @@
1
- pandas==2.0.3
2
- transformers[tf-cpu]
3
- tensorflow