File size: 1,434 Bytes
60dc67b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import os
import gradio as gr
from dotenv import load_dotenv
import openai
from utils import serialize
from utils import compress

from description import DESCRIPTION

load_dotenv()

# configuring openai package
OPENAI_API_KEY = os.getenv("OPENAI_API_KEY")
openai.api_key = OPENAI_API_KEY


def chat(message, history):
    """
    Sends a request to the OpenAi api based on the user input and the history
    """
    messages = serialize(history)
    messages.append({"role": "user", "content": message})

    completion = openai.ChatCompletion.create(
        model="gpt-3.5-turbo",
        messages=messages,
    )

    return completion["choices"][0]["message"]["content"].strip()


def transcribe(audio_file):
    audio_file = open(audio_file, "rb")
    transcription = openai.Audio.transcribe("whisper-1", audio_file, language="en")
    transcription = transcription["text"]
    return transcription


def predict(input, history=[]):
    compress(input)
    transcription = transcribe(input)

    answer = chat(transcription, history)
    history.append((transcription, answer))
    response = history
    return response, history


with gr.Blocks() as demo:
    gr.Markdown(DESCRIPTION)
    chatbot = gr.Chatbot()
    state = gr.State([])

    with gr.Row():
        audio_file = gr.Audio(label="Audio", source="microphone", type="filepath")

    audio_file.change(predict, [audio_file, state], [chatbot, state])

demo.launch()