JoshuaZywoo commited on
Commit
da8d9a8
·
verified ·
1 Parent(s): 0db3c6a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +9 -9
app.py CHANGED
@@ -5,7 +5,7 @@ import re
5
  # Load models
6
  emotion_classifier = pipeline("text-classification", model="bhadresh-savani/distilbert-base-uncased-emotion", top_k=1)
7
  intent_classifier = pipeline("zero-shot-classification", model="facebook/bart-large-mnli")
8
- text_generator = pipeline("text2text-generation", model="google/flan-t5-base", max_new_tokens=100)
9
 
10
  # Candidate intents
11
  candidate_tasks = [
@@ -43,7 +43,11 @@ def get_content_score(text, top_intents):
43
  return min(score + 0.1, 1.0)
44
 
45
  def generate_reply(input_text, intent):
46
- prompt = f"Generate a polite and helpful customer service reply to this intent: '{intent}'. Customer said: '{input_text}'"
 
 
 
 
47
  return text_generator(prompt)[0]['generated_text']
48
 
49
  # UI
@@ -60,17 +64,13 @@ if st.button("Analyze Message"):
60
 
61
  # Emotion detection
62
  emotion_result = emotion_classifier(user_input)
63
- if isinstance(emotion_result[0], list): # Handle top_k output format
64
- emotion_data = emotion_result[0][0]
65
- else:
66
- emotion_data = emotion_result[0]
67
-
68
  emotion_label = emotion_data['label']
69
  emotion_score = get_emotion_score(emotion_label)
70
 
71
  # Intent detection
72
  intent_result = intent_classifier(user_input, candidate_tasks)
73
- top_intents = [label for label, score in zip(intent_result['labels'], intent_result['scores']) if score > 0.2]
74
 
75
  # Content score
76
  content_score = get_content_score(user_input, top_intents)
@@ -102,6 +102,6 @@ if st.button("Analyze Message"):
102
  for intent in top_intents:
103
  reply = generate_reply(user_input, intent)
104
  st.markdown(f"**• {intent.capitalize()}**")
105
- st.write(f"Recommended Action: {reply}")
106
  else:
107
  st.warning("No clear intent detected. Manual review recommended.")
 
5
  # Load models
6
  emotion_classifier = pipeline("text-classification", model="bhadresh-savani/distilbert-base-uncased-emotion", top_k=1)
7
  intent_classifier = pipeline("zero-shot-classification", model="facebook/bart-large-mnli")
8
+ text_generator = pipeline("text2text-generation", model="google/flan-t5-base", max_new_tokens=150)
9
 
10
  # Candidate intents
11
  candidate_tasks = [
 
43
  return min(score + 0.1, 1.0)
44
 
45
  def generate_reply(input_text, intent):
46
+ prompt = (
47
+ f"Write a professional customer service response to the following customer request: '{input_text}'\n"
48
+ f"The request is related to: '{intent}'.\n"
49
+ f"Start with a polite sentence, then provide a clear solution, and end with a kind question offering further help."
50
+ )
51
  return text_generator(prompt)[0]['generated_text']
52
 
53
  # UI
 
64
 
65
  # Emotion detection
66
  emotion_result = emotion_classifier(user_input)
67
+ emotion_data = emotion_result[0][0] if isinstance(emotion_result[0], list) else emotion_result[0]
 
 
 
 
68
  emotion_label = emotion_data['label']
69
  emotion_score = get_emotion_score(emotion_label)
70
 
71
  # Intent detection
72
  intent_result = intent_classifier(user_input, candidate_tasks)
73
+ top_intents = [label for label, score in zip(intent_result['labels'], intent_result['scores']) if score > 0.15][:3]
74
 
75
  # Content score
76
  content_score = get_content_score(user_input, top_intents)
 
102
  for intent in top_intents:
103
  reply = generate_reply(user_input, intent)
104
  st.markdown(f"**• {intent.capitalize()}**")
105
+ st.write(reply)
106
  else:
107
  st.warning("No clear intent detected. Manual review recommended.")