File size: 12,314 Bytes
943c198
 
ecaceca
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3d4c19c
 
ecaceca
 
943c198
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ecaceca
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
943c198
ecaceca
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
import numpy as np
import sounddevice as sd
import speech_recognition as sr
from sentiment_analysis import analyze_sentiment
from product_recommender import ProductRecommender
from objection_handler import ObjectionHandler
from google_sheets import fetch_call_data, store_data_in_sheet
from sentence_transformers import SentenceTransformer
from env_setup import config
import re
import uuid
from google.oauth2 import service_account
from googleapiclient.discovery import build
import pandas as pd
import plotly.express as px
import plotly.graph_objs as go
import streamlit as st 

# Initialize components
objection_handler = ObjectionHandler('objections.csv')
product_recommender = ProductRecommender('recommendations.csv')
model = SentenceTransformer('all-MiniLM-L6-v2')

def record_audio(duration=5, sample_rate=16000):
    """
    Record audio using sounddevice and return as NumPy array
    """
    st.write("Recording...")
    audio = sd.rec(int(duration * sample_rate), samplerate=sample_rate, channels=1, dtype='float32')
    sd.wait()
    st.write("Recording finished.")
    return audio.flatten(), sample_rate
    
def numpy_to_audio_data(audio_data):
    """
    Convert NumPy array to AudioData for speech_recognition
    """
    # Convert float32 to int16
    int_audio = (audio_data * 32767).astype(np.int16)
    
    # Create AudioData object
    recognizer = sr.Recognizer()
    audio_data = sr.AudioData(
        int_audio.tobytes(), 
        sample_rate=16000, 
        sample_width=int_audio.dtype.itemsize
    )
    return audio_data

def real_time_analysis():
    recognizer = sr.Recognizer()
    sentiment_scores = []
    transcribed_chunks = []
    total_text = ""

    try:
        while True:
            # Record audio using sounddevice
            audio_array, sample_rate = record_audio()
            
            # Convert NumPy array to AudioData
            audio_data = numpy_to_audio_data(audio_array)

            try:
                # Transcribe using speech_recognition
                text = recognizer.recognize_google(audio_data)
                st.write(f"*Recognized Text:* {text}")

                if 'stop' in text.lower():
                    st.write("Stopping real-time analysis...")
                    break

                # Append to the total conversation
                total_text += text + " "
                sentiment, score = analyze_sentiment(text)
                sentiment_scores.append(score)
                
                # Handle objection
                objection_response = handle_objection(text)

                # Get product recommendation
                recommendations = []
                if is_valid_input(text) and is_relevant_sentiment(score):
                    query_embedding = model.encode([text])
                    distances, indices = product_recommender.index.search(query_embedding, 1)

                    if distances[0][0] < 1.5:  # Similarity threshold
                        recommendations = product_recommender.get_recommendations(text)

                transcribed_chunks.append((text, sentiment, score))

                st.write(f"*Sentiment:* {sentiment} (Score: {score})")
                st.write(f"*Objection Response:* {objection_response}")
                
                if recommendations:
                    st.write("*Product Recommendations:*")
                    for rec in recommendations:
                        st.write(rec)

            except sr.UnknownValueError:
                st.error("Speech Recognition could not understand the audio.")
            except sr.RequestError as e:
                st.error(f"Error with the Speech Recognition service: {e}")
            except Exception as e:
                st.error(f"Error during processing: {e}")

        # After conversation ends, calculate and display overall sentiment and summary
        overall_sentiment = calculate_overall_sentiment(sentiment_scores)
        call_summary = generate_comprehensive_summary(transcribed_chunks)
        
        st.subheader("Conversation Summary:")
        st.write(total_text.strip())
        st.subheader("Overall Sentiment:")
        st.write(overall_sentiment)

        # Store data in Google Sheets
        store_data_in_sheet(
            config["google_sheet_id"], 
            transcribed_chunks, 
            call_summary, 
            overall_sentiment
        )
        st.success("Conversation data stored successfully in Google Sheets!")

    except Exception as e:
        st.error(f"Error in real-time analysis: {e}")

def generate_comprehensive_summary(chunks):
    """
    Generate a comprehensive summary from conversation chunks
    """
    # Extract full text from chunks
    full_text = " ".join([chunk[0] for chunk in chunks])
    
    # Perform basic analysis
    total_chunks = len(chunks)
    sentiments = [chunk[1] for chunk in chunks]
    
    # Determine overall conversation context
    context_keywords = {
        'product_inquiry': ['dress', 'product', 'price', 'stock'],
        'pricing': ['cost', 'price', 'budget'],
        'negotiation': ['installment', 'payment', 'manage']
    }
    
    # Detect conversation themes
    themes = []
    for keyword_type, keywords in context_keywords.items():
        if any(keyword.lower() in full_text.lower() for keyword in keywords):
            themes.append(keyword_type)
    
    # Basic sentiment analysis
    positive_count = sentiments.count('POSITIVE')
    negative_count = sentiments.count('NEGATIVE')
    neutral_count = sentiments.count('NEUTRAL')
    
    # Key interaction highlights
    key_interactions = []
    for chunk in chunks:
        if any(keyword.lower() in chunk[0].lower() for keyword in ['price', 'dress', 'stock', 'installment']):
            key_interactions.append(chunk[0])
    
    # Construct summary
    summary = f"Conversation Summary:\n"
    
    # Context and themes
    if 'product_inquiry' in themes:
        summary += "• Customer initiated a product inquiry about items.\n"
    
    if 'pricing' in themes:
        summary += "• Price and budget considerations were discussed.\n"
    
    if 'negotiation' in themes:
        summary += "• Customer and seller explored flexible payment options.\n"
    
    # Sentiment insights
    summary += f"\nConversation Sentiment:\n"
    summary += f"• Positive Interactions: {positive_count}\n"
    summary += f"• Negative Interactions: {negative_count}\n"
    summary += f"• Neutral Interactions: {neutral_count}\n"
    
    # Key highlights
    summary += "\nKey Conversation Points:\n"
    for interaction in key_interactions[:3]:  # Limit to top 3 key points
        summary += f"• {interaction}\n"
    
    # Conversation outcome
    if positive_count > negative_count:
        summary += "\nOutcome: Constructive and potentially successful interaction."
    elif negative_count > positive_count:
        summary += "\nOutcome: Interaction may require further follow-up."
    else:
        summary += "\nOutcome: Neutral interaction with potential for future engagement."
    
    return summary


def is_valid_input(text):
    text = text.strip().lower()
    if len(text) < 3 or re.match(r'^[a-zA-Z\s]*$', text) is None:
        return False
    return True

def is_relevant_sentiment(sentiment_score):
    return sentiment_score > 0.4

def calculate_overall_sentiment(sentiment_scores):
    if sentiment_scores:
        average_sentiment = sum(sentiment_scores) / len(sentiment_scores)
        overall_sentiment = (
            "POSITIVE" if average_sentiment > 0 else
            "NEGATIVE" if average_sentiment < 0 else
            "NEUTRAL"
        )
    else:
        overall_sentiment = "NEUTRAL"
    return overall_sentiment

def handle_objection(text):
    query_embedding = model.encode([text])
    distances, indices = objection_handler.index.search(query_embedding, 1)
    if distances[0][0] < 1.5:  # Adjust similarity threshold as needed
        responses = objection_handler.handle_objection(text)
        return "\n".join(responses) if responses else "No objection response found."
    return "No objection response found."


def run_app():
    st.set_page_config(page_title="Sales Call Assistant", layout="wide")
    st.title("AI Sales Call Assistant")

    st.sidebar.title("Navigation")
    app_mode = st.sidebar.radio("Choose a mode:", ["Real-Time Call Analysis", "Dashboard"])

    if app_mode == "Real-Time Call Analysis":
        st.header("Real-Time Sales Call Analysis")
        if st.button("Start Listening"):
            real_time_analysis()

    elif app_mode == "Dashboard":
        st.header("Call Summaries and Sentiment Analysis")
        try:
            data = fetch_call_data(config["google_sheet_id"])
            if data.empty:
                st.warning("No data available in the Google Sheet.")
            else:
                # Sentiment Visualizations
                sentiment_counts = data['Sentiment'].value_counts()
                
                # Pie Chart
                col1, col2 = st.columns(2)
                with col1:
                    st.subheader("Sentiment Distribution")
                    fig_pie = px.pie(
                        values=sentiment_counts.values, 
                        names=sentiment_counts.index, 
                        title='Call Sentiment Breakdown',
                        color_discrete_map={
                            'POSITIVE': 'green', 
                            'NEGATIVE': 'red', 
                            'NEUTRAL': 'blue'
                        }
                    )
                    st.plotly_chart(fig_pie)

                # Bar Chart
                with col2:
                    st.subheader("Sentiment Counts")
                    fig_bar = px.bar(
                        x=sentiment_counts.index, 
                        y=sentiment_counts.values, 
                        title='Number of Calls by Sentiment',
                        labels={'x': 'Sentiment', 'y': 'Number of Calls'},
                        color=sentiment_counts.index,
                        color_discrete_map={
                            'POSITIVE': 'green', 
                            'NEGATIVE': 'red', 
                            'NEUTRAL': 'blue'
                        }
                    )
                    st.plotly_chart(fig_bar)

                # Existing Call Details Section
                st.subheader("All Calls")
                display_data = data.copy()
                display_data['Summary Preview'] = display_data['Summary'].str[:100] + '...'
                st.dataframe(display_data[['Call ID', 'Chunk', 'Sentiment', 'Summary Preview', 'Overall Sentiment']])

                # Dropdown to select Call ID
                unique_call_ids = data[data['Call ID'] != '']['Call ID'].unique()
                call_id = st.selectbox("Select a Call ID to view details:", unique_call_ids)

                # Display selected Call ID details
                call_details = data[data['Call ID'] == call_id]
                if not call_details.empty:
                    st.subheader("Detailed Call Information")
                    st.write(f"**Call ID:** {call_id}")
                    st.write(f"**Overall Sentiment:** {call_details.iloc[0]['Overall Sentiment']}")
                    
                    # Expand summary section
                    st.subheader("Full Call Summary")
                    st.text_area("Summary:", 
                                 value=call_details.iloc[0]['Summary'], 
                                 height=200, 
                                 disabled=True)
                    
                    # Show all chunks for the selected call
                    st.subheader("Conversation Chunks")
                    for _, row in call_details.iterrows():
                        if pd.notna(row['Chunk']):  
                            st.write(f"**Chunk:** {row['Chunk']}")
                            st.write(f"**Sentiment:** {row['Sentiment']}")
                            st.write("---")  # Separator between chunks
                else:
                    st.error("No details available for the selected Call ID.")
        except Exception as e:
            st.error(f"Error loading dashboard: {e}")

if __name__ == "__main__":
    run_app()