Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,116 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import json
|
| 3 |
+
from transformers import pipeline
|
| 4 |
+
|
| 5 |
+
# Load pre-trained models
|
| 6 |
+
sentiment_analyzer = pipeline('sentiment-analysis', model='nlptown/bert-base-multilingual-uncased-sentiment')
|
| 7 |
+
emotion_analyzer = pipeline('text-classification', model='j-hartmann/emotion-english-distilroberta-base')
|
| 8 |
+
intent_analyzer = pipeline('text-classification', model='Falconsai/intent_classification')
|
| 9 |
+
|
| 10 |
+
# Function to determine response length based on text length
|
| 11 |
+
def determine_response_length(text):
|
| 12 |
+
length = len(text.split())
|
| 13 |
+
if length < 5:
|
| 14 |
+
return "short"
|
| 15 |
+
elif length < 15:
|
| 16 |
+
return "medium"
|
| 17 |
+
else:
|
| 18 |
+
return "long"
|
| 19 |
+
|
| 20 |
+
# Analyze conversation function
|
| 21 |
+
def analyze_conversation(conversation):
|
| 22 |
+
output = {"conversation": []}
|
| 23 |
+
|
| 24 |
+
for entry in conversation:
|
| 25 |
+
speaker = entry['speaker']
|
| 26 |
+
text = entry['text']
|
| 27 |
+
|
| 28 |
+
# Sentiment analysis
|
| 29 |
+
sentiment_result = sentiment_analyzer(text)
|
| 30 |
+
sentiment = sentiment_result[0]['label']
|
| 31 |
+
|
| 32 |
+
# Emotion detection
|
| 33 |
+
emotion_result = emotion_analyzer(text)
|
| 34 |
+
emotion = emotion_result[0]['label']
|
| 35 |
+
|
| 36 |
+
# Intent recognition
|
| 37 |
+
intent_result = intent_analyzer(text)
|
| 38 |
+
intent = intent_result[0]['label']
|
| 39 |
+
|
| 40 |
+
# Assign other attributes
|
| 41 |
+
tone = "formal" if speaker == "AGENT" else "informal"
|
| 42 |
+
confidence_level = "high" if speaker == "AGENT" else "low"
|
| 43 |
+
frustration_level = "low" if sentiment == "positive" else "medium"
|
| 44 |
+
response_length = determine_response_length(text)
|
| 45 |
+
action_required = "no" if speaker == "AGENT" else "yes"
|
| 46 |
+
interruption = "no"
|
| 47 |
+
cooperation_level = "high" if sentiment == "positive" else "medium"
|
| 48 |
+
clarity = "clear" if speaker == "AGENT" else "confusing"
|
| 49 |
+
objective = "inform_customer" if speaker == "AGENT" else "get_clarification"
|
| 50 |
+
timeline = "present"
|
| 51 |
+
motivation = "inform_update" if speaker == "AGENT" else "verify_information"
|
| 52 |
+
conversation_stage = "development"
|
| 53 |
+
resolution = "pending"
|
| 54 |
+
context = "sales"
|
| 55 |
+
urgency = "low" if speaker == "AGENT" else "medium"
|
| 56 |
+
problem_type = "contract_update"
|
| 57 |
+
key_words = ["contract", "electricity"] # Consider dynamic extraction here
|
| 58 |
+
expected_detail = "basic" if speaker == "AGENT" else "detailed"
|
| 59 |
+
time_gap = "normal"
|
| 60 |
+
client_expectation = "get_information"
|
| 61 |
+
channel = "telephone"
|
| 62 |
+
power_relationship = "balanced"
|
| 63 |
+
|
| 64 |
+
# Add the entry to the conversation
|
| 65 |
+
output["conversation"].append({
|
| 66 |
+
"speaker": speaker,
|
| 67 |
+
"text": text,
|
| 68 |
+
"sentiment": sentiment,
|
| 69 |
+
"emotion": emotion,
|
| 70 |
+
"intent": intent,
|
| 71 |
+
"tone": tone,
|
| 72 |
+
"confidence_level": confidence_level,
|
| 73 |
+
"frustration_level": frustration_level,
|
| 74 |
+
"response_length": response_length,
|
| 75 |
+
"action_required": action_required,
|
| 76 |
+
"interruption": interruption,
|
| 77 |
+
"cooperation_level": cooperation_level,
|
| 78 |
+
"clarity": clarity,
|
| 79 |
+
"objective": objective,
|
| 80 |
+
"timeline": timeline,
|
| 81 |
+
"motivation": motivation,
|
| 82 |
+
"conversation_stage": conversation_stage,
|
| 83 |
+
"resolution": resolution,
|
| 84 |
+
"context": context,
|
| 85 |
+
"urgency": urgency,
|
| 86 |
+
"problem_type": problem_type,
|
| 87 |
+
"key_words": key_words,
|
| 88 |
+
"expected_detail": expected_detail,
|
| 89 |
+
"time_gap": time_gap,
|
| 90 |
+
"client_expectation": client_expectation,
|
| 91 |
+
"channel": channel,
|
| 92 |
+
"power_relationship": power_relationship
|
| 93 |
+
})
|
| 94 |
+
|
| 95 |
+
return output
|
| 96 |
+
|
| 97 |
+
# Streamlit app layout
|
| 98 |
+
st.title("Conversation Labeling App")
|
| 99 |
+
st.write("Upload a JSON file containing conversations in the specified format.")
|
| 100 |
+
|
| 101 |
+
# File uploader
|
| 102 |
+
uploaded_file = st.file_uploader("Choose a JSON file", type="json")
|
| 103 |
+
|
| 104 |
+
if uploaded_file is not None:
|
| 105 |
+
# Read the uploaded file
|
| 106 |
+
data = json.load(uploaded_file)
|
| 107 |
+
|
| 108 |
+
if "conversation" in data:
|
| 109 |
+
# Analyze the conversation and generate the output
|
| 110 |
+
result = analyze_conversation(data["conversation"])
|
| 111 |
+
|
| 112 |
+
# Display the output
|
| 113 |
+
st.subheader("Labeled Conversation")
|
| 114 |
+
st.json(result)
|
| 115 |
+
else:
|
| 116 |
+
st.error("The uploaded JSON file does not contain a 'conversation' key.")
|