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.")