Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,73 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from transformers import pipeline
|
| 3 |
+
import matplotlib.pyplot as plt
|
| 4 |
+
import numpy as np
|
| 5 |
+
import io
|
| 6 |
+
from PIL import Image
|
| 7 |
+
|
| 8 |
+
# Function to perform classification and create pie and bar charts
|
| 9 |
+
def classify_and_plot(text, labels):
|
| 10 |
+
# Splitting labels entered by user
|
| 11 |
+
labels_list = labels.split(',')
|
| 12 |
+
|
| 13 |
+
# Load the zero-shot classification pipeline with the specific model
|
| 14 |
+
classifier = pipeline("zero-shot-classification", model="facebook/bart-large-mnli")
|
| 15 |
+
|
| 16 |
+
# Perform classification
|
| 17 |
+
result = classifier(text, labels_list)
|
| 18 |
+
|
| 19 |
+
# Extract labels and scores
|
| 20 |
+
labels = result['labels']
|
| 21 |
+
scores = result['scores']
|
| 22 |
+
|
| 23 |
+
# Generate a colour for each label
|
| 24 |
+
colors = plt.cm.viridis(np.linspace(0, 1, len(labels)))
|
| 25 |
+
|
| 26 |
+
# Create a pie chart
|
| 27 |
+
fig1, ax1 = plt.subplots()
|
| 28 |
+
wedges, texts = ax1.pie(scores, startangle=140, colors=colors)
|
| 29 |
+
ax1.axis('equal') # Equal aspect ratio ensures the pie chart is circular.
|
| 30 |
+
ax1.set_title('Pie Chart')
|
| 31 |
+
|
| 32 |
+
# Prepare labels with percentages for the pie chart legend
|
| 33 |
+
legend_labels = ['{0} - {1:1.2f} %'.format(i,j*100) for i,j in zip(labels, scores)]
|
| 34 |
+
ax1.legend(wedges, legend_labels, title="Labels with Scores", loc="center left", bbox_to_anchor=(1, 0.5))
|
| 35 |
+
|
| 36 |
+
# Save the pie chart to a buffer
|
| 37 |
+
buf1 = io.BytesIO()
|
| 38 |
+
plt.savefig(buf1, format='png', bbox_inches='tight')
|
| 39 |
+
buf1.seek(0)
|
| 40 |
+
pie_chart = Image.open(buf1)
|
| 41 |
+
pie_chart_array = np.array(pie_chart)
|
| 42 |
+
plt.close()
|
| 43 |
+
|
| 44 |
+
# Create a bar chart
|
| 45 |
+
fig2, ax2 = plt.subplots()
|
| 46 |
+
y_pos = np.arange(len(labels))
|
| 47 |
+
ax2.bar(y_pos, scores, align='center', alpha=0.7, color='blue')
|
| 48 |
+
ax2.set_xticks(y_pos)
|
| 49 |
+
ax2.set_xticklabels(labels, rotation=45, ha="right")
|
| 50 |
+
ax2.set_ylabel('Scores')
|
| 51 |
+
ax2.set_title('Bar Chart')
|
| 52 |
+
|
| 53 |
+
# Save the bar chart to a buffer
|
| 54 |
+
buf2 = io.BytesIO()
|
| 55 |
+
plt.savefig(buf2, format='png', bbox_inches='tight')
|
| 56 |
+
buf2.seek(0)
|
| 57 |
+
bar_chart = Image.open(buf2)
|
| 58 |
+
bar_chart_array = np.array(bar_chart)
|
| 59 |
+
plt.close()
|
| 60 |
+
|
| 61 |
+
return pie_chart_array, bar_chart_array
|
| 62 |
+
|
| 63 |
+
# Create a Gradio interface
|
| 64 |
+
iface = gr.Interface(
|
| 65 |
+
fn=classify_and_plot,
|
| 66 |
+
inputs=["text", "text"],
|
| 67 |
+
outputs=["image", "image"],
|
| 68 |
+
title="Zero-Shot Classification with Pie and Bar Charts",
|
| 69 |
+
description="Enter text and comma-separated labels for classification using the facebook/bart-large-mnli model. The outputs will be separate pie and bar charts representing the classification scores."
|
| 70 |
+
)
|
| 71 |
+
|
| 72 |
+
# Launch the interface with the 'share' argument
|
| 73 |
+
iface.launch(share=True)
|