Spaces:
Sleeping
Sleeping
Luciferalive
commited on
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,68 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from transformers import pipeline
|
3 |
+
|
4 |
+
# Load the model
|
5 |
+
model_name = "knowledgator/comprehend_it-base"
|
6 |
+
classifier = pipeline("zero-shot-classification", model=model_name, device="cpu")
|
7 |
+
|
8 |
+
# Keywords associated with the "Value" label
|
9 |
+
value_keywords = [
|
10 |
+
"cheap", "expensive", "worth", "waste", "value for money", "overpriced", "bargain",
|
11 |
+
"affordable", "pricey", "costly", "economical", "deal", "rip-off", "budget-friendly",
|
12 |
+
"high-priced", "low-priced", "discounted", "premium", "luxurious", "inexpensive",
|
13 |
+
"priced right", "steal", "splurge", "bang for the buck", "investment", "saver",
|
14 |
+
"money's worth", "exorbitant", "reasonable", "unreasonable", "priced well",
|
15 |
+
"cost-effective", "overvalued", "undervalued", "fair price", "high cost", "low cost",
|
16 |
+
"good deal", "bad deal", "profitable", "loss", "savings", "spendy", "wallet-friendly",
|
17 |
+
"financially smart", "economic", "lavish", "modestly priced", "upscale", "downscale"
|
18 |
+
]
|
19 |
+
|
20 |
+
|
21 |
+
# Function to check for value-related keywords in feedback
|
22 |
+
def contains_value_keywords(feedback_text):
|
23 |
+
for keyword in value_keywords:
|
24 |
+
if keyword in feedback_text.lower():
|
25 |
+
return True
|
26 |
+
return False
|
27 |
+
|
28 |
+
# Function to classify feedback
|
29 |
+
def classify_feedback(feedback_text):
|
30 |
+
# Classify feedback using the loaded model
|
31 |
+
labels = ["Value", "Facilities", "Experience", "Functionality", "Quality"]
|
32 |
+
result = classifier(feedback_text, labels, multi_label=True)
|
33 |
+
|
34 |
+
# Check for value-related keywords and adjust scores if necessary
|
35 |
+
if contains_value_keywords(feedback_text):
|
36 |
+
# Find the index of the "Value" label
|
37 |
+
try:
|
38 |
+
value_index = result["labels"].index("Value")
|
39 |
+
# Promote the score of the "Value" label
|
40 |
+
result["scores"][value_index] += 0.2 # Adjust the promotion strength as needed
|
41 |
+
# Ensure the score does not exceed 1
|
42 |
+
result["scores"][value_index] = min(result["scores"][value_index], 1.0)
|
43 |
+
except ValueError:
|
44 |
+
pass # "Value" label not in the top results
|
45 |
+
|
46 |
+
# Get the top two labels associated with the feedback, after possible adjustment
|
47 |
+
top_labels_scores = sorted(zip(result["labels"], result["scores"]), key=lambda x: x[1], reverse=True)[:2]
|
48 |
+
top_labels, scores = zip(*top_labels_scores)
|
49 |
+
|
50 |
+
# Generate HTML content for displaying the scores as meters/progress bars
|
51 |
+
html_content = ""
|
52 |
+
for i in range(len(top_labels)):
|
53 |
+
score_percentage = scores[i] * 100 # Convert score to percentage
|
54 |
+
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>"
|
55 |
+
|
56 |
+
return html_content
|
57 |
+
|
58 |
+
# Create Gradio interface
|
59 |
+
feedback_textbox = gr.Textbox(label="Enter your feedback:")
|
60 |
+
feedback_output = gr.HTML(label="Top 2 Labels with Scores:")
|
61 |
+
|
62 |
+
gr.Interface(
|
63 |
+
fn=classify_feedback,
|
64 |
+
inputs=feedback_textbox,
|
65 |
+
outputs=feedback_output,
|
66 |
+
title="Feedback Classifier",
|
67 |
+
description="Enter your feedback and get the top 2 associated labels with scores."
|
68 |
+
).launch()
|