File size: 1,965 Bytes
b0e6d60 4460d3d c60d44f b0e39c2 4460d3d c60d44f 4460d3d c60d44f b0e39c2 c60d44f dc47816 4460d3d c60d44f 4460d3d c60d44f 4460d3d c60d44f 4460d3d c60d44f 4460d3d c60d44f 4460d3d c60d44f 4460d3d c60d44f 4460d3d |
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 |
import gradio as gr
import torch
from transformers import AutoTokenizer, AutoModelForSequenceClassification, pipeline
MODEL_NAME = "cheberle/autotrain-35swc-b4r9z"
# ---------------------------------------------------------------------------
# 1) Load the tokenizer and model for sequence classification
# ---------------------------------------------------------------------------
tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME, trust_remote_code=True)
model = AutoModelForSequenceClassification.from_pretrained(MODEL_NAME, trust_remote_code=True)
# Create a pipeline for text classification
classifier = pipeline("text-classification", model=model, tokenizer=tokenizer)
# ---------------------------------------------------------------------------
# 2) Define inference function
# ---------------------------------------------------------------------------
def classify_text(text):
"""
Return the classification results in the format:
[
{
'label': 'POSITIVE',
'score': 0.98
}
]
"""
results = classifier(text)
return results
# ---------------------------------------------------------------------------
# 3) Build the Gradio UI
# ---------------------------------------------------------------------------
with gr.Blocks() as demo:
gr.Markdown("<h3>Text Classification Demo</h3>")
with gr.Row():
input_text = gr.Textbox(
lines=3,
label="Enter text to classify",
placeholder="Type something..."
)
output = gr.JSON(label="Classification Output")
classify_btn = gr.Button("Classify")
# Link the button to the function
classify_btn.click(fn=classify_text, inputs=input_text, outputs=output)
# ---------------------------------------------------------------------------
# 4) Launch the demo
# ---------------------------------------------------------------------------
if __name__ == "__main__":
demo.launch() |