File size: 4,325 Bytes
c479345
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
import streamlit as st
import json
from transformers import pipeline

# Load pre-trained models
sentiment_analyzer = pipeline('sentiment-analysis', model='nlptown/bert-base-multilingual-uncased-sentiment')
emotion_analyzer = pipeline('text-classification', model='j-hartmann/emotion-english-distilroberta-base')
intent_analyzer = pipeline('text-classification', model='Falconsai/intent_classification')

# Function to determine response length based on text length
def determine_response_length(text):
    length = len(text.split())
    if length < 5:
        return "short"
    elif length < 15:
        return "medium"
    else:
        return "long"

# Analyze conversation function
def analyze_conversation(conversation):
    output = {"conversation": []}
    
    for entry in conversation:
        speaker = entry['speaker']
        text = entry['text']

        # Sentiment analysis
        sentiment_result = sentiment_analyzer(text)
        sentiment = sentiment_result[0]['label']

        # Emotion detection
        emotion_result = emotion_analyzer(text)
        emotion = emotion_result[0]['label']

        # Intent recognition
        intent_result = intent_analyzer(text)
        intent = intent_result[0]['label']

        # Assign other attributes
        tone = "formal" if speaker == "AGENT" else "informal"
        confidence_level = "high" if speaker == "AGENT" else "low"
        frustration_level = "low" if sentiment == "positive" else "medium"
        response_length = determine_response_length(text)
        action_required = "no" if speaker == "AGENT" else "yes"
        interruption = "no"
        cooperation_level = "high" if sentiment == "positive" else "medium"
        clarity = "clear" if speaker == "AGENT" else "confusing"
        objective = "inform_customer" if speaker == "AGENT" else "get_clarification"
        timeline = "present"
        motivation = "inform_update" if speaker == "AGENT" else "verify_information"
        conversation_stage = "development"
        resolution = "pending"
        context = "sales"
        urgency = "low" if speaker == "AGENT" else "medium"
        problem_type = "contract_update"
        key_words = ["contract", "electricity"]  # Consider dynamic extraction here
        expected_detail = "basic" if speaker == "AGENT" else "detailed"
        time_gap = "normal"
        client_expectation = "get_information"
        channel = "telephone"
        power_relationship = "balanced"

        # Add the entry to the conversation
        output["conversation"].append({
            "speaker": speaker,
            "text": text,
            "sentiment": sentiment,
            "emotion": emotion,
            "intent": intent,
            "tone": tone,
            "confidence_level": confidence_level,
            "frustration_level": frustration_level,
            "response_length": response_length,
            "action_required": action_required,
            "interruption": interruption,
            "cooperation_level": cooperation_level,
            "clarity": clarity,
            "objective": objective,
            "timeline": timeline,
            "motivation": motivation,
            "conversation_stage": conversation_stage,
            "resolution": resolution,
            "context": context,
            "urgency": urgency,
            "problem_type": problem_type,
            "key_words": key_words,
            "expected_detail": expected_detail,
            "time_gap": time_gap,
            "client_expectation": client_expectation,
            "channel": channel,
            "power_relationship": power_relationship
        })

    return output

# Streamlit app layout
st.title("Conversation Labeling App")
st.write("Upload a JSON file containing conversations in the specified format.")

# File uploader
uploaded_file = st.file_uploader("Choose a JSON file", type="json")

if uploaded_file is not None:
    # Read the uploaded file
    data = json.load(uploaded_file)
    
    if "conversation" in data:
        # Analyze the conversation and generate the output
        result = analyze_conversation(data["conversation"])
        
        # Display the output
        st.subheader("Labeled Conversation")
        st.json(result)
    else:
        st.error("The uploaded JSON file does not contain a 'conversation' key.")