lingobot / demos /scene.py
Gent (PG/R - Comp Sci & Elec Eng)
add demos
a20ccd6
import gradio as gr
import time
from utils.asr import recognize_from_file
from utils.tts import tts
from utils.GPT import generate_response
chat_history = [
]
def convert_chatbox(chat_history):
return [f"{i['role']}: {i['content']}" for i in chat_history]
with gr.Blocks() as demo:
scene = gr.Textbox(lines=5, label="scene", placeholder="Enter your scene name")
def init(scene):
global chat_history
chat_history=[
{"role": "system", "content": "Pretend to be a character and have a causal conversation with the user. Respond to the user's questions and ask questions back. The responses start with 'character:' if you are the character and 'GPT:' if you are ChatGPT. You are the character and never respond as the user. The conversation happens in the scene: " + scene},
]
init_bt=gr.Button("Init", type="button", label="Start")
init_bt.click(init, [scene], )
chatbot = gr.Chatbot()
with gr.Row():
msg = gr.Textbox()
audio = gr.Audio(source="microphone", type="filepath", streaming=False)
def respond(message):
# TODO: replace this with real GPT model
chat_history.append({'role': 'user', 'content': message})
result = generate_response(chat_history)
mesg=result['choices'][0]['message']
print("recv: ", mesg)
response = mesg['content']
chat_history.append(mesg)
chatbot.value.append((message,response))
print("chat_history: ", chatbot.value)
return None, chatbot.value
msg.submit(respond, [msg], [msg,chatbot])
def transcribe(audio_file):
print("start transcribe, ", audio_file)
start = time.time()
text = recognize_from_file(audio_file)
print("use ", time.time()-start)
print("transcribe done, ", text)
return respond(text)
audio.change(transcribe, [audio], [audio, chatbot])
demo.launch()