Spaces:
Sleeping
Sleeping
Update app.py
Browse files
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=
|
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 =
|
|
|
|
|
|
|
|
|
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)
|
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.
|
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(
|
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.")
|