chatGPT_voice / app.py
RamAnanth1's picture
Update app.py
85df983
raw
history blame
3.72 kB
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)