File size: 19,717 Bytes
3b6438e
6b76852
3b6438e
6b76852
 
 
 
3b6438e
a6fd639
6aca5d1
a709ee5
43cb086
 
 
95b4ecb
6b76852
 
 
 
95b4ecb
 
 
 
 
 
 
 
 
af3c41e
0a21faa
95b4ecb
7098de0
6ef8af5
6b76852
3b6438e
 
 
 
6b76852
 
 
 
 
 
 
 
 
2af0547
 
9c56a6f
 
af3c41e
82d7727
 
 
 
 
 
 
 
 
 
 
 
 
6ef8af5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5c57155
 
af3c41e
2e454fc
 
82d7727
 
4ed8b2e
82d7727
2e454fc
4ed8b2e
2e454fc
 
 
 
5df8bb5
 
82d7727
2392be5
5df8bb5
 
 
 
 
 
 
 
 
 
 
8b81140
5df8bb5
 
 
 
 
 
 
6b76852
 
 
 
 
06a5249
6b76852
 
 
06a5249
df7ef83
6b76852
 
 
29e8d29
 
 
df7ef83
 
b8c50c2
df7ef83
82cc0f7
6b76852
29e8d29
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2af0547
6b76852
 
 
 
 
 
c7e99d2
6b76852
 
c7e99d2
6b76852
395710d
 
 
6b76852
 
 
 
395710d
 
 
 
 
 
 
6b76852
 
 
 
c7e99d2
6b76852
 
 
 
9c56a6f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6b76852
 
9c56a6f
 
 
 
6b76852
5da74e8
 
 
 
 
 
6b76852
 
 
 
 
 
 
5da74e8
9c56a6f
 
 
 
 
 
 
 
6b76852
82cc0f7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6b76852
 
 
 
 
82cc0f7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6b76852
3e83f10
 
6b76852
3b6438e
 
 
 
 
 
 
 
 
 
 
f83400b
3b6438e
f59b16a
3b6438e
 
 
 
72205e3
 
 
 
 
 
 
 
 
 
 
 
b769aed
72205e3
 
 
 
333fa1f
 
3b6438e
6b76852
 
 
 
 
 
 
 
 
43cb086
 
 
 
 
 
 
 
 
 
 
 
6b76852
 
 
 
 
 
 
 
8532cb7
6b76852
 
 
9c56a6f
6b76852
 
 
3e83f10
 
 
 
 
6b76852
 
3b6438e
 
72205e3
3b6438e
 
 
 
 
 
 
 
333fa1f
3b6438e
 
72205e3
 
 
 
 
 
 
 
 
 
 
 
 
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
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
from fastapi import FastAPI, UploadFile, File
from fastapi.middleware.cors import CORSMiddleware
from fastapi import HTTPException
from pydantic import BaseModel
import openai
from dotenv import load_dotenv
import os
from deepgram import DeepgramClient, PrerecordedOptions
from typing import List, Dict
from fastapi.responses import PlainTextResponse
import re
from starlette.middleware.base import BaseHTTPMiddleware
from starlette.requests import Request
from starlette.responses import Response
from openai import AzureOpenAI

# Load environment variables from .env file
load_dotenv()

# # Configure OpenAI for Azure
# openai.api_type = "azure"
# openai.api_base = "https://amplifai-openai.openai.azure.com/"
# # openai.api_version = "2023-07-01-preview"
# openai.api_version = "2024-08-01-preview"
# openai.api_key = os.getenv("OPENAI_API_KEY")

client = AzureOpenAI(
    api_key=os.getenv("OPENAI_API_KEY"),  
    api_version="2024-05-01-preview",
    azure_endpoint = "https://amplifai-openai.openai.azure.com/"
)

deployment_name='gpt-4'

DEEPGRAM_API_KEY = os.getenv("DEEPGRAM_API_KEY")
if not DEEPGRAM_API_KEY:
    raise Exception("Deepgram API key not found in environment variables")

app = FastAPI()

class Prompt(BaseModel):
    text: str

class GenerateResponse(BaseModel):
    text: str
class GenerateResponse2(BaseModel):
    text: dict
class GenerateResponse3(BaseModel):
    text: List[Dict[str, str]]
class GenerateResponse4(BaseModel):
    text: list

# #Prepare the chat prompt 
# chat_prompt = [
#     {
#         "role": "system",
#         "content": [
#             {
#                 "type": "text",
#                 "text": "The data is a pre-recorded conversation between a call center agent and a customer. Identify the following: keywords/metrics about the conversation, a summary of the conversation including details. Output should be in the format Keywords/Metrics: ,\n Summary:. \n Data: "
#             }
#         ]
#     }
# ] 

# #Summarize function
# def summarize_text(text: str):
#         response = client.chat.completions.create(
#             model = deployment_name,
#             #engine="AmplifAI-Chat",
#             messages="The data is a pre-recorded conversation between a call center agent and a customer. Identify the following: keywords/metrics about the conversation, a summary of the conversation including details. Output should be in the format Keywords/Metrics: ,\n Summary:. \n Data: " + text,
#             temperature=0.7,
#             max_tokens=750,
#             top_p=1.0,
#             frequency_penalty=0.0,
#             presence_penalty=0.0
#         )
#         if response.choices:
#             # Extract the text from the response
#             summary = response.choices[0].text.strip()
            
#             # Use regular expressions to find the keywords/metrics and summary sections
#             keywords_match = re.search(r"Keywords/Metrics:(.*?)(\n|$)", summary, re.S)
#             summary_match = re.search(r"Summary:(.*?)(\n|$)", summary, re.S)
            
#             # Extract the content if matches are found
#             keywords = keywords_match.group(1).strip() if keywords_match else "No keywords found"
#             conversation_summary = summary_match.group(1).strip() if summary_match else "No summary found"
            
#             # Return a clean version with just the keywords and summary
#             return GenerateResponse(text=f"Keywords/Metrics: {keywords}\nSummary: {conversation_summary}")
        
#         else:
#             return "No response from the API."
#         # if response.choices:
#         #     summary = response.choices[0].text.strip()
#         #     return GenerateResponse(text=summary)
#         # else:
#         #     return "No response from the API."


#Summarize function
def summarize_text(text: str):
        response = client.chat.completions.create(
            model = deployment_name,
            #engine="AmplifAI-Chat",
            messages=[
                {"role": "system", "content": "You are an AI assistant that summarizes conversations and extracts keywords/metrics."},
                {"role": "user", "content": f"The data is a pre-recorded conversation between a call center agent and a customer. Identify the following: \n\n1. Keywords/Metrics about the conversation \n2. A summary of the conversation including details (max 4 lines). \n\nOutput format: \nKeywords/Metrics: [Extracted Keywords]\nSummary: [Generated Summary]\n\nData: {text}"}
            ],
            temperature=0.7,
            max_tokens=150,
            top_p=1.0,
            frequency_penalty=0.0,
            presence_penalty=0.0
        )
        if response.choices:
            # Extract the text from the response
            # summary = response.choices[0].text.strip()
            summary = response.choices[0].message.content.strip()
            
            # Use regular expressions to find the keywords/metrics and summary sections
            keywords_match = re.search(r"Keywords/Metrics:(.*?)(\n|$)", summary, re.S)
            summary_match = re.search(r"Summary:(.*?)(\n|$)", summary, re.S)
            
            # Extract the content if matches are found
            keywords = keywords_match.group(1).strip() if keywords_match else "No keywords found"
            conversation_summary = summary_match.group(1).strip() if summary_match else "No summary found"
            
            # Return a clean version with just the keywords and summary
            return GenerateResponse(text=f"Keywords/Metrics: {keywords}\nSummary: {conversation_summary}")
        
        else:
            return "No response from the API."
        # if response.choices:
        #     summary = response.choices[0].text.strip()
        #     return GenerateResponse(text=summary)
        # else:
        #     return "No response from the API."

# QA Automation function
def qa_automation(text: str):
    questions = [
        "Did the agent verify the identity of the caller clearly? Provide Details.",
        #"Did the agent resolve the Primary reason of the call? Provide Details.",
        "Was the agent courteous? Provide Details.",
        # "Did the agent get a promise to pay? Provide Details."
        "Rate the satisfaction of the customer on a scale of 10. 0 being worse and 10 being best.",
        #"Rate the customer effort on a scale of 0-10. 0 being the most effort spent by the customer and 10 being least effort:",
        "How could the agent have made the call easier for the customer in upto 3 simple steps?"
    ]
    answers = []
    for question in questions:
        response = client.chat.completions.create(
            model = deployment_name,
            messages=[
                {"role": "system", "content": "You are an AI assistant evaluating a customer service call based on quality assurance criteria. Keep responses concise."},
                {"role": "user", "content": f"Question: {question}\nTranscript: {text}\nAnswer with a brief Yes/No and a short reason (max 2 sentences):"}
            ],
            max_tokens=100,
            temperature=0.5
        )
        if response.choices:
            answer = response.choices[0].message.content.strip()
            
            # Split the answer into the main answer (Yes/No) and the details
            if "Yes" in answer:
                main_answer, details = answer.split("Yes", 1)
                main_answer += "Yes"
            elif "No" in answer:
                main_answer, details = answer.split("No", 1)
                main_answer += "No"
            else:
                main_answer = answer
                details = "No additional details provided."
    
            # Format the answer with the "Comments:" tag in bold and on a new line
            # formatted_answer = f"**{question}** {main_answer}\n\n**Comments:** {details}" - change made on 8/27/2024
            # Change is to attain answers as a list of dictionaries (json) instead of a formatted string
            answer_dict = {
                "question": question,
                "answer": main_answer.strip(),
                "comments": details.strip()
            }
            answers.append(answer_dict) # Previously, we appended formatted_answer to 'answers'
        # return GenerateResponse(text = ("\n\n".join(answers)))
    return GenerateResponse3(text=answers)


def check_agent_steps(text: str):
    categories = {
        "Introduction": [
            "Greet the customer politely.",
            "Identify yourself and your company."        
        ],
        "Purpose of the Call": [
            "State the purpose of the call clearly."
        ],
        # "Account Review": [
        #     "Provide details of outstanding debt, the amount, due date, and charges or fees."
        # ],
        "Listen to the Customer": [
            "Give the customer an opportunity to explain their situation.",
            "Show empathy and understanding."
        ],
        # "Payment Discussion": [
        #     "Ask the customer about their ability to pay the outstanding amount."
        # ],
        # "Confirmation": [
        #     "Confirm the agreed-upon payment plan or next steps.",
        #     "Provide details on how the payment can be made (e.g., online, phone, mail)."
        # ],
        "Documentation": [
            "Offer to send a written confirmation of any agreements made during the call."
        ],
        "Closing": [
            "Thank the customer for their time and cooperation."
        ]
    }

    # Initialize a dictionary to store the results
    # results = {category: {} for category in categories}

    # # Iterate through each category and its questions
    # for category, questions in categories.items():
    #     for question in questions:
    #         # print("Checking question:", question)
    #         response = openai.Completion.create(
    #             engine="AmplifAI-Chat",
    #             prompt=f"Based on the transcript, did the agent follow the step: {question}? Provide a 'Yes' or 'No' answer.\nTranscript: {text}\nAnswer: ",
    #             temperature=0.2,
    #             max_tokens=10,
    #             top_p=1.0,
    #             frequency_penalty=0.0,
    #             presence_penalty=0.0,
    #             stop=["\n"]
    #         )
    #         answer = response.choices[0].text.strip()
    #         # emoji_answer = '✅' if answer == "Yes" else '❌'
    #         # print("Answer:", answer)
    #         results[category][question] = answer

    results = []

    for category, questions in categories.items():
        cat_map = {
            "title": category,
            "responses": []
        }
        for question in questions:
            response = client.chat.completions.create(
                model = deployment_name,
                messages=[
                    {"role": "system", "content": "You are an AI assistant evaluating call center agent performance. Keep answers concise."},
                    {"role": "user", "content": f"Based on the transcript, did the agent follow this step: '{question}'?\nTranscript: {text}\nAnswer with 'Right' or 'Wrong' only:"}
                ],
                temperature=0.2,
                max_tokens=10,
                top_p=1.0,
                frequency_penalty=0.0,
                presence_penalty=0.0,
                stop=["\n"]
            )
            answer = response.choices[0].message.content.strip()
            response_map = {
                "lable": question,
                "icon": answer
            }
            cat_map["responses"].append(response_map)
        results.append(cat_map)
        
    return GenerateResponse4(text=results)

# def custom_qa_automation(text: str, custom_question: str):
#     # Ensure the custom question ends with "Provide Details."
#     if not custom_question.strip().endswith("Provide Details."):
#         custom_question = custom_question.strip() + " Provide Details."

#     response = openai.Completion.create(
#         engine="AmplifAI-Chat",
#         prompt=f"Respons with a Yes/No followed by a reason. Question: {custom_question}\nTranscript: {text}\nAnswer:",
#         temperature=0.2,
#         max_tokens=100,
#         top_p=1.0,
#         frequency_penalty=0.0,
#         presence_penalty=0.0,
#         stop=["\n"]
#     )
#     answer = response.choices[0].text.strip()
#     # Split the answer into the main answer (Yes/No) and the details
#     if "Yes" in answer:
#         main_answer, details = answer.split("Yes", 1)
#         main_answer += "Yes"
#     elif "No" in answer:
#         main_answer, details = answer.split("No", 1)
#         main_answer += "No"
#     else:
#         main_answer = answer
#         details = "No additional details provided."

#     # Format the answer with the "Comments:" tag in bold and on a new line
#     formatted_answer = f"<p><b>Response:</b> {main_answer}</p><p><b>Comment:</b> {details}</p>" #f"**{custom_question}** {main_answer}\n\n**Comments:** {details}"

#     #return GenerateResponse(text=formatted_answer)
#     return formatted_answer # Returning string directly instead of being placed within the GenerateResponse class

def custom_qa_automation(text: str, custom_question: str):
    # Ensure the custom question ends with "Provide Details."
    if not custom_question.strip().endswith("Provide Details."):
        custom_question = custom_question.strip() + " Provide Details."

        response = client.chat.completions.create(
            model = deployment_name,
            messages=[
                {"role": "system", "content": "You are an AI assistant evaluating a customer service call. Keep responses concise."},
                {"role": "user", "content": f"Question: {custom_question}\nTranscript: {text}\nAnswer with a brief Yes/No and a short reason (max 1 sentence):"}
            ],
            temperature=0.2,
            max_tokens=100,
            top_p=1.0,
            frequency_penalty=0.0,
            presence_penalty=0.0,
            stop=["\n"]
        )
        answer = response.choices[0].message.content.strip()
        # Split the answer into the main answer (Yes/No) and the details
        if "Yes" in answer:
            main_answer, details = answer.split("Yes", 1)
            main_answer += "Yes"
        elif "No" in answer:
            main_answer, details = answer.split("No", 1)
            main_answer += "No"
        else:
            main_answer = answer
            details = "No additional details provided."
    
        # Format the answer with the "Comments:" tag in bold and on a new line
        formatted_answer = f"<p><b>Response:</b> {main_answer}</p><p><b>Comment:</b> {details}</p>" #f"**{custom_question}** {main_answer}\n\n**Comments:** {details}"

    #return GenerateResponse(text=formatted_answer)
    return formatted_answer # Returning string directly instead of being placed within the GenerateResponse class

# Transcription function
def transcription(file):
    try:
        deepgram = DeepgramClient(DEEPGRAM_API_KEY)
        if isinstance(file, str):  # File from folder
            with open(file, "rb") as f:
                buffer_data = f.read()
        else:  # Uploaded file
            buffer_data = file.file.read()

        payload = {"buffer": buffer_data}
        options = PrerecordedOptions(model="nova-2", smart_format=True, diarize=True, redact = ['SSN'])
        response = deepgram.listen.prerecorded.v("1").transcribe_file(payload, options)
        return response.results.channels[0].alternatives[0].paragraphs.transcript
    except Exception as e:
        print(f"Exception: {e}")
        return None

def transcription_content(file):
    try:
        deepgram = DeepgramClient(DEEPGRAM_API_KEY)
        if isinstance(file, str):  # File from folder
            with open(file, "rb") as f:
                buffer_data = f.read()
        else:  # Uploaded file
            buffer_data = file.file.read()

        payload = {"buffer": buffer_data}
        options = PrerecordedOptions(model="nova-2", smart_format=True, diarize=True)
        response = deepgram.listen.prerecorded.v("1").transcribe_file(payload, options)
        return response.results.channels[0].alternatives[0].transcript
    except Exception as e:
        print(f"Exception: {e}")
        return None



ALLOWED_EXTENSIONS = {".mp3", ".wav", ".wma"}

app.add_middleware(
    CORSMiddleware,
    allow_origins=["*"],
    allow_credentials=True,
    allow_methods=["*"],
    allow_headers=["*"],
)

# Middleware to handle large request body size (e.g., up to 50MB)
class LargeRequestMiddleware(BaseHTTPMiddleware):
    async def dispatch(self, request: Request, call_next):
        request_body_size = request.headers.get('content-length')
        if request_body_size and int(request_body_size) > 50 * 1024 * 1024:  # 50MB limit
            return Response("Request body too large", status_code=413)
        response = await call_next(request)
        return response

# Add LargeRequestMiddleware to app
app.add_middleware(LargeRequestMiddleware)

@app.get("/", tags=["Home"])
def api_home():
    return {'detail': 'Welcome to FastAPI TextGen Tutorial!'}

@app.post("/api/summarize", summary="Generate text from prompt", tags=["Generate"], response_model=GenerateResponse)
def inference(input_prompt: Prompt):
    return summarize_text(text=input_prompt.text)

@app.post("/api/qautomation", summary="Generate text from prompt", tags=["Generate"], response_model=GenerateResponse3)
def inference(input_prompt: Prompt):
    return qa_automation(text=input_prompt.text)

@app.post("/api/verify-call-flow", summary="Generate text from prompt", tags=["Generate"], response_model=GenerateResponse4)
def inference(input_prompt: Prompt):
    return check_agent_steps(text=input_prompt.text)

# @app.post("/api/custom-qa", summary="Generate text from prompt", tags=["Generate"], response_model=GenerateResponse)
# def inference(input_prompt: Prompt, question: Prompt):
#     return custom_qa_automation(text=input_prompt.text, custom_question=question.text)

@app.post("/api/custom-qa", summary="Generate text from prompt", response_class=PlainTextResponse) # Attempt to only return HTML string instead of json output
def inference(input_prompt: Prompt, question: Prompt):
    return custom_qa_automation(text=input_prompt.text, custom_question=question.text)


@app.post("/api/transcribe-display", summary="Transcribe audio file", tags=["Transcription"])
async def transcribe_audio_route(file: UploadFile = File(...)):
    # Check if the file extension is allowed
    file_extension = os.path.splitext(file.filename)[1].lower()
    if file_extension not in ALLOWED_EXTENSIONS:
        raise HTTPException(status_code=400, detail=f"File format not supported. Allowed formats: {', '.join(ALLOWED_EXTENSIONS)}")

    transcription_result = transcription(file)
    if transcription_result:
        return {"transcription": transcription_result}
    else:
        return {"error": "Transcription failed"}

@app.post("/api/transcribe-content", summary="Transcribe audio file", tags=["Transcription"])
async def transcribe_audio_route(file: UploadFile = File(...)):
    # Check if the file extension is allowed
    file_extension = os.path.splitext(file.filename)[1].lower()
    if file_extension not in ALLOWED_EXTENSIONS:
        raise HTTPException(status_code=400, detail=f"File format not supported. Allowed formats: {', '.join(ALLOWED_EXTENSIONS)}")

    transcription_result = transcription_content(file)
    if transcription_result:
        return {"transcription": transcription_result}
    else:
        return {"error": "Transcription failed"}