Zasha1 commited on
Commit
f244f02
·
verified ·
1 Parent(s): e5c1252

Delete main.py

Browse files
Files changed (1) hide show
  1. main.py +0 -117
main.py DELETED
@@ -1,117 +0,0 @@
1
- import streamlit as st
2
- import pyaudio
3
- import json
4
- from vosk import Model, KaldiRecognizer
5
- from transformers import pipeline, AutoModelForSequenceClassification, AutoTokenizer
6
- from sentence_transformers import SentenceTransformer
7
- import time
8
- import pandas as pd
9
- from dotenv import load_dotenv
10
- import os
11
- import numpy as np
12
-
13
- def cosine_similarity(a, b):
14
- return np.dot(a, b) / (np.linalg.norm(a) * np.linalg.norm(b))
15
-
16
- class SalesAnalysisApp:
17
- def __init__(self):
18
- model_name = "tabularisai/multilingual-sentiment-analysis"
19
- model = AutoModelForSequenceClassification.from_pretrained(model_name)
20
- tokenizer = AutoTokenizer.from_pretrained(model_name)
21
- self.sentiment_analyzer = pipeline("sentiment-analysis", model=model, tokenizer=tokenizer)
22
-
23
- vosk_model_path = os.getenv("VOSK_MODEL_PATH")
24
- self.vosk_model = Model(vosk_model_path)
25
- self.recognizer = KaldiRecognizer(self.vosk_model, 16000)
26
-
27
- self.audio = pyaudio.PyAudio()
28
- self.stream = None
29
-
30
- self.product_data = pd.read_csv(r"C:\Users\shaik\Downloads\Sales Calls Transcriptions - Sheet2.csv")
31
- self.objection_data = pd.read_csv(r"C:\Users\shaik\Downloads\Sales Calls Transcriptions - Sheet3.csv")
32
-
33
- self.sentence_model = SentenceTransformer('all-MiniLM-L6-v2')
34
-
35
- def get_recommendations(self, text):
36
- text_embedding = self.sentence_model.encode([text])
37
- product_embeddings = self.sentence_model.encode(self.product_data['Description'].tolist())
38
-
39
- similarities = [cosine_similarity(text_embedding[0], prod_emb) for prod_emb in product_embeddings]
40
- top_indices = np.argsort(similarities)[-5:][::-1]
41
- return self.product_data.iloc[top_indices]['Product'].tolist()
42
-
43
- def get_objection_response(self, text):
44
- text_embedding = self.sentence_model.encode([text])
45
- objection_embeddings = self.sentence_model.encode(self.objection_data['Objection'].tolist())
46
-
47
- similarities = [cosine_similarity(text_embedding[0], obj_emb) for obj_emb in objection_embeddings]
48
- max_similarity = max(similarities)
49
- if max_similarity > 0.5:
50
- top_idx = np.argmax(similarities)
51
- return self.objection_data.iloc[top_idx]['Response']
52
- return None
53
-
54
- # Rest of the code remains the same...
55
- def analyze_sentiment(self, text):
56
- if not text.strip():
57
- return "NEUTRAL", 0.0
58
- result = self.sentiment_analyzer(text.strip().lower())[0]
59
- sentiment_map = {
60
- 'Very Negative': "NEGATIVE",
61
- 'Negative': "NEGATIVE",
62
- 'Neutral': "NEUTRAL",
63
- 'Positive': "POSITIVE",
64
- 'Very Positive': "POSITIVE"
65
- }
66
- return sentiment_map.get(result['label'], "NEUTRAL"), result['score']
67
-
68
- def run_app(self):
69
- st.title("Real-time Sales Call Analysis")
70
-
71
- if st.button("Start Recording"):
72
- self.stream = self.audio.open(format=pyaudio.paInt16,
73
- channels=1,
74
- rate=16000,
75
- input=True,
76
- frames_per_buffer=4000)
77
-
78
- transcript_placeholder = st.empty()
79
- sentiment_placeholder = st.empty()
80
- recommendations_placeholder = st.empty()
81
- objections_placeholder = st.empty()
82
-
83
- try:
84
- while True:
85
- data = self.stream.read(4000, exception_on_overflow=False)
86
-
87
- if self.recognizer.AcceptWaveform(data):
88
- result = json.loads(self.recognizer.Result())
89
- text = result["text"]
90
-
91
- if text:
92
- transcript_placeholder.write(f"Transcription: {text}")
93
-
94
- sentiment, score = self.analyze_sentiment(text)
95
- sentiment_placeholder.write(f"Sentiment: {sentiment} (Score: {score:.2f})")
96
-
97
- recommendations = self.get_recommendations(text)
98
- if recommendations:
99
- recommendations_placeholder.write("Product Recommendations:")
100
- for rec in recommendations:
101
- recommendations_placeholder.write(f"- {rec}")
102
-
103
- objection_response = self.get_objection_response(text)
104
- if objection_response:
105
- objections_placeholder.write(f"Suggested Response: {objection_response}")
106
-
107
- time.sleep(0.1)
108
-
109
- except Exception as e:
110
- st.error(f"Error: {str(e)}")
111
- if self.stream:
112
- self.stream.stop_stream()
113
- self.stream.close()
114
-
115
- if __name__ == "__main__":
116
- app = SalesAnalysisApp()
117
- app.run_app()