Space7_basic / app.py
pm's picture
Update app.py
3aae778 verified
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()