File size: 1,895 Bytes
c4b3a24
 
 
 
 
1f73fce
c4b3a24
 
5e2264d
c4b3a24
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import openai
import gradio as gr
import time
import warnings
import warnings
import os
from gtts import gTTS


warnings.filterwarnings("ignore")

openai.api_key = "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"

def chatgpt_api(input_text):
    messages = [
    {"role": "system", "content": "You are a helpful assistant."}]
    
    if input_text:
        messages.append(
            {"role": "user", "content": input_text},
        )
        chat_completion = openai.ChatCompletion.create(
            model="gpt-3.5-turbo", messages=messages
        )
    
    reply = chat_completion.choices[0].message.content
    return reply

#ffmpeg -f lavfi -i anullsrc=r=44100:cl=mono -t 10 -q:a 9 -acodec libmp3lame Temp.mp3'

def transcribe(audio, text):
    language = "en"

    if audio is not None:
        with open(audio, "rb") as transcript:
            prompt = openai.Audio.transcribe("whisper-1", transcript)
        s = prompt["text"]
    else:
        s = text
    
    response = openai.Completion.create(
        engine="text-davinci-002",
        prompt=s,
        max_tokens=60,
        n=1,
        stop=None,
        temperature=0.5,
    )
    
    out_result = chatgpt_api(s)
    
    audioobj = gTTS(text = out_result, 
                    lang = language, 
                    slow = False)
    
    audioobj.save("Temp.mp3")

    return [s, out_result, "Temp.mp3"]


with gr.Blocks() as demo:
    gr.Markdown("Dilip AI")
    input1 = gr.inputs.Audio(source="microphone", type = "filepath", label="Use your voice to chat")
    input2 = gr.inputs.Textbox(lines=7, label="Chat with AI")
    output_1 = gr.Textbox(label="User Input")
    output_2 = gr.Textbox(label="Text Output")
    output_3 = gr.Audio("Temp.mp3", label="Speech Output")
    btn = gr.Button("Run")
    btn.click(fn=transcribe, inputs=[input1, input2], outputs=[output_1, output_2, output_3])
demo.launch()