joey1101 commited on
Commit
2253128
·
verified ·
1 Parent(s): a51e4d3

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +105 -92
app.py CHANGED
@@ -56,101 +56,114 @@ def analyze_dominant_emotion(user_review):
56
  dominant_emotion = max(emotion_results, key=lambda x: x['score']) # Identify the emotion with the highest confidence
57
  return dominant_emotion # Return the dominant emotion (label and score)
58
 
 
59
  ##########################################
60
- # Step 2: Response Generation Function
61
  ##########################################
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
62
  def response_gen(user_review):
63
- """
64
- Generate a concise and logical response based on the sentiment of the user's comment.
65
- """
66
- dominant_emotion = analyze_dominant_emotion(user_review) # Determine the dominant emotion from the user's comment
67
- emotion_label = dominant_emotion['label'].lower() # Extract the emotion label in lowercase format
68
-
69
- # Define response templates for each emotion
70
- emotion_prompts = {
71
- "anger": (
72
- f"'{user_review}'\n\n"
73
- "As a customer service representative, craft a professional response that:\n"
74
- "- Begins with sincere apology and acknowledgment\n"
75
- "- Clearly explains solution process with concrete steps\n"
76
- "- Offers appropriate compensation/redemption\n"
77
- "- Keeps tone humble and solution-focused (3-4 sentences)\n\n"
78
- "Response:"
79
- ),
80
- "disgust": (
81
- f"'{user_review}'\n\n"
82
- "As a customer service representative, craft a response that:\n"
83
- "- Immediately acknowledges the product issue\n"
84
- "- Explains quality control measures being taken\n"
85
- "- Provides clear return/replacement instructions\n"
86
- "- Offers goodwill gesture (3-4 sentences)\n\n"
87
- "Response:"
88
- ),
89
- "fear": (
90
- f"'{user_review}'\n\n"
91
- "As a customer service representative, craft a reassuring response that:\n"
92
- "- Directly addresses the safety worries\n"
93
- "- References relevant certifications/standards\n"
94
- "- Offers dedicated support contact\n"
95
- "- Provides satisfaction guarantee (3-4 sentences)\n\n"
96
- "Response:"
97
- ),
98
- "joy": (
99
- f"'{user_review}'\n\n"
100
- "As a customer service representative, craft a concise and enthusiastic response that:\n"
101
- "- Thanks the customer for their feedback\n"
102
- "- Acknowledges both positive and constructive comments\n"
103
- "- Invites them to explore loyalty programs\n\n"
104
- "Response:"
105
- ),
106
- "neutral": (
107
- f"'{user_review}'\n\n"
108
- "As a customer service representative, craft a balanced response that:\n"
109
- "- Provides additional relevant product information\n"
110
- "- Highlights key service features\n"
111
- "- Politely requests more detailed feedback\n"
112
- "- Maintains professional tone (3-4 sentences)\n\n"
113
- "Response:"
114
- ),
115
- "sadness": (
116
- f"'{user_review}'\n\n"
117
- "As a customer service representative, craft an empathetic response that:\n"
118
- "- Shows genuine understanding of the issue\n"
119
- "- Proposes personalized recovery solution\n"
120
- "- Offers extended support options\n"
121
- "- Maintains positive outlook (3-4 sentences)\n\n"
122
- "Response:"
123
- ),
124
- "surprise": (
125
- f"'{user_review}'\n\n"
126
- "As a customer service representative, craft a response that:\n"
127
- "- Matches customer's positive energy appropriately\n"
128
- "- Highlights unexpected product benefits\n"
129
- "- Invites to user community/events\n"
130
- "- Maintains brand voice (3-4 sentences)\n\n"
131
- "Response:"
132
- )
133
- }
134
-
135
- prompt = emotion_prompts.get(
136
- emotion_label,
137
- f"Neutral feedback: '{user_review}'\n\nWrite a professional and concise response (50-200 words max).\n\nResponse:"
138
- ) # Default to neutral if emotion is not found
139
-
140
- # Load the tokenizer and language model for response generation
141
- tokenizer = AutoTokenizer.from_pretrained("Qwen/Qwen1.5-0.5B") # Load tokenizer for text processing
142
- model = AutoModelForCausalLM.from_pretrained("Qwen/Qwen1.5-0.5B") # Load language model for response generation
143
-
144
- inputs = tokenizer(prompt, return_tensors="pt") # Tokenize the input prompt
145
- outputs = model.generate(
146
- **inputs,
147
- max_new_tokens=150, # Limit generated tokens to ensure concise responses
148
- min_length=50, # Ensure the generated response is logical and complete
149
- no_repeat_ngram_size=2, # Avoid repetitive phrases
150
- temperature=0.7 # Add randomness for more natural responses
151
- )
152
- response = tokenizer.decode(outputs[0], skip_special_tokens=True) # Decode the generated response
153
- return response # Return the response
154
 
155
  ##########################################
156
  # Step 3: Text-to-Speech Conversion Function
 
56
  dominant_emotion = max(emotion_results, key=lambda x: x['score']) # Identify the emotion with the highest confidence
57
  return dominant_emotion # Return the dominant emotion (label and score)
58
 
59
+
60
  ##########################################
61
+ # Step 2Response Generation Function
62
  ##########################################
63
+
64
+ def prompt_gen(user_review):
65
+ dominant_emotion = analyze_dominant_emotion(user_review)
66
+ emotion_strategies = {
67
+ "anger": {
68
+ "prompt": (
69
+ "Customer complaint: '{review}'\n\n"
70
+ "As a customer service representative, craft a professional response that:\n"
71
+ "- Begins with sincere apology and acknowledgment\n"
72
+ "- Clearly explains solution process with concrete steps\n"
73
+ "- Offers appropriate compensation/redemption\n"
74
+ "- Keeps tone humble and solution-focused (3-4 sentences)\n\n"
75
+ "Response:"
76
+ )
77
+ },
78
+ "disgust": {
79
+ "prompt": (
80
+ "Customer quality concern: '{review}'\n\n"
81
+ "As a customer service representative, craft a response that:\n"
82
+ "- Immediately acknowledges the product issue\n"
83
+ "- Explains quality control measures being taken\n"
84
+ "- Provides clear return/replacement instructions\n"
85
+ "- Offers goodwill gesture (3-4 sentences)\n\n"
86
+ "Response:"
87
+ )
88
+ },
89
+ "fear": {
90
+ "prompt": (
91
+ "Customer safety concern: '{review}'\n\n"
92
+ "As a customer service representative, craft a reassuring response that:\n"
93
+ "- Directly addresses the safety worries\n"
94
+ "- References relevant certifications/standards\n"
95
+ "- Offers dedicated support contact\n"
96
+ "- Provides satisfaction guarantee (3-4 sentences)\n\n"
97
+ "Response:"
98
+ )
99
+ },
100
+ "joy": {
101
+ "prompt": (
102
+ "Customer review: '{review}'\n\n"
103
+ "As a customer service representative, craft a concise response that:\n"
104
+ "- Specifically acknowledges both positive and constructive feedback\n"
105
+ "- Briefly mentions loyalty/referral programs\n"
106
+ "- Ends with shopping invitation (3-4 sentences)\n\n"
107
+ "Response:"
108
+ )
109
+ },
110
+ "neutral": {
111
+ "prompt": (
112
+ "Customer feedback: '{review}'\n\n"
113
+ "As a customer service representative, craft a balanced response that:\n"
114
+ "- Provides additional relevant product information\n"
115
+ "- Highlights key service features\n"
116
+ "- Politely requests more detailed feedback\n"
117
+ "- Maintains professional tone (3-4 sentences)\n\n"
118
+ "Response:"
119
+ )
120
+ },
121
+ "sadness": {
122
+ "prompt": (
123
+ "Customer disappointment: '{review}'\n\n"
124
+ "As a customer service representative, craft an empathetic response that:\n"
125
+ "- Shows genuine understanding of the issue\n"
126
+ "- Proposes personalized recovery solution\n"
127
+ "- Offers extended support options\n"
128
+ "- Maintains positive outlook (3-4 sentences)\n\n"
129
+ "Response:"
130
+ )
131
+ },
132
+ "surprise": {
133
+ "prompt": (
134
+ "Customer enthusiastic feedback: '{review}'\n\n"
135
+ "As a customer service representative, craft a response that:\n"
136
+ "- Matches customer's positive energy appropriately\n"
137
+ "- Highlights unexpected product benefits\n"
138
+ "- Invites to user community/events\n"
139
+ "- Maintains brand voice (3-4 sentences)\n\n"
140
+ "Response:"
141
+ )
142
+ }
143
+ }
144
+
145
+ template = emotion_strategies[dominant_emotion['label'].lower()]["prompt"]
146
+ prompt = template.format(review=user_review)
147
+ print(prompt)
148
+ return prompt
149
+
150
  def response_gen(user_review):
151
+ prompt = prompt_gen(user_review)
152
+ # Load model directly
153
+ from transformers import AutoTokenizer, AutoModelForCausalLM
154
+
155
+ tokenizer = AutoTokenizer.from_pretrained("Qwen/Qwen1.5-0.5B")
156
+ model = AutoModelForCausalLM.from_pretrained("Qwen/Qwen1.5-0.5B")
157
+
158
+ inputs = tokenizer(prompt, return_tensors="pt")
159
+ outputs = model.generate(**inputs, max_new_tokens=200)
160
+
161
+ input_length = inputs.input_ids.shape[1]
162
+ response = tokenizer.decode(outputs[0][input_length:], skip_special_tokens=True)
163
+ # print(response)
164
+ return response
165
+
166
+
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
167
 
168
  ##########################################
169
  # Step 3: Text-to-Speech Conversion Function