create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,87 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from transformers import AutoTokenizer, AutoModelForSequenceClassification, AutoModelForSeq2SeqLM, pipeline
|
3 |
+
|
4 |
+
# Load the pre-trained BERT model and tokenizer for sentiment analysis
|
5 |
+
sentiment_model_name = "nlptown/bert-base-multilingual-uncased-sentiment"
|
6 |
+
sentiment_tokenizer = AutoTokenizer.from_pretrained(sentiment_model_name)
|
7 |
+
sentiment_model = AutoModelForSequenceClassification.from_pretrained(sentiment_model_name)
|
8 |
+
nlp_pipeline = pipeline('sentiment-analysis', model=sentiment_model, tokenizer=sentiment_tokenizer)
|
9 |
+
|
10 |
+
# Load the pre-trained T5 model and tokenizer for reason extraction
|
11 |
+
reason_model_name = "google/flan-t5-large"
|
12 |
+
reason_tokenizer = AutoTokenizer.from_pretrained(reason_model_name)
|
13 |
+
reason_model = AutoModelForSeq2SeqLM.from_pretrained(reason_model_name)
|
14 |
+
reason_extraction_pipeline = pipeline("text2text-generation", model=reason_model, tokenizer=reason_tokenizer)
|
15 |
+
|
16 |
+
# Function to classify sentiment as Positive, Neutral, or Negative
|
17 |
+
def classify_sentiment(feedback):
|
18 |
+
if isinstance(feedback, str):
|
19 |
+
# Truncate review if it exceeds 512 tokens for sentiment analysis
|
20 |
+
result = nlp_pipeline(feedback[:512])
|
21 |
+
label = result[0]['label']
|
22 |
+
|
23 |
+
# Convert Hugging Face star labels to "Positive", "Negative", or "Neutral"
|
24 |
+
if label in ['4 stars', '5 stars']:
|
25 |
+
return "Positive"
|
26 |
+
elif label == '3 stars':
|
27 |
+
return "Neutral"
|
28 |
+
else:
|
29 |
+
return "Negative"
|
30 |
+
else:
|
31 |
+
return "No Feedback"
|
32 |
+
|
33 |
+
# Function to generate the prompt for reason extraction
|
34 |
+
def generate_prompt(feedback_text):
|
35 |
+
return f"""
|
36 |
+
You are given customer feedback. Please identify the key product attribute or aspect that the customer is discussing, whether positive or negative.
|
37 |
+
Focus on words like price, quality, smell, packaging, durability, or any other relevant product feature.
|
38 |
+
If no clear aspect can be identified, return "Could not extract a specific reason".
|
39 |
+
|
40 |
+
Feedback: '{feedback_text}'
|
41 |
+
"""
|
42 |
+
|
43 |
+
# Function to post-process reasons and filter based on known product aspects
|
44 |
+
def post_process_reason(reason):
|
45 |
+
possible_reasons = ["price", "quality", "smell", "packaging", "durability"]
|
46 |
+
for r in possible_reasons:
|
47 |
+
if r in reason.lower():
|
48 |
+
return r
|
49 |
+
return reason
|
50 |
+
|
51 |
+
# Function to extract the reason for the sentiment
|
52 |
+
def analyze_feedback(feedback_text):
|
53 |
+
feedback_text = feedback_text.strip()
|
54 |
+
prompt = generate_prompt(feedback_text)
|
55 |
+
|
56 |
+
try:
|
57 |
+
result = reason_extraction_pipeline(prompt, max_length=50)
|
58 |
+
reason = result[0]['generated_text'].strip()
|
59 |
+
return post_process_reason(reason)
|
60 |
+
except Exception as e:
|
61 |
+
print(f"Error processing feedback: {feedback_text}")
|
62 |
+
print(e)
|
63 |
+
return "Error in processing"
|
64 |
+
|
65 |
+
# Function to integrate both sentiment classification and reason extraction
|
66 |
+
def sentiment_and_reason(review_text):
|
67 |
+
sentiment = classify_sentiment(review_text)
|
68 |
+
reason = analyze_feedback(review_text)
|
69 |
+
return f"Sentiment: {sentiment}\nReason: {reason}"
|
70 |
+
|
71 |
+
# Define the Gradio interface
|
72 |
+
def main_interface():
|
73 |
+
with gr.Blocks() as demo:
|
74 |
+
gr.Markdown("## Sentiment and Reason Extraction for Product Reviews")
|
75 |
+
|
76 |
+
with gr.Row():
|
77 |
+
review_input = gr.Textbox(label="Enter your review:", lines=5, placeholder="Type your product review here...")
|
78 |
+
output_box = gr.Textbox(label="Output:", lines=5, placeholder="Sentiment and reason will be displayed here...")
|
79 |
+
|
80 |
+
analyze_button = gr.Button("Analyze")
|
81 |
+
|
82 |
+
analyze_button.click(fn=sentiment_and_reason, inputs=review_input, outputs=output_box)
|
83 |
+
|
84 |
+
return demo
|
85 |
+
|
86 |
+
# Launch the Gradio app
|
87 |
+
main_interface().launch()
|