karthikvarunn's picture
Update app.py
c709cf5 verified
# import gradio as gr
# gr.load("models/karthikvarunn/skar_propaganda_v1.1").launch()
# import gradio as gr
# from transformers import pipeline
# # Load the model correctly by specifying the repo id in the correct format
# classifier = pipeline("zero-shot-classification", model="karthikvarunn/skar_propaganda_v1.1")
# def format_output(result):
# # Unpack the result
# sequence = result['sequence']
# labels = result['labels']
# scores = result['scores']
# # Create a formatted string or list to display results clearly
# output_lines = []
# for label, score in zip(labels, scores):
# # Strip any leading/trailing whitespace from the label
# label = label.strip()
# output_lines.append(f"{label}: {score:.2f}") # Formatting score to two decimal places
# return "\n".join(output_lines)
# def predict(text, classes):
# labels = [label.strip() for label in classes.split(",")] # Ensure labels are properly stripped of whitespace
# result = classifier(text, labels)
# formatted_result = format_output(result)
# return formatted_result
# demo = gr.Interface(
# fn=predict,
# inputs=[
# gr.Textbox(lines=2, placeholder="Type something here...", value="This miracle drug can save lives!"),
# gr.Textbox(lines=2, placeholder="Possible labels, comma-separated", value="Doubt, Loaded Language, Jingoism, Hyperbole, no_propaganda")
# ],
# outputs=gr.Textbox(label="Formatted Output"),
# title="Text Classification",
# description="Enter text and labels to classify the text."
# )
# demo.launch()
import gradio as gr
from transformers import pipeline
# Initialize the zero-shot classification pipeline with your model from Hugging Face
classifier = pipeline("zero-shot-classification", model="karthikvarunn/skar_propaganda_v1.1")
def predict(text, classes):
# Split the comma-separated labels provided by the user and strip whitespace
labels = [label.strip() for label in classes.split(",")]
# Use the classifier to get predictions
result = classifier(text, labels)
# Convert labels and scores to a dictionary for the Gradio Label component
label_scores = {label: score for label, score in zip(result['labels'], result['scores'])}
return label_scores
# Create the Gradio interface
demo = gr.Interface(
fn=predict,
inputs=[
gr.Textbox(lines=2, placeholder="Type a sentence to find the propaganda technique...", value="This miracle drug can save lives!"),
gr.Textbox(lines=2, placeholder="Possible labels, comma-separated", value="Doubt, Loaded Language, Jingoism, Hyperbole, no_propaganda")
],
outputs=gr.Label(num_top_classes=None), # Automatically render scores as a bar chart
title="Zero-shot Classification for propaganda techniques",
description="Enter text and labels to classify the text."
)
# Launch the Gradio app
demo.launch()