Spaces:
Runtime error
Runtime error
File size: 1,558 Bytes
7e85d92 849584d 726dbd1 3aae778 849584d 3aae778 849584d 726dbd1 3aae778 849584d |
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 |
import gradio as gr
from transformers import pipeline
# def classify(text):
# classifier = pipeline("zero-shot-classification")
# candidate_labels = ["positive", "negative", "neutral"]
# output = classifier(text, candidate_labels)
# return output
# def classify(text):
# classifier = pipeline("zero-shot-classification")
# candidate_labels = ["positive", "negative", "neutral"]
# output = classifier(text, candidate_labels)
# output_labels = [label['label'] for label in output['labels']]
# output_scores = [score for score in output['scores']]
# sorted_output = sorted(zip(output_labels, output_scores), key=lambda x: x[1], reverse=True)
# return sorted_output[:3]
# demo = gr.Interface(fn=classify,
# inputs=gr.Textbox(label="Enter text to classify"),
# outputs=gr.Label(num_top_classes=3))
# demo.launch()
classifier = pipeline("zero-shot-classification")
def classify(text):
candidate_labels = ["positive", "negative", "neutral"]
output = classifier(text, candidate_labels)
# Process the output to match Gradio's expected input format for gr.Label
labels = output['labels']
scores = output['scores']
# Construct a simple string representation of top classifications
top_classes = ', '.join([f"{labels[i]}: {scores[i]:.2f}" for i in range(len(labels))])
return top_classes
demo = gr.Interface(fn=classify,
inputs=gr.Textbox(label="Enter something"),
outputs=gr.Label())
demo.launch()
|