Updated_Sp1 / app.py
Luciferalive's picture
Create app.py
8b672e9 verified
import gradio as gr
from transformers import pipeline
# Load the model
model_name = "knowledgator/comprehend_it-base"
classifier = pipeline("zero-shot-classification", model=model_name, device="cpu")
# Keywords associated with the "Value" label
value_keywords = [
"cheap", "expensive", "worth", "waste", "value for money", "overpriced", "bargain",
"affordable", "pricey", "costly", "economical", "deal", "rip-off", "budget-friendly",
"high-priced", "low-priced", "discounted", "premium", "luxurious", "inexpensive",
"priced right", "steal", "splurge", "bang for the buck", "investment", "saver",
"money's worth", "exorbitant", "reasonable", "unreasonable", "priced well",
"cost-effective", "overvalued", "undervalued", "fair price", "high cost", "low cost",
"good deal", "bad deal", "profitable", "loss", "savings", "spendy", "wallet-friendly",
"financially smart", "economic", "lavish", "modestly priced", "upscale", "downscale"
]
# Function to check for value-related keywords in feedback
def contains_value_keywords(feedback_text):
for keyword in value_keywords:
if keyword in feedback_text.lower():
return True
return False
# Function to classify feedback
def classify_feedback(feedback_text):
# Classify feedback using the loaded model
labels = ["Value", "Facilities", "Experience", "Functionality", "Quality"]
result = classifier(feedback_text, labels, multi_label=True)
# Check for value-related keywords and adjust scores if necessary
if contains_value_keywords(feedback_text):
# Find the index of the "Value" label
try:
value_index = result["labels"].index("Value")
# Promote the score of the "Value" label
result["scores"][value_index] += 0.2 # Adjust the promotion strength as needed
# Ensure the score does not exceed 1
result["scores"][value_index] = min(result["scores"][value_index], 1.0)
except ValueError:
pass # "Value" label not in the top results
# Get the top two labels associated with the feedback, after possible adjustment
top_labels_scores = sorted(zip(result["labels"], result["scores"]), key=lambda x: x[1], reverse=True)[:2]
top_labels, scores = zip(*top_labels_scores)
# Generate HTML content for displaying the scores as meters/progress bars
html_content = ""
for i in range(len(top_labels)):
score_percentage = scores[i] * 100 # Convert score to percentage
html_content += f"<div><b>{top_labels[i]}:</b> {scores[i]:.2f} <div style='background-color: #e0e0e0; border-radius: 10px;'><div style='height: 24px; width: {score_percentage}%; background-color: #76b900; border-radius: 10px;'></div></div></div>"
return html_content
# Create Gradio interface
feedback_textbox = gr.Textbox(label="Enter your feedback:")
feedback_output = gr.HTML(label="Top 2 Labels with Scores:")
gr.Interface(
fn=classify_feedback,
inputs=feedback_textbox,
outputs=feedback_output,
title="Feedback Classifier",
description="Enter your feedback and get the top 2 associated labels with scores."
).launch()