File size: 3,030 Bytes
7bc6133
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
06ad84b
7bc6133
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
06ad84b
 
cfa5b8b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
06ad84b
 
cfa5b8b
7bc6133
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import openai
import requests
import json
from deepgram import (
    DeepgramClient,
    PrerecordedOptions,
    FileSource,
)
import streamlit as st
# Path to the audio file
#AUDIO_FILE = "temp.wav"
res = ''

def extract_transcript(json_data):
    try:
        data = json.loads(json_data)
        transcript = data['results']['channels'][0]['alternatives'][0]['transcript']
        return transcript
    except (KeyError, json.JSONDecodeError):
      
        print("Error: Transcript not found or invalid JSON format.")
        return None

def analyze(AUDIO_FILE):
    try:
        # STEP 1 Create a Deepgram client using the API key
        deepgram = DeepgramClient('DAPI')

        with open(AUDIO_FILE, "rb") as file:
            buffer_data = file.read()

        payload: FileSource = {
            "buffer": buffer_data,
        }

        #STEP 2: Configure Deepgram options for audio analysis
        options = PrerecordedOptions(
            model="nova-2",
            smart_format=True,
        )
        print('before deepgram')
        # STEP 3: Call the transcribe_file method with the text payload and options
        response = deepgram.listen.prerecorded.v("1").transcribe_file(payload, options)

        # STEP 4: Print the response
        res = (extract_transcript(response.to_json(indent=4)))
        print('after deepgram')

        openai.api_key= 'OAPI'
    
        # Define OpenAI API endpoint
        OPENAI_API_URL = 'https://api.openai.com/v1/engines/gpt-3.5-turbo/completions'
        print('before opapi')
        completion = openai.ChatCompletion.create(
            model="gpt-3.5-turbo",
            messages=[
                {"role": "user", "content": "So, in input of Web interface Hiring manager attaches audio with any human conversation. In output Hiring manager should get sentiment or psychological insights derived from the conversation, some insights about speakers. Please don’t provide summary of conversation, key words, etc. Output should be related to sentimental analysis.",
                "role": "user", "content": res}
            ]
        )

        # Extract the message content from the completion response
        message_content = completion['choices'][0]['message']['content']
        print('after opapi')
        # Remove newlines and concatenate into a single line
        message_content_single_line = ' '.join(message_content.splitlines())
        return message_content_single_line

    except Exception as e:
        print(f"Exception: {e}")

    


def main():
    st.title("Audio Conversation Analysis")
    st.write("Upload an audio file containing a conversation, and get insights generated by OpenAI's GPT-3.5 model.")

    # Upload audio file
    uploaded_file = st.file_uploader("Upload an audio file", type=["wav"])

    if uploaded_file is not None:
        st.write("File uploaded successfully!")
        transcript = analyze(uploaded_file)
        st.write("Generated Text:")
        st.write(transcript)

if __name__ == "__main__":
    main()