File size: 3,722 Bytes
3796a5e
 
 
 
 
382782b
7947ceb
3796a5e
7947ceb
 
ff1d740
 
4094da1
ff1d740
91e84de
ff1d740
 
057830c
ff1d740
 
 
3796a5e
 
164ab71
3796a5e
 
 
ff1d740
3796a5e
 
 
164ab71
3796a5e
 
 
 
9ec3e03
85df983
 
3796a5e
 
0ed9063
 
 
7a9a684
 
 
 
 
0ed9063
30e5c47
ff1d740
0ed9063
91e84de
0ed9063
5069bf5
0695967
 
 
 
 
0ed9063
 
91e84de
 
0ed9063
 
30e5c47
164ab71
0ed9063
164ab71
 
3796a5e
49e1c3f
c52bc49
49e1c3f
 
 
 
 
 
59716dc
3796a5e
 
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
from pyChatGPT import ChatGPT
import gradio as gr
import os, json
from loguru import logger
import random
from transformers import pipeline
import torch

device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")

whisper = gr.Interface.load(name="spaces/sanchit-gandhi/whisper-large-v2")
openai_chatgpt = gr.Interface.load(name="spaces/anzorq/chatgpt-demo")

#input_message.submit([input_message, history], [input_message, chatbot, history])
def translate_or_transcribe(audio, task):
    text_result = whisper(audio, None, task, fn_index=0)
    return text_result
    
def get_response_from_chatbot(text, chat_history):
    r = openai_chatgpt(message, chat_history)
    response = "Sorry, the chatGPT queue is full. Please try again in some time"
    return response

def chat(message, chat_history):      
    out_chat = []
    if chat_history != '':
        out_chat = json.loads(chat_history)
    response = get_response_from_chatbot(message, chat_history)
    out_chat.append((message, response))
    chat_history = json.dumps(out_chat)
    logger.info(f"out_chat_: {len(out_chat)}")
    return out_chat, chat_history



with gr.Blocks(title='Talk to chatGPT') as demo:
    gr.Markdown("## Talk to chatGPT with your voice in your native language ! ##")
    
    with gr.Group(elem_id="page_2") as page_2:        
        with gr.Row(elem_id="chat_row"):
            chatbot = gr.Chatbot(elem_id="chat_bot", visible=False).style(color_map=("green", "blue"))
            chatbot1 = gr.Chatbot(elem_id="chat_bot1").style(color_map=("green", "blue"))    
        with gr.Row():
            prompt_input_audio = gr.Audio(
                                        source="microphone",
                                        type="filepath",
                                        label="Record Audio Input",
                                        
                                    )
            translate_btn = gr.Button("Check Whisper first ? πŸ‘")
            
        whisper_task = gr.Radio(["translate", "transcribe"], value="transcribe", show_label=False)
        with gr.Row(elem_id="prompt_row"):
            prompt_input = gr.Textbox(lines=2, label="Input text",show_label=True)
            chat_history = gr.Textbox(lines=4, label="prompt", visible=False)
            submit_btn = gr.Button(value = "Send to chatGPT",elem_id="submit-btn").style(
                    margin=True,
                    rounded=(True, True, True, True),
                    width=100
                )
            
             

        translate_btn.click(fn=translate_or_transcribe, 
                         inputs=[prompt_input_audio,whisper_task], 
                         outputs=prompt_input
                        )
        
          
        submit_btn.click(fn=chat, 
                             inputs=[prompt_input, chat_history], 
                             outputs=[chatbot, chat_history],
                            )
    gr.HTML('''
         <p>Note: Please be aware that audio records from iOS devices will not be decoded as expected by Gradio. For the best experience, record your voice from a computer instead of your smartphone ;)</p>
        <div class="footer">
                    <p>Whisper Model by <a href="https://github.com/openai/whisper" style="text-decoration: underline;" target="_blank">OpenAI</a> -   
                    <a href="https://chat.openai.com/chat" target="_blank">chatGPT</a> by <a href="https://openai.com/" style="text-decoration: underline;" target="_blank">OpenAI</a> 
                    </p>
        </div>
        ''')
    gr.Markdown("![visitor badge](https://visitor-badge.glitch.me/badge?page_id=RamAnanth1.chatGPT_voice)")

demo.launch(debug = True)